2015-09-05 15:49:06 -04:00
|
|
|
package dockerfile
|
2014-08-05 13:17:40 -07:00
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// This file contains the dispatchers for each command. Note that
|
|
|
|
// `nullDispatch` is not actually a command, but support for commands we parse
|
|
|
|
// but do nothing with.
|
|
|
|
//
|
|
|
|
// See evaluator.go for a higher level discussion of the whole evaluator
|
|
|
|
// package.
|
|
|
|
|
2014-08-05 13:17:40 -07:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-21 19:26:20 +00:00
|
|
|
"regexp"
|
2015-05-05 08:39:37 -07:00
|
|
|
"runtime"
|
2014-11-12 03:22:08 -05:00
|
|
|
"sort"
|
2016-04-18 10:48:13 +01:00
|
|
|
"strconv"
|
2014-08-05 13:17:40 -07:00
|
|
|
"strings"
|
2016-04-18 10:48:13 +01:00
|
|
|
"time"
|
2014-08-05 15:41:09 -07:00
|
|
|
|
2017-04-04 12:28:59 -04:00
|
|
|
"bytes"
|
2015-03-26 23:22:04 +01:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-31 05:57:58 -08:00
|
|
|
"github.com/docker/docker/api"
|
2016-11-16 15:02:27 -08:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-09-06 11:18:12 -07:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2017-05-05 18:52:11 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2017-05-05 13:05:25 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile/parser"
|
2015-08-18 10:30:44 -07:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-12-18 12:58:48 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-03-15 15:28:06 -07:00
|
|
|
"github.com/pkg/errors"
|
2014-08-05 13:17:40 -07:00
|
|
|
)
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// ENV foo bar
|
|
|
|
//
|
|
|
|
// Sets the environment variable foo to bar, also makes interpolation
|
|
|
|
// in the dockerfile available from the next statement on via ${foo}.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func env(req dispatchRequest) error {
|
|
|
|
if len(req.args) == 0 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errAtLeastOneArgument("ENV")
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if len(req.args)%2 != 0 {
|
2014-09-25 19:28:24 -07:00
|
|
|
// should never get here, but just in case
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errTooManyArguments("ENV")
|
2014-09-25 19:28:24 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
2017-04-04 12:28:59 -04:00
|
|
|
commitMessage := bytes.NewBufferString("ENV")
|
2016-08-01 09:38:05 -07:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
for j := 0; j < len(req.args); j += 2 {
|
|
|
|
if len(req.args[j]) == 0 {
|
2016-08-26 17:06:07 +08:00
|
|
|
return errBlankCommandNames("ENV")
|
2016-08-01 09:38:05 -07:00
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
name := req.args[j]
|
|
|
|
value := req.args[j+1]
|
2017-04-04 12:28:59 -04:00
|
|
|
newVar := name + "=" + value
|
|
|
|
commitMessage.WriteString(" " + newVar)
|
2014-08-05 13:17:40 -07:00
|
|
|
|
2014-09-25 19:28:24 -07:00
|
|
|
gotOne := false
|
2017-04-26 17:45:16 -04:00
|
|
|
for i, envVar := range runConfig.Env {
|
2014-09-25 19:28:24 -07:00
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
2016-11-22 11:26:02 -08:00
|
|
|
compareFrom := envParts[0]
|
2017-04-04 12:28:59 -04:00
|
|
|
if equalEnvKeys(compareFrom, name) {
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Env[i] = newVar
|
2014-09-25 19:28:24 -07:00
|
|
|
gotOne = true
|
|
|
|
break
|
|
|
|
}
|
2014-08-13 03:07:41 -07:00
|
|
|
}
|
2014-09-25 19:28:24 -07:00
|
|
|
if !gotOne {
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Env = append(runConfig.Env, newVar)
|
2014-09-25 19:28:24 -07:00
|
|
|
}
|
2014-08-13 03:07:41 -07:00
|
|
|
}
|
2014-09-25 19:28:24 -07:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, commitMessage.String())
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// MAINTAINER some text <maybe@an.email.address>
|
|
|
|
//
|
|
|
|
// Sets the maintainer metadata.
|
2017-04-11 14:34:05 -04:00
|
|
|
func maintainer(req dispatchRequest) error {
|
|
|
|
if len(req.args) != 1 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errExactlyOneArgument("MAINTAINER")
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
maintainer := req.args[0]
|
2017-04-26 17:45:16 -04:00
|
|
|
req.state.maintainer = maintainer
|
|
|
|
return req.builder.commit(req.state, "MAINTAINER "+maintainer)
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
|
|
|
|
2015-02-17 07:20:06 -08:00
|
|
|
// LABEL some json data describing the image
|
|
|
|
//
|
|
|
|
// Sets the Label variable foo to bar,
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func label(req dispatchRequest) error {
|
|
|
|
if len(req.args) == 0 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errAtLeastOneArgument("LABEL")
|
2015-02-17 07:20:06 -08:00
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
if len(req.args)%2 != 0 {
|
2015-02-17 07:20:06 -08:00
|
|
|
// should never get here, but just in case
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errTooManyArguments("LABEL")
|
2015-02-17 07:20:06 -08:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-17 07:20:06 -08:00
|
|
|
commitStr := "LABEL"
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
2015-02-17 07:20:06 -08:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
if runConfig.Labels == nil {
|
|
|
|
runConfig.Labels = map[string]string{}
|
2015-02-17 07:20:06 -08:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
for j := 0; j < len(req.args); j++ {
|
2017-04-26 17:45:16 -04:00
|
|
|
name := req.args[j]
|
|
|
|
if name == "" {
|
2016-08-26 17:06:07 +08:00
|
|
|
return errBlankCommandNames("LABEL")
|
2016-08-01 09:38:05 -07:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
value := req.args[j+1]
|
|
|
|
commitStr += " " + name + "=" + value
|
2015-02-17 07:20:06 -08:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Labels[name] = value
|
2015-02-17 07:20:06 -08:00
|
|
|
j++
|
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, commitStr)
|
2015-02-17 07:20:06 -08:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// ADD foo /path
|
|
|
|
//
|
|
|
|
// Add the file 'foo' to '/path'. Tarball and Remote URL (git, http) handling
|
|
|
|
// exist here. If you do not wish to have this automatic handling, use COPY.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func add(req dispatchRequest) error {
|
|
|
|
if len(req.args) < 2 {
|
2016-08-08 15:08:55 +01:00
|
|
|
return errAtLeastTwoArguments("ADD")
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.runContextCommand(req, true, true, "ADD", nil)
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// COPY foo /path
|
|
|
|
//
|
|
|
|
// Same as 'ADD' but without the tar and remote url handling.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func dispatchCopy(req dispatchRequest) error {
|
|
|
|
if len(req.args) < 2 {
|
2016-08-08 15:08:55 +01:00
|
|
|
return errAtLeastTwoArguments("COPY")
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
flFrom := req.flags.AddString("from", "")
|
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:36:28 -07:00
|
|
|
im, err := req.builder.getImageMount(flFrom)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "invalid from flag value %s", flFrom.Value)
|
2017-03-15 15:28:06 -07:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.runContextCommand(req, false, false, "COPY", im)
|
2014-08-05 13:17:40 -07:00
|
|
|
}
|
2014-08-05 15:41:09 -07:00
|
|
|
|
2017-03-27 18:36:28 -07:00
|
|
|
func (b *Builder) getImageMount(fromFlag *Flag) (*imageMount, error) {
|
|
|
|
if !fromFlag.IsUsed() {
|
2017-05-05 18:52:11 -04:00
|
|
|
// TODO: this could return the source in the default case as well?
|
2017-03-27 18:36:28 -07:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
|
|
|
|
imageRefOrID := fromFlag.Value
|
|
|
|
stage, err := b.buildStages.get(fromFlag.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if stage != nil {
|
|
|
|
imageRefOrID = stage.ImageID()
|
2017-03-27 18:36:28 -07:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return b.imageSources.Get(imageRefOrID)
|
2017-03-27 18:36:28 -07:00
|
|
|
}
|
|
|
|
|
2017-03-27 17:51:42 -07:00
|
|
|
// FROM imagename[:tag | @digest] [AS build-stage-name]
|
2014-08-06 22:56:44 -07:00
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func from(req dispatchRequest) error {
|
2017-04-26 17:45:16 -04:00
|
|
|
stageName, err := parseBuildStageName(req.args)
|
2017-03-27 17:51:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
|
|
|
|
req.builder.resetImageCache()
|
2017-03-27 18:36:28 -07:00
|
|
|
image, err := req.builder.getFromImage(req.shlex, req.args[0])
|
2017-02-24 17:19:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
if err := req.builder.buildStages.add(stageName, image); err != nil {
|
2017-03-27 18:36:28 -07:00
|
|
|
return err
|
2017-03-27 17:51:42 -07:00
|
|
|
}
|
2017-05-05 13:05:25 -04:00
|
|
|
req.state.beginStage(stageName, image)
|
2017-04-11 14:34:05 -04:00
|
|
|
req.builder.buildArgs.ResetAllowed()
|
2017-05-05 13:05:25 -04:00
|
|
|
if image.ImageID() == "" {
|
|
|
|
// Typically this means they used "FROM scratch"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return processOnBuild(req)
|
2017-03-27 17:51:42 -07:00
|
|
|
}
|
2016-01-03 19:45:06 -08:00
|
|
|
|
2017-03-27 17:51:42 -07:00
|
|
|
func parseBuildStageName(args []string) (string, error) {
|
|
|
|
stageName := ""
|
|
|
|
switch {
|
|
|
|
case len(args) == 3 && strings.EqualFold(args[1], "as"):
|
|
|
|
stageName = strings.ToLower(args[2])
|
|
|
|
if ok, _ := regexp.MatchString("^[a-z][a-z0-9-_\\.]*$", stageName); !ok {
|
|
|
|
return "", errors.Errorf("invalid name for build stage: %q, name can't start with a number or contain symbols", stageName)
|
|
|
|
}
|
|
|
|
case len(args) != 1:
|
|
|
|
return "", errors.New("FROM requires either one or three arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
return stageName, nil
|
|
|
|
}
|
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
// scratchImage is used as a token for the empty base image. It uses buildStage
|
|
|
|
// as a convenient implementation of builder.Image, but is not actually a
|
|
|
|
// buildStage.
|
|
|
|
var scratchImage builder.Image = &buildStage{}
|
|
|
|
|
|
|
|
func (b *Builder) getFromImage(shlex *ShellLex, name string) (builder.Image, error) {
|
2017-03-27 17:51:42 -07:00
|
|
|
substitutionArgs := []string{}
|
|
|
|
for key, value := range b.buildArgs.GetAllMeta() {
|
|
|
|
substitutionArgs = append(substitutionArgs, key+"="+value)
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:24:41 -04:00
|
|
|
name, err := shlex.ProcessWord(name, substitutionArgs)
|
2017-03-27 17:51:42 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-20 10:28:21 -07:00
|
|
|
}
|
2017-03-16 14:31:02 -07:00
|
|
|
|
2017-05-05 18:52:11 -04:00
|
|
|
if im, ok := b.buildStages.getByName(name); ok {
|
2017-05-05 13:05:25 -04:00
|
|
|
return im, nil
|
2017-03-22 18:36:08 -07:00
|
|
|
}
|
2016-01-03 19:45:06 -08:00
|
|
|
|
2017-03-27 17:51:42 -07:00
|
|
|
// Windows cannot support a container with no base image.
|
|
|
|
if name == api.NoBaseImageSpecifier {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return nil, errors.New("Windows does not support FROM scratch")
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return scratchImage, nil
|
2017-03-27 17:51:42 -07:00
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
imageMount, err := b.imageSources.Get(name)
|
2017-03-27 18:36:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-05 18:52:11 -04:00
|
|
|
return imageMount.Image(), nil
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2017-05-05 13:05:25 -04:00
|
|
|
func processOnBuild(req dispatchRequest) error {
|
|
|
|
dispatchState := req.state
|
|
|
|
// Process ONBUILD triggers if they exist
|
|
|
|
if nTriggers := len(dispatchState.runConfig.OnBuild); nTriggers != 0 {
|
|
|
|
word := "trigger"
|
|
|
|
if nTriggers > 1 {
|
|
|
|
word = "triggers"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(req.builder.Stderr, "# Executing %d build %s...\n", nTriggers, word)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
|
|
|
|
onBuildTriggers := dispatchState.runConfig.OnBuild
|
|
|
|
dispatchState.runConfig.OnBuild = []string{}
|
|
|
|
|
|
|
|
// Reset stdin settings as all build actions run without stdin
|
|
|
|
dispatchState.runConfig.OpenStdin = false
|
|
|
|
dispatchState.runConfig.StdinOnce = false
|
|
|
|
|
|
|
|
// parse the ONBUILD triggers by invoking the parser
|
|
|
|
for _, step := range onBuildTriggers {
|
|
|
|
dockerfile, err := parser.Parse(strings.NewReader(step))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range dockerfile.AST.Children {
|
|
|
|
if err := checkDispatch(n); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
upperCasedCmd := strings.ToUpper(n.Value)
|
|
|
|
switch upperCasedCmd {
|
|
|
|
case "ONBUILD":
|
|
|
|
return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
|
|
|
case "MAINTAINER", "FROM":
|
|
|
|
return errors.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := dispatchFromDockerfile(req.builder, dockerfile, dispatchState); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// ONBUILD RUN echo yo
|
|
|
|
//
|
|
|
|
// ONBUILD triggers run when the image is used in a FROM statement.
|
|
|
|
//
|
|
|
|
// ONBUILD handling has a lot of special-case functionality, the heading in
|
|
|
|
// evaluator.go and comments around dispatch() in the same file explain the
|
|
|
|
// special cases. search for 'OnBuild' in internals.go for additional special
|
|
|
|
// cases.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func onbuild(req dispatchRequest) error {
|
|
|
|
if len(req.args) == 0 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errAtLeastOneArgument("ONBUILD")
|
2015-02-04 09:34:25 -08:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
triggerInstruction := strings.ToUpper(strings.TrimSpace(req.args[0]))
|
2014-08-05 15:41:09 -07:00
|
|
|
switch triggerInstruction {
|
|
|
|
case "ONBUILD":
|
2016-12-25 14:37:31 +08:00
|
|
|
return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
2014-08-05 15:41:09 -07:00
|
|
|
case "MAINTAINER", "FROM":
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", triggerInstruction)
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
2017-04-11 14:34:05 -04:00
|
|
|
original := regexp.MustCompile(`(?i)^\s*ONBUILD\s*`).ReplaceAllString(req.original, "")
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.OnBuild = append(runConfig.OnBuild, original)
|
|
|
|
return req.builder.commit(req.state, "ONBUILD "+original)
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// WORKDIR /tmp
|
|
|
|
//
|
|
|
|
// Set the working directory for future RUN/CMD/etc statements.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func workdir(req dispatchRequest) error {
|
|
|
|
if len(req.args) != 1 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errExactlyOneArgument("WORKDIR")
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
err := req.flags.Parse()
|
2016-05-02 18:33:59 -07:00
|
|
|
if err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
2015-08-26 16:39:16 -07:00
|
|
|
// This is from the Dockerfile and will not necessarily be in platform
|
|
|
|
// specific semantics, hence ensure it is converted.
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.WorkingDir, err = normaliseWorkdir(runConfig.WorkingDir, req.args[0])
|
2016-05-02 18:33:59 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
2014-12-12 10:32:11 -08:00
|
|
|
|
2016-11-16 15:02:27 -08:00
|
|
|
// For performance reasons, we explicitly do a create/mkdir now
|
|
|
|
// This avoids having an unnecessary expensive mount/unmount calls
|
|
|
|
// (on Windows in particular) during each container create.
|
|
|
|
// Prior to 1.13, the mkdir was deferred and not executed at this step.
|
2017-04-11 14:34:05 -04:00
|
|
|
if req.builder.disableCommit {
|
2016-11-16 15:02:27 -08:00
|
|
|
// Don't call back into the daemon if we're going through docker commit --change "WORKDIR /foo".
|
|
|
|
// We've already updated the runConfig and that's enough.
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-28 15:44:10 -08:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
comment := "WORKDIR " + runConfig.WorkingDir
|
|
|
|
runConfigWithCommentCmd := copyRunConfig(runConfig, withCmdCommentString(comment))
|
|
|
|
if hit, err := req.builder.probeCache(req.state, runConfigWithCommentCmd); err != nil || hit {
|
2016-11-28 15:44:10 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
container, err := req.builder.docker.ContainerCreate(types.ContainerCreateConfig{
|
2017-04-21 15:08:11 -04:00
|
|
|
Config: runConfigWithCommentCmd,
|
2016-12-19 15:18:06 -05:00
|
|
|
// Set a log config to override any default value set on the daemon
|
|
|
|
HostConfig: &container.HostConfig{LogConfig: defaultLogConfig},
|
|
|
|
})
|
2016-11-16 15:02:27 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
req.builder.tmpContainers[container.ID] = struct{}{}
|
|
|
|
if err := req.builder.docker.ContainerCreateWorkdir(container.ID); err != nil {
|
2016-11-16 15:02:27 -08:00
|
|
|
return err
|
|
|
|
}
|
2016-10-28 19:24:37 -07:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commitContainer(req.state, container.ID, runConfigWithCommentCmd)
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// RUN some command yo
|
|
|
|
//
|
|
|
|
// run a command and commit the image. Args are automatically prepended with
|
2016-05-03 13:56:59 -07:00
|
|
|
// the current SHELL which defaults to 'sh -c' under linux or 'cmd /S /C' under
|
|
|
|
// Windows, in the event there is only one argument The difference in processing:
|
2014-08-06 22:56:44 -07:00
|
|
|
//
|
2015-05-05 08:39:37 -07:00
|
|
|
// RUN echo hi # sh -c echo hi (Linux)
|
|
|
|
// RUN echo hi # cmd /S /C echo hi (Windows)
|
2014-08-06 22:56:44 -07:00
|
|
|
// RUN [ "echo", "hi" ] # echo hi
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func run(req dispatchRequest) error {
|
2017-04-26 17:45:16 -04:00
|
|
|
if !req.state.hasFromImage() {
|
2016-12-25 14:37:31 +08:00
|
|
|
return errors.New("Please provide a source image with `from` prior to run")
|
2014-09-11 05:58:50 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
stateRunConfig := req.state.runConfig
|
2017-04-11 14:34:05 -04:00
|
|
|
args := handleJSONArgs(req.args, req.attributes)
|
|
|
|
if !req.attributes["json"] {
|
2017-04-26 17:45:16 -04:00
|
|
|
args = append(getShell(stateRunConfig), args...)
|
2014-08-30 04:34:09 -07:00
|
|
|
}
|
2017-04-21 15:08:11 -04:00
|
|
|
cmdFromArgs := strslice.StrSlice(args)
|
2017-04-26 17:45:16 -04:00
|
|
|
buildArgs := req.builder.buildArgs.FilterAllowed(stateRunConfig.Env)
|
2014-08-05 15:41:09 -07:00
|
|
|
|
2017-04-21 15:08:11 -04:00
|
|
|
saveCmd := cmdFromArgs
|
|
|
|
if len(buildArgs) > 0 {
|
|
|
|
saveCmd = prependEnvOnCmd(req.builder.buildArgs, buildArgs, cmdFromArgs)
|
2016-01-05 08:48:09 -08:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfigForCacheProbe := copyRunConfig(stateRunConfig,
|
2017-04-25 12:21:43 -04:00
|
|
|
withCmd(saveCmd),
|
|
|
|
withEntrypointOverride(saveCmd, nil))
|
2017-04-26 17:45:16 -04:00
|
|
|
hit, err := req.builder.probeCache(req.state, runConfigForCacheProbe)
|
2017-04-21 14:11:21 -04:00
|
|
|
if err != nil || hit {
|
2014-08-05 15:41:09 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := copyRunConfig(stateRunConfig,
|
2017-04-21 15:08:11 -04:00
|
|
|
withCmd(cmdFromArgs),
|
2017-04-26 17:45:16 -04:00
|
|
|
withEnv(append(stateRunConfig.Env, buildArgs...)),
|
2017-04-25 12:21:43 -04:00
|
|
|
withEntrypointOverride(saveCmd, strslice.StrSlice{""}))
|
2017-04-21 15:08:11 -04:00
|
|
|
|
2015-11-09 11:49:16 -08:00
|
|
|
// set config as already being escaped, this prevents double escaping on windows
|
2017-04-21 15:08:11 -04:00
|
|
|
runConfig.ArgsEscaped = true
|
2014-11-14 10:59:14 -08:00
|
|
|
|
2017-04-21 15:08:11 -04:00
|
|
|
logrus.Debugf("[BUILDER] Command to be executed: %v", runConfig.Cmd)
|
|
|
|
cID, err := req.builder.create(runConfig)
|
2014-08-05 15:41:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-21 15:08:11 -04:00
|
|
|
if err := req.builder.run(cID, runConfig.Cmd); err != nil {
|
2014-08-05 15:41:09 -07:00
|
|
|
return err
|
|
|
|
}
|
2014-11-14 10:59:14 -08:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commitContainer(req.state, cID, runConfigForCacheProbe)
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2017-04-21 15:08:11 -04:00
|
|
|
// Derive the command to use for probeCache() and to commit in this container.
|
|
|
|
// Note that we only do this if there are any build-time env vars. Also, we
|
|
|
|
// use the special argument "|#" at the start of the args array. This will
|
|
|
|
// avoid conflicts with any RUN command since commands can not
|
|
|
|
// start with | (vertical bar). The "#" (number of build envs) is there to
|
|
|
|
// help ensure proper cache matches. We don't want a RUN command
|
|
|
|
// that starts with "foo=abc" to be considered part of a build-time env var.
|
|
|
|
//
|
|
|
|
// remove any unreferenced built-in args from the environment variables.
|
|
|
|
// These args are transparent so resulting image should be the same regardless
|
|
|
|
// of the value.
|
2017-04-28 12:49:50 -04:00
|
|
|
func prependEnvOnCmd(buildArgs *buildArgs, buildArgVars []string, cmd strslice.StrSlice) strslice.StrSlice {
|
|
|
|
var tmpBuildEnv []string
|
|
|
|
for _, env := range buildArgVars {
|
|
|
|
key := strings.SplitN(env, "=", 2)[0]
|
2017-05-09 11:25:33 -04:00
|
|
|
if buildArgs.IsReferencedOrNotBuiltin(key) {
|
2017-04-28 12:49:50 -04:00
|
|
|
tmpBuildEnv = append(tmpBuildEnv, env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(tmpBuildEnv)
|
|
|
|
tmpEnv := append([]string{fmt.Sprintf("|%d", len(tmpBuildEnv))}, tmpBuildEnv...)
|
|
|
|
return strslice.StrSlice(append(tmpEnv, cmd...))
|
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// CMD foo
|
|
|
|
//
|
|
|
|
// Set the default command to run in the container (which may be empty).
|
|
|
|
// Argument handling is the same as RUN.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func cmd(req dispatchRequest) error {
|
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
2017-04-11 14:34:05 -04:00
|
|
|
cmdSlice := handleJSONArgs(req.args, req.attributes)
|
|
|
|
if !req.attributes["json"] {
|
2017-04-26 17:45:16 -04:00
|
|
|
cmdSlice = append(getShell(runConfig), cmdSlice...)
|
2014-08-30 04:34:09 -07:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Cmd = strslice.StrSlice(cmdSlice)
|
2016-05-20 16:05:14 -07:00
|
|
|
// set config as already being escaped, this prevents double escaping on windows
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.ArgsEscaped = true
|
2015-04-10 17:05:21 -07:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
if err := req.builder.commit(req.state, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
|
2014-08-05 15:41:09 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if len(req.args) != 0 {
|
2017-04-26 17:45:16 -04:00
|
|
|
req.state.cmdSet = true
|
2014-08-30 04:34:09 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 15:41:09 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 10:48:13 +01:00
|
|
|
// parseOptInterval(flag) is the duration of flag.Value, or 0 if
|
2017-04-10 18:58:31 -07:00
|
|
|
// empty. An error is reported if the value is given and less than minimum duration.
|
2016-04-18 10:48:13 +01:00
|
|
|
func parseOptInterval(f *Flag) (time.Duration, error) {
|
|
|
|
s := f.Value
|
|
|
|
if s == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
d, err := time.ParseDuration(s)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-04-10 18:58:31 -07:00
|
|
|
if d < time.Duration(container.MinimumDuration) {
|
|
|
|
return 0, fmt.Errorf("Interval %#v cannot be less than %s", f.name, container.MinimumDuration)
|
2016-04-18 10:48:13 +01:00
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HEALTHCHECK foo
|
|
|
|
//
|
|
|
|
// Set the default healthcheck command to run in the container (which may be empty).
|
|
|
|
// Argument handling is the same as RUN.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func healthcheck(req dispatchRequest) error {
|
|
|
|
if len(req.args) == 0 {
|
2016-08-26 17:06:07 +08:00
|
|
|
return errAtLeastOneArgument("HEALTHCHECK")
|
2016-04-18 10:48:13 +01:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
2017-04-11 14:34:05 -04:00
|
|
|
typ := strings.ToUpper(req.args[0])
|
|
|
|
args := req.args[1:]
|
2016-04-18 10:48:13 +01:00
|
|
|
if typ == "NONE" {
|
|
|
|
if len(args) != 0 {
|
2016-12-25 14:37:31 +08:00
|
|
|
return errors.New("HEALTHCHECK NONE takes no arguments")
|
2016-04-18 10:48:13 +01:00
|
|
|
}
|
|
|
|
test := strslice.StrSlice{typ}
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Healthcheck = &container.HealthConfig{
|
2016-04-18 10:48:13 +01:00
|
|
|
Test: test,
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-26 17:45:16 -04:00
|
|
|
if runConfig.Healthcheck != nil {
|
|
|
|
oldCmd := runConfig.Healthcheck.Test
|
2016-04-18 10:48:13 +01:00
|
|
|
if len(oldCmd) > 0 && oldCmd[0] != "NONE" {
|
2017-04-11 14:34:05 -04:00
|
|
|
fmt.Fprintf(req.builder.Stdout, "Note: overriding previous HEALTHCHECK: %v\n", oldCmd)
|
2016-04-18 10:48:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
healthcheck := container.HealthConfig{}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
flInterval := req.flags.AddString("interval", "")
|
|
|
|
flTimeout := req.flags.AddString("timeout", "")
|
|
|
|
flStartPeriod := req.flags.AddString("start-period", "")
|
|
|
|
flRetries := req.flags.AddString("retries", "")
|
2016-04-18 10:48:13 +01:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2016-04-18 10:48:13 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case "CMD":
|
2017-04-11 14:34:05 -04:00
|
|
|
cmdSlice := handleJSONArgs(args, req.attributes)
|
2016-04-18 10:48:13 +01:00
|
|
|
if len(cmdSlice) == 0 {
|
2016-12-25 14:37:31 +08:00
|
|
|
return errors.New("Missing command after HEALTHCHECK CMD")
|
2016-04-18 10:48:13 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if !req.attributes["json"] {
|
2016-04-18 10:48:13 +01:00
|
|
|
typ = "CMD-SHELL"
|
|
|
|
}
|
|
|
|
|
|
|
|
healthcheck.Test = strslice.StrSlice(append([]string{typ}, cmdSlice...))
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown type %#v in HEALTHCHECK (try CMD)", typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
interval, err := parseOptInterval(flInterval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
healthcheck.Interval = interval
|
|
|
|
|
|
|
|
timeout, err := parseOptInterval(flTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
healthcheck.Timeout = timeout
|
|
|
|
|
2016-11-29 10:58:47 +01:00
|
|
|
startPeriod, err := parseOptInterval(flStartPeriod)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
healthcheck.StartPeriod = startPeriod
|
|
|
|
|
2016-04-18 10:48:13 +01:00
|
|
|
if flRetries.Value != "" {
|
|
|
|
retries, err := strconv.ParseInt(flRetries.Value, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if retries < 1 {
|
|
|
|
return fmt.Errorf("--retries must be at least 1 (not %d)", retries)
|
|
|
|
}
|
|
|
|
healthcheck.Retries = int(retries)
|
|
|
|
} else {
|
|
|
|
healthcheck.Retries = 0
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Healthcheck = &healthcheck
|
2016-04-18 10:48:13 +01:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, fmt.Sprintf("HEALTHCHECK %q", runConfig.Healthcheck))
|
2016-04-18 10:48:13 +01:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// ENTRYPOINT /usr/sbin/nginx
|
|
|
|
//
|
2016-05-03 13:56:59 -07:00
|
|
|
// Set the entrypoint to /usr/sbin/nginx. Will accept the CMD as the arguments
|
|
|
|
// to /usr/sbin/nginx. Uses the default shell if not in JSON format.
|
2014-08-06 22:56:44 -07:00
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
// Handles command processing similar to CMD and RUN, only req.runConfig.Entrypoint
|
2017-04-13 14:37:32 -04:00
|
|
|
// is initialized at newBuilder time instead of through argument parsing.
|
2014-08-06 22:56:44 -07:00
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func entrypoint(req dispatchRequest) error {
|
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
2017-04-11 14:34:05 -04:00
|
|
|
parsed := handleJSONArgs(req.args, req.attributes)
|
2014-10-07 22:39:50 +00:00
|
|
|
|
|
|
|
switch {
|
2017-04-11 14:34:05 -04:00
|
|
|
case req.attributes["json"]:
|
2014-10-07 22:39:50 +00:00
|
|
|
// ENTRYPOINT ["echo", "hi"]
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Entrypoint = strslice.StrSlice(parsed)
|
2014-10-24 00:23:25 +00:00
|
|
|
case len(parsed) == 0:
|
|
|
|
// ENTRYPOINT []
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Entrypoint = nil
|
2014-10-07 22:39:50 +00:00
|
|
|
default:
|
2014-10-24 00:23:25 +00:00
|
|
|
// ENTRYPOINT echo hi
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Entrypoint = strslice.StrSlice(append(getShell(runConfig), parsed[0]))
|
2014-10-07 22:39:50 +00:00
|
|
|
}
|
2014-08-05 15:41:09 -07:00
|
|
|
|
2014-10-07 22:39:50 +00:00
|
|
|
// when setting the entrypoint if a CMD was not explicitly set then
|
|
|
|
// set the command to nil
|
2017-04-26 17:45:16 -04:00
|
|
|
if !req.state.cmdSet {
|
|
|
|
runConfig.Cmd = nil
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
2014-08-13 03:07:41 -07:00
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, fmt.Sprintf("ENTRYPOINT %q", runConfig.Entrypoint))
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// EXPOSE 6667/tcp 7000/tcp
|
|
|
|
//
|
|
|
|
// Expose ports for links and port mappings. This all ends up in
|
2017-04-11 14:34:05 -04:00
|
|
|
// req.runConfig.ExposedPorts for runconfig.
|
2014-08-06 22:56:44 -07:00
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func expose(req dispatchRequest) error {
|
|
|
|
portsTab := req.args
|
2014-08-05 15:41:09 -07:00
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if len(req.args) == 0 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errAtLeastOneArgument("EXPOSE")
|
2015-02-04 09:34:25 -08:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
|
|
|
if runConfig.ExposedPorts == nil {
|
|
|
|
runConfig.ExposedPorts = make(nat.PortSet)
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2015-05-24 15:17:29 +02:00
|
|
|
ports, _, err := nat.ParsePortSpecs(portsTab)
|
2014-08-05 15:41:09 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:22:08 -05:00
|
|
|
// instead of using ports directly, we build a list of ports and sort it so
|
|
|
|
// the order is consistent. This prevents cache burst where map ordering
|
|
|
|
// changes between builds
|
|
|
|
portList := make([]string, len(ports))
|
|
|
|
var i int
|
2014-08-05 15:41:09 -07:00
|
|
|
for port := range ports {
|
2017-04-26 17:45:16 -04:00
|
|
|
if _, exists := runConfig.ExposedPorts[port]; !exists {
|
|
|
|
runConfig.ExposedPorts[port] = struct{}{}
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
2014-11-12 03:22:08 -05:00
|
|
|
portList[i] = string(port)
|
|
|
|
i++
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
2014-11-12 03:22:08 -05:00
|
|
|
sort.Strings(portList)
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, "EXPOSE "+strings.Join(portList, " "))
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// USER foo
|
|
|
|
//
|
|
|
|
// Set the user to 'foo' for future commands and when running the
|
|
|
|
// ENTRYPOINT/CMD at container run time.
|
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func user(req dispatchRequest) error {
|
|
|
|
if len(req.args) != 1 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errExactlyOneArgument("USER")
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
req.state.runConfig.User = req.args[0]
|
|
|
|
return req.builder.commit(req.state, fmt.Sprintf("USER %v", req.args))
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 22:56:44 -07:00
|
|
|
// VOLUME /foo
|
|
|
|
//
|
2014-09-11 06:27:51 -07:00
|
|
|
// Expose the volume /foo for use. Will also accept the JSON array form.
|
2014-08-06 22:56:44 -07:00
|
|
|
//
|
2017-04-11 14:34:05 -04:00
|
|
|
func volume(req dispatchRequest) error {
|
|
|
|
if len(req.args) == 0 {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return errAtLeastOneArgument("VOLUME")
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
if err := req.flags.Parse(); err != nil {
|
2015-05-05 14:27:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig := req.state.runConfig
|
|
|
|
if runConfig.Volumes == nil {
|
|
|
|
runConfig.Volumes = map[string]struct{}{}
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
for _, v := range req.args {
|
2015-03-20 21:39:49 -07:00
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
if v == "" {
|
2016-12-25 14:37:31 +08:00
|
|
|
return errors.New("VOLUME specified can not be an empty string")
|
2015-03-20 21:39:49 -07:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
runConfig.Volumes[v] = struct{}{}
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, fmt.Sprintf("VOLUME %v", req.args))
|
2014-08-05 15:41:09 -07:00
|
|
|
}
|
2015-08-18 10:30:44 -07:00
|
|
|
|
|
|
|
// STOPSIGNAL signal
|
|
|
|
//
|
|
|
|
// Set the signal that will be used to kill the container.
|
2017-04-11 14:34:05 -04:00
|
|
|
func stopSignal(req dispatchRequest) error {
|
|
|
|
if len(req.args) != 1 {
|
2016-08-26 17:06:07 +08:00
|
|
|
return errExactlyOneArgument("STOPSIGNAL")
|
2015-08-18 10:30:44 -07:00
|
|
|
}
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
sig := req.args[0]
|
2015-08-18 10:30:44 -07:00
|
|
|
_, err := signal.ParseSignal(sig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:45:16 -04:00
|
|
|
req.state.runConfig.StopSignal = sig
|
|
|
|
return req.builder.commit(req.state, fmt.Sprintf("STOPSIGNAL %v", req.args))
|
2015-08-18 10:30:44 -07:00
|
|
|
}
|
2014-11-14 10:59:14 -08:00
|
|
|
|
|
|
|
// ARG name[=value]
|
|
|
|
//
|
|
|
|
// Adds the variable foo to the trusted list of variables that can be passed
|
2017-02-16 23:56:53 +08:00
|
|
|
// to builder using the --build-arg flag for expansion/substitution or passing to 'run'.
|
2014-11-14 10:59:14 -08:00
|
|
|
// Dockerfile author may optionally set a default value of this variable.
|
2017-04-11 14:34:05 -04:00
|
|
|
func arg(req dispatchRequest) error {
|
|
|
|
if len(req.args) != 1 {
|
2016-08-26 17:06:07 +08:00
|
|
|
return errExactlyOneArgument("ARG")
|
2014-11-14 10:59:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
name string
|
2016-12-03 05:46:04 -08:00
|
|
|
newValue string
|
2014-11-14 10:59:14 -08:00
|
|
|
hasDefault bool
|
|
|
|
)
|
|
|
|
|
2017-04-11 14:34:05 -04:00
|
|
|
arg := req.args[0]
|
2014-11-14 10:59:14 -08:00
|
|
|
// 'arg' can just be a name or name-value pair. Note that this is different
|
|
|
|
// from 'env' that handles the split of name and value at the parser level.
|
|
|
|
// The reason for doing it differently for 'arg' is that we support just
|
|
|
|
// defining an arg and not assign it a value (while 'env' always expects a
|
|
|
|
// name-value pair). If possible, it will be good to harmonize the two.
|
|
|
|
if strings.Contains(arg, "=") {
|
|
|
|
parts := strings.SplitN(arg, "=", 2)
|
2016-08-01 09:38:05 -07:00
|
|
|
if len(parts[0]) == 0 {
|
2016-08-26 17:06:07 +08:00
|
|
|
return errBlankCommandNames("ARG")
|
2016-08-01 09:38:05 -07:00
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:14 -08:00
|
|
|
name = parts[0]
|
2016-12-03 05:46:04 -08:00
|
|
|
newValue = parts[1]
|
2014-11-14 10:59:14 -08:00
|
|
|
hasDefault = true
|
|
|
|
} else {
|
|
|
|
name = arg
|
|
|
|
hasDefault = false
|
|
|
|
}
|
|
|
|
|
2017-03-16 14:31:02 -07:00
|
|
|
var value *string
|
|
|
|
if hasDefault {
|
|
|
|
value = &newValue
|
2014-11-14 10:59:14 -08:00
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
req.builder.buildArgs.AddArg(name, value)
|
2014-11-14 10:59:14 -08:00
|
|
|
|
2017-02-24 17:19:45 -05:00
|
|
|
// Arg before FROM doesn't add a layer
|
2017-04-26 17:45:16 -04:00
|
|
|
if !req.state.hasFromImage() {
|
2017-04-11 14:34:05 -04:00
|
|
|
req.builder.buildArgs.AddMetaArg(name, value)
|
2017-02-24 17:19:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, "ARG "+arg)
|
2014-11-14 10:59:14 -08:00
|
|
|
}
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
|
2016-05-03 13:56:59 -07:00
|
|
|
// SHELL powershell -command
|
|
|
|
//
|
|
|
|
// Set the non-default shell to use.
|
2017-04-11 14:34:05 -04:00
|
|
|
func shell(req dispatchRequest) error {
|
|
|
|
if err := req.flags.Parse(); err != nil {
|
2016-05-03 13:56:59 -07:00
|
|
|
return err
|
|
|
|
}
|
2017-04-11 14:34:05 -04:00
|
|
|
shellSlice := handleJSONArgs(req.args, req.attributes)
|
2016-05-03 13:56:59 -07:00
|
|
|
switch {
|
|
|
|
case len(shellSlice) == 0:
|
|
|
|
// SHELL []
|
|
|
|
return errAtLeastOneArgument("SHELL")
|
2017-04-11 14:34:05 -04:00
|
|
|
case req.attributes["json"]:
|
2016-05-03 13:56:59 -07:00
|
|
|
// SHELL ["powershell", "-command"]
|
2017-04-26 17:45:16 -04:00
|
|
|
req.state.runConfig.Shell = strslice.StrSlice(shellSlice)
|
2016-05-03 13:56:59 -07:00
|
|
|
default:
|
|
|
|
// SHELL powershell -command - not JSON
|
2017-04-11 14:34:05 -04:00
|
|
|
return errNotJSON("SHELL", req.original)
|
2016-05-03 13:56:59 -07:00
|
|
|
}
|
2017-04-26 17:45:16 -04:00
|
|
|
return req.builder.commit(req.state, fmt.Sprintf("SHELL %v", shellSlice))
|
2016-05-03 13:56:59 -07:00
|
|
|
}
|
|
|
|
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
func errAtLeastOneArgument(command string) error {
|
|
|
|
return fmt.Errorf("%s requires at least one argument", command)
|
|
|
|
}
|
|
|
|
|
|
|
|
func errExactlyOneArgument(command string) error {
|
|
|
|
return fmt.Errorf("%s requires exactly one argument", command)
|
|
|
|
}
|
|
|
|
|
2016-08-08 15:08:55 +01:00
|
|
|
func errAtLeastTwoArguments(command string) error {
|
|
|
|
return fmt.Errorf("%s requires at least two arguments", command)
|
|
|
|
}
|
|
|
|
|
2016-08-26 17:06:07 +08:00
|
|
|
func errBlankCommandNames(command string) error {
|
|
|
|
return fmt.Errorf("%s names can not be blank", command)
|
|
|
|
}
|
|
|
|
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
func errTooManyArguments(command string) error {
|
|
|
|
return fmt.Errorf("Bad input to %s, too many arguments", command)
|
|
|
|
}
|