docker_config.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package config
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/fatih/structs"
  6. )
  7. func (d *DockerConfig) FullArgs() []string {
  8. args := []string{"daemon"}
  9. args = append(args, generateEngineOptsSlice(d.EngineOpts)...)
  10. args = append(args, d.ExtraArgs...)
  11. if d.TLS {
  12. args = append(args, d.TLSArgs...)
  13. }
  14. return args
  15. }
  16. func (d *DockerConfig) AppendEnv() []string {
  17. return append(os.Environ(), d.Environment...)
  18. }
  19. func generateEngineOptsSlice(opts EngineOpts) []string {
  20. optsStruct := structs.New(opts)
  21. var optsSlice []string
  22. for k, v := range optsStruct.Map() {
  23. optTag := optsStruct.Field(k).Tag("opt")
  24. switch value := v.(type) {
  25. case string:
  26. if value != "" {
  27. optsSlice = append(optsSlice, fmt.Sprintf("--%s", optTag), value)
  28. }
  29. case *bool:
  30. if value != nil {
  31. if *value {
  32. optsSlice = append(optsSlice, fmt.Sprintf("--%s", optTag))
  33. } else {
  34. optsSlice = append(optsSlice, fmt.Sprintf("--%s=false", optTag))
  35. }
  36. }
  37. case map[string]string:
  38. for k, v := range value {
  39. optsSlice = append(optsSlice, fmt.Sprintf("--%s", optTag), fmt.Sprintf("%s=%s", k, v))
  40. }
  41. }
  42. }
  43. return optsSlice
  44. }