create.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package context
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "github.com/spf13/cobra"
  19. "github.com/docker/compose-cli/api/context/store"
  20. "github.com/docker/compose-cli/cli/mobycli"
  21. )
  22. type descriptionCreateOpts struct {
  23. description string
  24. }
  25. var extraCommands []func() *cobra.Command
  26. var extraHelp []string
  27. func createCommand() *cobra.Command {
  28. help := strings.Join(extraHelp, "\n")
  29. longHelp := fmt.Sprintf(`Create a new context
  30. Create docker engine context:
  31. $ docker context create CONTEXT [flags]
  32. %s
  33. Docker endpoint config:
  34. NAME DESCRIPTION
  35. from Copy named context's Docker endpoint configuration
  36. host Docker endpoint on which to connect
  37. ca Trust certs signed only by this CA
  38. cert Path to TLS certificate file
  39. key Path to TLS key file
  40. skip-tls-verify Skip TLS certificate validation
  41. Kubernetes endpoint config:
  42. NAME DESCRIPTION
  43. from Copy named context's Kubernetes endpoint configuration
  44. config-file Path to a Kubernetes config file
  45. context-override Overrides the context set in the kubernetes config file
  46. namespace-override Overrides the namespace set in the kubernetes config file
  47. Example:
  48. $ docker context create my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"`, help)
  49. cmd := &cobra.Command{
  50. Use: "create CONTEXT",
  51. Short: "Create new context",
  52. RunE: func(cmd *cobra.Command, args []string) error {
  53. mobycli.Exec(cmd.Root())
  54. return nil
  55. },
  56. Long: longHelp,
  57. }
  58. cmd.AddCommand(
  59. createLocalCommand(),
  60. createExampleCommand(),
  61. )
  62. for _, command := range extraCommands {
  63. cmd.AddCommand(command())
  64. }
  65. flags := cmd.Flags()
  66. flags.String("description", "", "Description of the context")
  67. flags.String(
  68. "default-stack-orchestrator", "",
  69. "Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
  70. flags.StringToString("docker", nil, "Set the docker endpoint")
  71. flags.StringToString("kubernetes", nil, "Set the kubernetes endpoint")
  72. flags.String("from", "", "Create context from a named context")
  73. return cmd
  74. }
  75. func createLocalCommand() *cobra.Command {
  76. var opts descriptionCreateOpts
  77. cmd := &cobra.Command{
  78. Use: "local CONTEXT",
  79. Short: "Create a context for accessing local engine",
  80. Args: cobra.ExactArgs(1),
  81. Hidden: true,
  82. RunE: func(cmd *cobra.Command, args []string) error {
  83. return createDockerContext(cmd.Context(), args[0], store.LocalContextType, opts.description, store.LocalContext{})
  84. },
  85. }
  86. addDescriptionFlag(cmd, &opts.description)
  87. return cmd
  88. }
  89. func createExampleCommand() *cobra.Command {
  90. var opts descriptionCreateOpts
  91. cmd := &cobra.Command{
  92. Use: "example CONTEXT",
  93. Short: "Create a test context returning fixed output",
  94. Args: cobra.ExactArgs(1),
  95. Hidden: true,
  96. RunE: func(cmd *cobra.Command, args []string) error {
  97. return createDockerContext(cmd.Context(), args[0], store.ExampleContextType, opts.description, store.ExampleContext{})
  98. },
  99. }
  100. addDescriptionFlag(cmd, &opts.description)
  101. return cmd
  102. }
  103. func createDockerContext(ctx context.Context, name string, contextType string, description string, data interface{}) error {
  104. s := store.ContextStore(ctx)
  105. result := s.Create(
  106. name,
  107. contextType,
  108. description,
  109. data,
  110. )
  111. fmt.Printf("Successfully created %s context %q\n", contextType, name)
  112. return result
  113. }
  114. func contextExists(ctx context.Context, name string) bool {
  115. s := store.ContextStore(ctx)
  116. return s.ContextExists(name)
  117. }
  118. func addDescriptionFlag(cmd *cobra.Command, descriptionOpt *string) {
  119. cmd.Flags().StringVar(descriptionOpt, "description", "", "Description of the context")
  120. }