create.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. "github.com/spf13/cobra"
  17. "github.com/docker/api/cli/mobycli"
  18. "github.com/docker/api/context/store"
  19. )
  20. type descriptionCreateOpts struct {
  21. description string
  22. }
  23. func createCommand() *cobra.Command {
  24. const longHelp = `Create a new context
  25. Create docker engine context:
  26. $ docker context create CONTEXT [flags]
  27. Create Azure Container Instances context:
  28. $ docker context create aci CONTEXT [flags]
  29. (see docker context create aci --help)
  30. Docker endpoint config:
  31. NAME DESCRIPTION
  32. from Copy named context's Docker endpoint configuration
  33. host Docker endpoint on which to connect
  34. ca Trust certs signed only by this CA
  35. cert Path to TLS certificate file
  36. key Path to TLS key file
  37. skip-tls-verify Skip TLS certificate validation
  38. Kubernetes endpoint config:
  39. NAME DESCRIPTION
  40. from Copy named context's Kubernetes endpoint configuration
  41. config-file Path to a Kubernetes config file
  42. context-override Overrides the context set in the kubernetes config file
  43. namespace-override Overrides the namespace set in the kubernetes config file
  44. Example:
  45. $ docker context create my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"`
  46. cmd := &cobra.Command{
  47. Use: "create CONTEXT",
  48. Short: "Create new context",
  49. RunE: func(cmd *cobra.Command, args []string) error {
  50. return mobycli.ExecCmd(cmd)
  51. },
  52. Long: longHelp,
  53. }
  54. cmd.AddCommand(
  55. createAciCommand(),
  56. createLocalCommand(),
  57. createExampleCommand(),
  58. )
  59. flags := cmd.Flags()
  60. flags.String("description", "", "Description of the context")
  61. flags.String(
  62. "default-stack-orchestrator", "",
  63. "Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
  64. flags.StringToString("docker", nil, "set the docker endpoint")
  65. flags.StringToString("kubernetes", nil, "set the kubernetes endpoint")
  66. flags.String("from", "", "create context from a named context")
  67. return cmd
  68. }
  69. func createLocalCommand() *cobra.Command {
  70. var opts descriptionCreateOpts
  71. cmd := &cobra.Command{
  72. Use: "local CONTEXT",
  73. Short: "Create a context for accessing local engine",
  74. Args: cobra.ExactArgs(1),
  75. Hidden: true,
  76. RunE: func(cmd *cobra.Command, args []string) error {
  77. return createDockerContext(cmd.Context(), args[0], store.LocalContextType, opts.description, store.LocalContext{})
  78. },
  79. }
  80. addDescriptionFlag(cmd, &opts.description)
  81. return cmd
  82. }
  83. func createExampleCommand() *cobra.Command {
  84. var opts descriptionCreateOpts
  85. cmd := &cobra.Command{
  86. Use: "example CONTEXT",
  87. Short: "Create a test context returning fixed output",
  88. Args: cobra.ExactArgs(1),
  89. Hidden: true,
  90. RunE: func(cmd *cobra.Command, args []string) error {
  91. return createDockerContext(cmd.Context(), args[0], store.ExampleContextType, opts.description, store.ExampleContext{})
  92. },
  93. }
  94. addDescriptionFlag(cmd, &opts.description)
  95. return cmd
  96. }
  97. func createDockerContext(ctx context.Context, name string, contextType string, description string, data interface{}) error {
  98. s := store.ContextStore(ctx)
  99. result := s.Create(
  100. name,
  101. contextType,
  102. description,
  103. data,
  104. )
  105. return result
  106. }
  107. func contextExists(ctx context.Context, name string) bool {
  108. s := store.ContextStore(ctx)
  109. return s.ContextExists(name)
  110. }
  111. func addDescriptionFlag(cmd *cobra.Command, descriptionOpt *string) {
  112. cmd.Flags().StringVar(descriptionOpt, "description", "", "Description of the context")
  113. }