2015-09-05 19:49:06 +00:00
|
|
|
package dockerfile
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2014-08-07 05:56:44 +00: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 20:17:40 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-21 19:26:20 +00:00
|
|
|
"regexp"
|
2015-05-05 15:39:37 +00:00
|
|
|
"runtime"
|
2014-11-12 08:22:08 +00:00
|
|
|
"sort"
|
2016-04-18 09:48:13 +00:00
|
|
|
"strconv"
|
2014-08-05 20:17:40 +00:00
|
|
|
"strings"
|
2016-04-18 09:48:13 +00:00
|
|
|
"time"
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-31 13:57:58 +00:00
|
|
|
"github.com/docker/docker/api"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2015-12-16 16:01:36 +00:00
|
|
|
"github.com/docker/docker/builder"
|
2015-08-18 17:30:44 +00:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2015-12-22 01:05:55 +00:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2015-12-18 17:58:48 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2014-08-05 20:17:40 +00:00
|
|
|
)
|
|
|
|
|
2014-08-07 05:56:44 +00: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}.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func env(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-26 02:28:24 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errAtLeastOneArgument("ENV")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 02:28:24 +00:00
|
|
|
if len(args)%2 != 0 {
|
|
|
|
// 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 15:53:35 +00:00
|
|
|
return errTooManyArguments("ENV")
|
2014-09-26 02:28:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-01-27 15:57:34 +00:00
|
|
|
// TODO/FIXME/NOT USED
|
|
|
|
// Just here to show how to use the builder flags stuff within the
|
|
|
|
// context of a builder command. Will remove once we actually add
|
|
|
|
// a builder command to something!
|
|
|
|
/*
|
2015-09-06 17:26:40 +00:00
|
|
|
flBool1 := b.flags.AddBool("bool1", false)
|
|
|
|
flStr1 := b.flags.AddString("str1", "HI")
|
2015-01-27 15:57:34 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-01-27 15:57:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Bool1:%v\n", flBool1)
|
|
|
|
fmt.Printf("Str1:%v\n", flStr1)
|
|
|
|
*/
|
|
|
|
|
2014-09-26 02:28:24 +00:00
|
|
|
commitStr := "ENV"
|
|
|
|
|
|
|
|
for j := 0; j < len(args); j++ {
|
|
|
|
// name ==> args[j]
|
|
|
|
// value ==> args[j+1]
|
2016-08-01 16:38:05 +00:00
|
|
|
|
|
|
|
if len(args[j]) == 0 {
|
2016-08-26 09:06:07 +00:00
|
|
|
return errBlankCommandNames("ENV")
|
2016-08-01 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
2014-09-26 02:28:24 +00:00
|
|
|
newVar := args[j] + "=" + args[j+1] + ""
|
|
|
|
commitStr += " " + newVar
|
2014-08-05 20:17:40 +00:00
|
|
|
|
2014-09-26 02:28:24 +00:00
|
|
|
gotOne := false
|
2015-09-06 17:26:40 +00:00
|
|
|
for i, envVar := range b.runConfig.Env {
|
2014-09-26 02:28:24 +00:00
|
|
|
envParts := strings.SplitN(envVar, "=", 2)
|
|
|
|
if envParts[0] == args[j] {
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Env[i] = newVar
|
2014-09-26 02:28:24 +00:00
|
|
|
gotOne = true
|
|
|
|
break
|
|
|
|
}
|
2014-08-13 10:07:41 +00:00
|
|
|
}
|
2014-09-26 02:28:24 +00:00
|
|
|
if !gotOne {
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Env = append(b.runConfig.Env, newVar)
|
2014-09-26 02:28:24 +00:00
|
|
|
}
|
|
|
|
j++
|
2014-08-13 10:07:41 +00:00
|
|
|
}
|
2014-09-26 02:28:24 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
return b.commit("", b.runConfig.Cmd, commitStr)
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// MAINTAINER some text <maybe@an.email.address>
|
|
|
|
//
|
|
|
|
// Sets the maintainer metadata.
|
2015-09-06 17:26:40 +00:00
|
|
|
func maintainer(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 20:17:40 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errExactlyOneArgument("MAINTAINER")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 20:17:40 +00:00
|
|
|
b.maintainer = args[0]
|
2015-09-06 17:26:40 +00:00
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("MAINTAINER %s", b.maintainer))
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:20:06 +00:00
|
|
|
// LABEL some json data describing the image
|
|
|
|
//
|
|
|
|
// Sets the Label variable foo to bar,
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func label(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-02-17 15:20:06 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errAtLeastOneArgument("LABEL")
|
2015-02-17 15:20:06 +00:00
|
|
|
}
|
|
|
|
if len(args)%2 != 0 {
|
|
|
|
// 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 15:53:35 +00:00
|
|
|
return errTooManyArguments("LABEL")
|
2015-02-17 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-17 15:20:06 +00:00
|
|
|
commitStr := "LABEL"
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if b.runConfig.Labels == nil {
|
|
|
|
b.runConfig.Labels = map[string]string{}
|
2015-02-17 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < len(args); j++ {
|
|
|
|
// name ==> args[j]
|
|
|
|
// value ==> args[j+1]
|
2016-08-01 16:38:05 +00:00
|
|
|
|
|
|
|
if len(args[j]) == 0 {
|
2016-08-26 09:06:07 +00:00
|
|
|
return errBlankCommandNames("LABEL")
|
2016-08-01 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 15:20:06 +00:00
|
|
|
newVar := args[j] + "=" + args[j+1] + ""
|
|
|
|
commitStr += " " + newVar
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Labels[args[j]] = args[j+1]
|
2015-02-17 15:20:06 +00:00
|
|
|
j++
|
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
return b.commit("", b.runConfig.Cmd, commitStr)
|
2015-02-17 15:20:06 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00: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.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func add(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-16 16:58:20 +00:00
|
|
|
if len(args) < 2 {
|
2016-08-08 14:08:55 +00:00
|
|
|
return errAtLeastTwoArguments("ADD")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
return b.runContextCommand(args, true, true, "ADD")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// COPY foo /path
|
|
|
|
//
|
|
|
|
// Same as 'ADD' but without the tar and remote url handling.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func dispatchCopy(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-16 16:58:20 +00:00
|
|
|
if len(args) < 2 {
|
2016-08-08 14:08:55 +00:00
|
|
|
return errAtLeastTwoArguments("COPY")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
return b.runContextCommand(args, false, false, "COPY")
|
2014-08-05 20:17:40 +00:00
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// FROM imagename
|
|
|
|
//
|
|
|
|
// This sets the image the dockerfile will build on top of.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func from(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errExactlyOneArgument("FROM")
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
name := args[0]
|
|
|
|
|
2016-01-04 03:45:06 +00:00
|
|
|
var (
|
|
|
|
image builder.Image
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-08-15 01:17:19 +00:00
|
|
|
// Windows cannot support a container with no base image.
|
2015-12-31 13:57:58 +00:00
|
|
|
if name == api.NoBaseImageSpecifier {
|
2015-08-15 01:17:19 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return fmt.Errorf("Windows does not support FROM scratch")
|
|
|
|
}
|
2014-10-28 21:06:23 +00:00
|
|
|
b.image = ""
|
|
|
|
b.noBaseImage = true
|
2016-01-04 03:45:06 +00:00
|
|
|
} else {
|
|
|
|
// TODO: don't use `name`, instead resolve it to a digest
|
|
|
|
if !b.options.PullParent {
|
2016-01-20 23:32:02 +00:00
|
|
|
image, err = b.docker.GetImageOnBuild(name)
|
2016-01-04 03:45:06 +00:00
|
|
|
// TODO: shouldn't we error out if error is different from "not found" ?
|
|
|
|
}
|
|
|
|
if image == nil {
|
2016-03-18 21:42:40 +00:00
|
|
|
image, err = b.docker.PullOnBuild(b.clientCtx, name, b.options.AuthConfigs, b.Output)
|
2016-01-04 03:45:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-04 03:45:06 +00:00
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
return b.processImageFrom(image)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00: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.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func onbuild(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-02-04 17:34:25 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errAtLeastOneArgument("ONBUILD")
|
2015-02-04 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
triggerInstruction := strings.ToUpper(strings.TrimSpace(args[0]))
|
|
|
|
switch triggerInstruction {
|
|
|
|
case "ONBUILD":
|
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 15:53:35 +00:00
|
|
|
return fmt.Errorf("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
|
2014-08-05 22:41:09 +00: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 15:53:35 +00:00
|
|
|
return fmt.Errorf("%s isn't allowed as an ONBUILD trigger", triggerInstruction)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-10-21 19:26:20 +00:00
|
|
|
original = regexp.MustCompile(`(?i)^\s*ONBUILD\s*`).ReplaceAllString(original, "")
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.OnBuild = append(b.runConfig.OnBuild, original)
|
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("ONBUILD %s", original))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// WORKDIR /tmp
|
|
|
|
//
|
|
|
|
// Set the working directory for future RUN/CMD/etc statements.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func workdir(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errExactlyOneArgument("WORKDIR")
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:33:59 +00:00
|
|
|
err := b.flags.Parse()
|
|
|
|
if err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-26 23:39:16 +00:00
|
|
|
// This is from the Dockerfile and will not necessarily be in platform
|
|
|
|
// specific semantics, hence ensure it is converted.
|
2016-05-03 01:33:59 +00:00
|
|
|
b.runConfig.WorkingDir, err = normaliseWorkdir(b.runConfig.WorkingDir, args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-12-12 18:32:11 +00:00
|
|
|
|
2016-05-19 20:40:46 +00:00
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("WORKDIR %v", b.runConfig.WorkingDir))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// RUN some command yo
|
|
|
|
//
|
|
|
|
// run a command and commit the image. Args are automatically prepended with
|
2016-05-03 20:56:59 +00: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-07 05:56:44 +00:00
|
|
|
//
|
2015-05-05 15:39:37 +00:00
|
|
|
// RUN echo hi # sh -c echo hi (Linux)
|
|
|
|
// RUN echo hi # cmd /S /C echo hi (Windows)
|
2014-08-07 05:56:44 +00:00
|
|
|
// RUN [ "echo", "hi" ] # echo hi
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func run(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-10-28 21:06:23 +00:00
|
|
|
if b.image == "" && !b.noBaseImage {
|
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 15:53:35 +00:00
|
|
|
return fmt.Errorf("Please provide a source image with `from` prior to run")
|
2014-09-11 12:58:50 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
args = handleJSONArgs(args, attributes)
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-03-09 21:44:14 +00:00
|
|
|
if !attributes["json"] {
|
2016-05-03 20:56:59 +00:00
|
|
|
args = append(getShell(b.runConfig), args...)
|
2014-08-30 11:34:09 +00:00
|
|
|
}
|
2015-12-28 00:58:51 +00:00
|
|
|
config := &container.Config{
|
2016-02-29 11:28:37 +00:00
|
|
|
Cmd: strslice.StrSlice(args),
|
2015-12-28 00:58:51 +00:00
|
|
|
Image: b.image,
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
// stash the cmd
|
2015-09-06 17:26:40 +00:00
|
|
|
cmd := b.runConfig.Cmd
|
2016-02-29 11:28:37 +00:00
|
|
|
if len(b.runConfig.Entrypoint) == 0 && len(b.runConfig.Cmd) == 0 {
|
2016-01-05 16:48:09 +00:00
|
|
|
b.runConfig.Cmd = config.Cmd
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
// stash the config environment
|
2015-09-06 17:26:40 +00:00
|
|
|
env := b.runConfig.Env
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2016-02-29 11:28:37 +00:00
|
|
|
defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
|
2015-09-06 17:26:40 +00:00
|
|
|
defer func(env []string) { b.runConfig.Env = env }(env)
|
2014-11-14 18:59:14 +00:00
|
|
|
|
|
|
|
// derive the net build-time environment for this run. We let config
|
|
|
|
// environment override the build time environment.
|
|
|
|
// This means that we take the b.buildArgs list of env vars and remove
|
|
|
|
// any of those variables that are defined as part of the container. In other
|
|
|
|
// words, anything in b.Config.Env. What's left is the list of build-time env
|
|
|
|
// vars that we need to add to each RUN command - note the list could be empty.
|
|
|
|
//
|
|
|
|
// We don't persist the build time environment with container's config
|
|
|
|
// environment, but just sort and prepend it to the command string at time
|
|
|
|
// of commit.
|
|
|
|
// This helps with tracing back the image's actual environment at the time
|
|
|
|
// of RUN, without leaking it to the final image. It also aids cache
|
|
|
|
// lookup for same image built with same build time environment.
|
|
|
|
cmdBuildEnv := []string{}
|
2015-12-22 01:05:55 +00:00
|
|
|
configEnv := runconfigopts.ConvertKVStringsToMap(b.runConfig.Env)
|
2015-12-29 20:49:17 +00:00
|
|
|
for key, val := range b.options.BuildArgs {
|
2014-11-14 18:59:14 +00:00
|
|
|
if !b.isBuildArgAllowed(key) {
|
|
|
|
// skip build-args that are not in allowed list, meaning they have
|
|
|
|
// not been defined by an "ARG" Dockerfile command yet.
|
|
|
|
// This is an error condition but only if there is no "ARG" in the entire
|
|
|
|
// Dockerfile, so we'll generate any necessary errors after we parsed
|
|
|
|
// the entire file (see 'leftoverArgs' processing in evaluator.go )
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := configEnv[key]; !ok {
|
|
|
|
cmdBuildEnv = append(cmdBuildEnv, fmt.Sprintf("%s=%s", key, val))
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-11-14 18:59:14 +00: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.
|
|
|
|
saveCmd := config.Cmd
|
|
|
|
if len(cmdBuildEnv) > 0 {
|
|
|
|
sort.Strings(cmdBuildEnv)
|
|
|
|
tmpEnv := append([]string{fmt.Sprintf("|%d", len(cmdBuildEnv))}, cmdBuildEnv...)
|
2016-02-29 11:28:37 +00:00
|
|
|
saveCmd = strslice.StrSlice(append(tmpEnv, saveCmd...))
|
2014-11-14 18:59:14 +00:00
|
|
|
}
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Cmd = saveCmd
|
2015-09-29 17:51:40 +00:00
|
|
|
hit, err := b.probeCache()
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if hit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
// set Cmd manually, this is special case only for Dockerfiles
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Cmd = config.Cmd
|
2014-11-14 18:59:14 +00:00
|
|
|
// set build-time environment for 'run'.
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Env = append(b.runConfig.Env, cmdBuildEnv...)
|
2015-11-09 19:49:16 +00:00
|
|
|
// set config as already being escaped, this prevents double escaping on windows
|
|
|
|
b.runConfig.ArgsEscaped = true
|
2014-11-14 18:59:14 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
logrus.Debugf("[BUILDER] Command to be executed: %v", b.runConfig.Cmd)
|
2014-11-14 18:59:14 +00:00
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
cID, err := b.create()
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-13 10:07:41 +00:00
|
|
|
|
2015-12-10 14:35:53 +00:00
|
|
|
if err := b.run(cID); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-11-14 18:59:14 +00:00
|
|
|
|
|
|
|
// revert to original config environment and set the command string to
|
|
|
|
// have the build-time env vars in it (if any) so that future cache look-ups
|
|
|
|
// properly match it.
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Env = env
|
|
|
|
b.runConfig.Cmd = saveCmd
|
2015-12-10 14:35:53 +00:00
|
|
|
return b.commit(cID, cmd, "run")
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// CMD foo
|
|
|
|
//
|
|
|
|
// Set the default command to run in the container (which may be empty).
|
|
|
|
// Argument handling is the same as RUN.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func cmd(b *Builder, args []string, attributes map[string]bool, original string) error {
|
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
cmdSlice := handleJSONArgs(args, attributes)
|
2014-08-05 22:41:09 +00:00
|
|
|
|
2014-10-27 21:15:28 +00:00
|
|
|
if !attributes["json"] {
|
2016-05-03 20:56:59 +00:00
|
|
|
cmdSlice = append(getShell(b.runConfig), cmdSlice...)
|
2014-08-30 11:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 11:28:37 +00:00
|
|
|
b.runConfig.Cmd = strslice.StrSlice(cmdSlice)
|
2016-05-20 23:05:14 +00:00
|
|
|
// set config as already being escaped, this prevents double escaping on windows
|
|
|
|
b.runConfig.ArgsEscaped = true
|
2015-04-11 00:05:21 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.commit("", b.runConfig.Cmd, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-30 11:34:09 +00:00
|
|
|
if len(args) != 0 {
|
|
|
|
b.cmdSet = true
|
|
|
|
}
|
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
// parseOptInterval(flag) is the duration of flag.Value, or 0 if
|
|
|
|
// empty. An error is reported if the value is given and is not positive.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if d <= 0 {
|
|
|
|
return 0, fmt.Errorf("Interval %#v must be positive", f.name)
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
func healthcheck(b *Builder, args []string, attributes map[string]bool, original string) error {
|
|
|
|
if len(args) == 0 {
|
2016-08-26 09:06:07 +00:00
|
|
|
return errAtLeastOneArgument("HEALTHCHECK")
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
|
|
|
typ := strings.ToUpper(args[0])
|
|
|
|
args = args[1:]
|
|
|
|
if typ == "NONE" {
|
|
|
|
if len(args) != 0 {
|
|
|
|
return fmt.Errorf("HEALTHCHECK NONE takes no arguments")
|
|
|
|
}
|
|
|
|
test := strslice.StrSlice{typ}
|
|
|
|
b.runConfig.Healthcheck = &container.HealthConfig{
|
|
|
|
Test: test,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if b.runConfig.Healthcheck != nil {
|
|
|
|
oldCmd := b.runConfig.Healthcheck.Test
|
|
|
|
if len(oldCmd) > 0 && oldCmd[0] != "NONE" {
|
|
|
|
fmt.Fprintf(b.Stdout, "Note: overriding previous HEALTHCHECK: %v\n", oldCmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
healthcheck := container.HealthConfig{}
|
|
|
|
|
|
|
|
flInterval := b.flags.AddString("interval", "")
|
|
|
|
flTimeout := b.flags.AddString("timeout", "")
|
|
|
|
flRetries := b.flags.AddString("retries", "")
|
|
|
|
|
|
|
|
if err := b.flags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case "CMD":
|
|
|
|
cmdSlice := handleJSONArgs(args, attributes)
|
|
|
|
if len(cmdSlice) == 0 {
|
|
|
|
return fmt.Errorf("Missing command after HEALTHCHECK CMD")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !attributes["json"] {
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
b.runConfig.Healthcheck = &healthcheck
|
|
|
|
}
|
|
|
|
|
2016-08-26 09:06:07 +00:00
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("HEALTHCHECK %q", b.runConfig.Healthcheck))
|
2016-04-18 09:48:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// ENTRYPOINT /usr/sbin/nginx
|
|
|
|
//
|
2016-05-03 20:56:59 +00: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-07 05:56:44 +00:00
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
// Handles command processing similar to CMD and RUN, only b.runConfig.Entrypoint
|
2014-08-07 05:56:44 +00:00
|
|
|
// is initialized at NewBuilder time instead of through argument parsing.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func entrypoint(b *Builder, args []string, attributes map[string]bool, original string) error {
|
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:29:03 +00:00
|
|
|
parsed := handleJSONArgs(args, attributes)
|
2014-10-07 22:39:50 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case attributes["json"]:
|
|
|
|
// ENTRYPOINT ["echo", "hi"]
|
2016-02-29 11:28:37 +00:00
|
|
|
b.runConfig.Entrypoint = strslice.StrSlice(parsed)
|
2014-10-24 00:23:25 +00:00
|
|
|
case len(parsed) == 0:
|
|
|
|
// ENTRYPOINT []
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Entrypoint = nil
|
2014-10-07 22:39:50 +00:00
|
|
|
default:
|
2014-10-24 00:23:25 +00:00
|
|
|
// ENTRYPOINT echo hi
|
2016-05-03 20:56:59 +00:00
|
|
|
b.runConfig.Entrypoint = strslice.StrSlice(append(getShell(b.runConfig), parsed[0]))
|
2014-10-07 22:39:50 +00:00
|
|
|
}
|
2014-08-05 22:41:09 +00: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
|
|
|
|
if !b.cmdSet {
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Cmd = nil
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-08-13 10:07:41 +00:00
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.commit("", b.runConfig.Cmd, fmt.Sprintf("ENTRYPOINT %q", b.runConfig.Entrypoint)); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-10-07 22:39:50 +00:00
|
|
|
|
2014-08-05 22:41:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// EXPOSE 6667/tcp 7000/tcp
|
|
|
|
//
|
|
|
|
// Expose ports for links and port mappings. This all ends up in
|
2015-09-06 17:26:40 +00:00
|
|
|
// b.runConfig.ExposedPorts for runconfig.
|
2014-08-07 05:56:44 +00:00
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func expose(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
portsTab := args
|
|
|
|
|
2015-02-04 17:34:25 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errAtLeastOneArgument("EXPOSE")
|
2015-02-04 17:34:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if b.runConfig.ExposedPorts == nil {
|
|
|
|
b.runConfig.ExposedPorts = make(nat.PortSet)
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 13:17:29 +00:00
|
|
|
ports, _, err := nat.ParsePortSpecs(portsTab)
|
2014-08-05 22:41:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-12 08:22:08 +00: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 22:41:09 +00:00
|
|
|
for port := range ports {
|
2015-09-06 17:26:40 +00:00
|
|
|
if _, exists := b.runConfig.ExposedPorts[port]; !exists {
|
|
|
|
b.runConfig.ExposedPorts[port] = struct{}{}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-11-12 08:22:08 +00:00
|
|
|
portList[i] = string(port)
|
|
|
|
i++
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-11-12 08:22:08 +00:00
|
|
|
sort.Strings(portList)
|
2015-09-06 17:26:40 +00:00
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("EXPOSE %s", strings.Join(portList, " ")))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// USER foo
|
|
|
|
//
|
|
|
|
// Set the user to 'foo' for future commands and when running the
|
|
|
|
// ENTRYPOINT/CMD at container run time.
|
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func user(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-08-05 22:41:09 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errExactlyOneArgument("USER")
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.User = args[0]
|
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("USER %v", args))
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 05:56:44 +00:00
|
|
|
// VOLUME /foo
|
|
|
|
//
|
2014-09-11 13:27:51 +00:00
|
|
|
// Expose the volume /foo for use. Will also accept the JSON array form.
|
2014-08-07 05:56:44 +00:00
|
|
|
//
|
2015-09-06 17:26:40 +00:00
|
|
|
func volume(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-09-11 13:27:51 +00:00
|
|
|
if len(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 15:53:35 +00:00
|
|
|
return errAtLeastOneArgument("VOLUME")
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.flags.Parse(); err != nil {
|
2015-05-05 21:27:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
if b.runConfig.Volumes == nil {
|
|
|
|
b.runConfig.Volumes = map[string]struct{}{}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2014-09-11 13:27:51 +00:00
|
|
|
for _, v := range args {
|
2015-03-21 04:39:49 +00:00
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
if v == "" {
|
2016-08-26 09:06:07 +00:00
|
|
|
return fmt.Errorf("VOLUME specified can not be an empty string")
|
2015-03-21 04:39:49 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.Volumes[v] = struct{}{}
|
2014-08-05 22:41:09 +00:00
|
|
|
}
|
2015-09-06 17:26:40 +00:00
|
|
|
if err := b.commit("", b.runConfig.Cmd, fmt.Sprintf("VOLUME %v", args)); err != nil {
|
2014-08-05 22:41:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-18 17:30:44 +00:00
|
|
|
|
|
|
|
// STOPSIGNAL signal
|
|
|
|
//
|
|
|
|
// Set the signal that will be used to kill the container.
|
2015-09-06 17:26:40 +00:00
|
|
|
func stopSignal(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2015-08-18 17:30:44 +00:00
|
|
|
if len(args) != 1 {
|
2016-08-26 09:06:07 +00:00
|
|
|
return errExactlyOneArgument("STOPSIGNAL")
|
2015-08-18 17:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sig := args[0]
|
|
|
|
_, err := signal.ParseSignal(sig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
b.runConfig.StopSignal = sig
|
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("STOPSIGNAL %v", args))
|
2015-08-18 17:30:44 +00:00
|
|
|
}
|
2014-11-14 18:59:14 +00:00
|
|
|
|
|
|
|
// ARG name[=value]
|
|
|
|
//
|
|
|
|
// Adds the variable foo to the trusted list of variables that can be passed
|
|
|
|
// to builder using the --build-arg flag for expansion/subsitution or passing to 'run'.
|
|
|
|
// Dockerfile author may optionally set a default value of this variable.
|
2015-09-06 17:26:40 +00:00
|
|
|
func arg(b *Builder, args []string, attributes map[string]bool, original string) error {
|
2014-11-14 18:59:14 +00:00
|
|
|
if len(args) != 1 {
|
2016-08-26 09:06:07 +00:00
|
|
|
return errExactlyOneArgument("ARG")
|
2014-11-14 18:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
hasDefault bool
|
|
|
|
)
|
|
|
|
|
|
|
|
arg := args[0]
|
|
|
|
// '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 16:38:05 +00:00
|
|
|
if len(parts[0]) == 0 {
|
2016-08-26 09:06:07 +00:00
|
|
|
return errBlankCommandNames("ARG")
|
2016-08-01 16:38:05 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 18:59:14 +00:00
|
|
|
name = parts[0]
|
|
|
|
value = parts[1]
|
|
|
|
hasDefault = true
|
|
|
|
} else {
|
|
|
|
name = arg
|
|
|
|
hasDefault = false
|
|
|
|
}
|
|
|
|
// add the arg to allowed list of build-time args from this step on.
|
|
|
|
b.allowedBuildArgs[name] = true
|
|
|
|
|
|
|
|
// If there is a default value associated with this arg then add it to the
|
|
|
|
// b.buildArgs if one is not already passed to the builder. The args passed
|
2015-12-13 16:00:39 +00:00
|
|
|
// to builder override the default value of 'arg'.
|
2015-12-29 20:49:17 +00:00
|
|
|
if _, ok := b.options.BuildArgs[name]; !ok && hasDefault {
|
|
|
|
b.options.BuildArgs[name] = value
|
2014-11-14 18:59:14 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("ARG %s", arg))
|
2014-11-14 18:59:14 +00: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 15:53:35 +00:00
|
|
|
|
2016-05-03 20:56:59 +00:00
|
|
|
// SHELL powershell -command
|
|
|
|
//
|
|
|
|
// Set the non-default shell to use.
|
|
|
|
func shell(b *Builder, args []string, attributes map[string]bool, original string) error {
|
|
|
|
if err := b.flags.Parse(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
shellSlice := handleJSONArgs(args, attributes)
|
|
|
|
switch {
|
|
|
|
case len(shellSlice) == 0:
|
|
|
|
// SHELL []
|
|
|
|
return errAtLeastOneArgument("SHELL")
|
|
|
|
case attributes["json"]:
|
|
|
|
// SHELL ["powershell", "-command"]
|
|
|
|
b.runConfig.Shell = strslice.StrSlice(shellSlice)
|
|
|
|
default:
|
|
|
|
// SHELL powershell -command - not JSON
|
|
|
|
return errNotJSON("SHELL", original)
|
|
|
|
}
|
|
|
|
return b.commit("", b.runConfig.Cmd, fmt.Sprintf("SHELL %v", shellSlice))
|
|
|
|
}
|
|
|
|
|
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 15:53:35 +00: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 14:08:55 +00:00
|
|
|
func errAtLeastTwoArguments(command string) error {
|
|
|
|
return fmt.Errorf("%s requires at least two arguments", command)
|
|
|
|
}
|
|
|
|
|
2016-08-26 09:06:07 +00: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 15:53:35 +00:00
|
|
|
func errTooManyArguments(command string) error {
|
|
|
|
return fmt.Errorf("Bad input to %s, too many arguments", command)
|
|
|
|
}
|
2016-05-03 20:56:59 +00:00
|
|
|
|
|
|
|
// getShell is a helper function which gets the right shell for prefixing the
|
|
|
|
// shell-form of RUN, ENTRYPOINT and CMD instructions
|
|
|
|
func getShell(c *container.Config) []string {
|
|
|
|
if 0 == len(c.Shell) {
|
|
|
|
return defaultShell[:]
|
|
|
|
}
|
|
|
|
return c.Shell[:]
|
|
|
|
}
|