context.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2020 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package cmd
  25. import (
  26. "context"
  27. "fmt"
  28. "os"
  29. "text/tabwriter"
  30. "github.com/hashicorp/go-multierror"
  31. "github.com/spf13/cobra"
  32. cliconfig "github.com/docker/api/cli/config"
  33. cliopts "github.com/docker/api/cli/options"
  34. "github.com/docker/api/context/store"
  35. )
  36. // ContextCommand manages contexts
  37. func ContextCommand(opts *cliopts.GlobalOpts) *cobra.Command {
  38. cmd := &cobra.Command{
  39. Use: "context",
  40. Short: "Manage contexts",
  41. }
  42. cmd.AddCommand(
  43. createCommand(),
  44. listCommand(),
  45. removeCommand(),
  46. useCommand(opts),
  47. )
  48. return cmd
  49. }
  50. type createOpts struct {
  51. description string
  52. aciLocation string
  53. aciSubscriptionID string
  54. aciResourceGroup string
  55. }
  56. func createCommand() *cobra.Command {
  57. var opts createOpts
  58. cmd := &cobra.Command{
  59. Use: "create CONTEXT BACKEND [OPTIONS]",
  60. Short: "Create a context",
  61. Args: cobra.ExactArgs(2),
  62. RunE: func(cmd *cobra.Command, args []string) error {
  63. return runCreate(cmd.Context(), opts, args[0], args[1])
  64. },
  65. }
  66. cmd.Flags().StringVar(&opts.description, "description", "", "Description of the context")
  67. cmd.Flags().StringVar(&opts.aciLocation, "aci-location", "eastus", "Location")
  68. cmd.Flags().StringVar(&opts.aciSubscriptionID, "aci-subscription-id", "", "Location")
  69. cmd.Flags().StringVar(&opts.aciResourceGroup, "aci-resource-group", "", "Resource group")
  70. return cmd
  71. }
  72. func listCommand() *cobra.Command {
  73. cmd := &cobra.Command{
  74. Use: "list",
  75. Short: "List available contexts",
  76. Aliases: []string{"ls"},
  77. Args: cobra.NoArgs,
  78. RunE: func(cmd *cobra.Command, args []string) error {
  79. return runList(cmd.Context())
  80. },
  81. }
  82. return cmd
  83. }
  84. func removeCommand() *cobra.Command {
  85. return &cobra.Command{
  86. Use: "rm CONTEXT [CONTEXT...]",
  87. Short: "Remove one or more contexts",
  88. Aliases: []string{"remove"},
  89. Args: cobra.MinimumNArgs(1),
  90. RunE: func(cmd *cobra.Command, args []string) error {
  91. return runRemove(cmd.Context(), args)
  92. },
  93. }
  94. }
  95. func useCommand(opts *cliopts.GlobalOpts) *cobra.Command {
  96. return &cobra.Command{
  97. Use: "use CONTEXT",
  98. Short: "Set the default context",
  99. Args: cobra.ExactArgs(1),
  100. RunE: func(cmd *cobra.Command, args []string) error {
  101. return runUse(cmd.Context(), opts.Config, args[0])
  102. },
  103. }
  104. }
  105. func runCreate(ctx context.Context, opts createOpts, name string, contextType string) error {
  106. switch contextType {
  107. case "aci":
  108. return createACIContext(ctx, name, opts)
  109. default:
  110. s := store.ContextStore(ctx)
  111. return s.Create(name, store.TypedContext{
  112. Type: contextType,
  113. Description: opts.description,
  114. })
  115. }
  116. }
  117. func createACIContext(ctx context.Context, name string, opts createOpts) error {
  118. s := store.ContextStore(ctx)
  119. return s.Create(name, store.TypedContext{
  120. Type: "aci",
  121. Description: opts.description,
  122. Data: store.AciContext{
  123. SubscriptionID: opts.aciSubscriptionID,
  124. Location: opts.aciLocation,
  125. ResourceGroup: opts.aciResourceGroup,
  126. },
  127. })
  128. }
  129. func runList(ctx context.Context) error {
  130. s := store.ContextStore(ctx)
  131. contexts, err := s.List()
  132. if err != nil {
  133. return err
  134. }
  135. w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
  136. fmt.Fprintln(w, "NAME\tDESCRIPTION\tTYPE")
  137. format := "%s\t%s\t%s\n"
  138. for _, c := range contexts {
  139. fmt.Fprintf(w, format, c.Name, c.Metadata.Description, c.Metadata.Type)
  140. }
  141. return w.Flush()
  142. }
  143. func runRemove(ctx context.Context, args []string) error {
  144. s := store.ContextStore(ctx)
  145. var errs *multierror.Error
  146. for _, n := range args {
  147. if err := s.Remove(n); err != nil {
  148. errs = multierror.Append(errs, err)
  149. } else {
  150. fmt.Println(n)
  151. }
  152. }
  153. return errs.ErrorOrNil()
  154. }
  155. func runUse(ctx context.Context, configDir string, name string) error {
  156. s := store.ContextStore(ctx)
  157. // Match behavior of existing CLI
  158. if name != store.DefaultContextName {
  159. if _, err := s.Get(name, nil); err != nil {
  160. return err
  161. }
  162. }
  163. if err := cliconfig.WriteCurrentContext(configDir, name); err != nil {
  164. return err
  165. }
  166. fmt.Println(name)
  167. return nil
  168. }