use.go 862 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package context
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/spf13/cobra"
  6. cliconfig "github.com/docker/api/cli/config"
  7. cliopts "github.com/docker/api/cli/options"
  8. "github.com/docker/api/context/store"
  9. )
  10. func useCommand(opts *cliopts.GlobalOpts) *cobra.Command {
  11. return &cobra.Command{
  12. Use: "use CONTEXT",
  13. Short: "Set the default context",
  14. Args: cobra.ExactArgs(1),
  15. RunE: func(cmd *cobra.Command, args []string) error {
  16. return runUse(cmd.Context(), opts.Config, args[0])
  17. },
  18. }
  19. }
  20. func runUse(ctx context.Context, configDir string, name string) error {
  21. s := store.ContextStore(ctx)
  22. // Match behavior of existing CLI
  23. if name != store.DefaultContextName {
  24. if _, err := s.Get(name, nil); err != nil {
  25. return err
  26. }
  27. }
  28. if err := cliconfig.WriteCurrentContext(configDir, name); err != nil {
  29. return err
  30. }
  31. fmt.Println(name)
  32. return nil
  33. }