update.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "github.com/pkg/errors"
  16. "github.com/spf13/cobra"
  17. "github.com/docker/compose-cli/api/context/store"
  18. "github.com/docker/compose-cli/cli/mobycli"
  19. "github.com/docker/compose-cli/errdefs"
  20. )
  21. func updateCommand() *cobra.Command {
  22. longHelp := `Update a context
  23. Docker endpoint config:
  24. NAME DESCRIPTION
  25. from Copy named context's Docker endpoint configuration
  26. host Docker endpoint on which to connect
  27. ca Trust certs signed only by this CA
  28. cert Path to TLS certificate file
  29. key Path to TLS key file
  30. skip-tls-verify Skip TLS certificate validation
  31. Kubernetes endpoint config:
  32. NAME DESCRIPTION
  33. from Copy named context's Kubernetes endpoint configuration
  34. config-file Path to a Kubernetes config file
  35. context-override Overrides the context set in the kubernetes config file
  36. namespace-override Overrides the namespace set in the kubernetes config file
  37. Example:
  38. $ docker context update my-context --description "some description" --docker "host=tcp://myserver:2376,ca=~/ca-file,cert=~/cert-file,key=~/key-file"`
  39. cmd := &cobra.Command{
  40. Use: "update",
  41. Short: "Update a context",
  42. Args: cobra.ExactArgs(1),
  43. RunE: func(cmd *cobra.Command, args []string) error {
  44. return runUpdate(cmd, args[0])
  45. },
  46. Long: longHelp,
  47. }
  48. flags := cmd.Flags()
  49. flags.String("description", "", "Description of the context")
  50. flags.String(
  51. "default-stack-orchestrator", "",
  52. "Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
  53. flags.StringToString("docker", nil, "Set the docker endpoint")
  54. flags.StringToString("kubernetes", nil, "Set the kubernetes endpoint")
  55. return cmd
  56. }
  57. func runUpdate(cmd *cobra.Command, name string) error {
  58. s := store.ContextStore(cmd.Context())
  59. dockerContext, err := s.Get(name)
  60. if err == nil && dockerContext != nil {
  61. if dockerContext.Type() != store.DefaultContextType {
  62. return errors.Wrapf(errdefs.ErrNotImplemented, "context update for context type %q not supported", dockerContext.Type())
  63. }
  64. }
  65. mobycli.Exec(cmd.Root())
  66. return nil
  67. }