context.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/spf13/cobra"
  31. "github.com/docker/api/context/store"
  32. )
  33. // ContextCommand manages contexts
  34. func ContextCommand() *cobra.Command {
  35. cmd := &cobra.Command{
  36. Use: "context",
  37. Short: "Manage contexts",
  38. }
  39. cmd.AddCommand(
  40. createCommand(),
  41. listCommand(),
  42. )
  43. return cmd
  44. }
  45. type createOpts struct {
  46. description string
  47. aciLocation string
  48. aciSubscriptionID string
  49. aciResourceGroup string
  50. }
  51. func createCommand() *cobra.Command {
  52. var opts createOpts
  53. cmd := &cobra.Command{
  54. Use: "create CONTEXT BACKEND [OPTIONS]",
  55. Short: "Create a context",
  56. Args: cobra.ExactArgs(2),
  57. RunE: func(cmd *cobra.Command, args []string) error {
  58. return runCreate(cmd.Context(), opts, args[0], args[1])
  59. },
  60. }
  61. cmd.Flags().StringVar(&opts.description, "description", "", "Description of the context")
  62. cmd.Flags().StringVar(&opts.aciLocation, "aci-location", "eastus", "Location")
  63. cmd.Flags().StringVar(&opts.aciSubscriptionID, "aci-subscription-id", "", "Location")
  64. cmd.Flags().StringVar(&opts.aciResourceGroup, "aci-resource-group", "", "Resource group")
  65. return cmd
  66. }
  67. func listCommand() *cobra.Command {
  68. cmd := &cobra.Command{
  69. Use: "list",
  70. Aliases: []string{"ls"},
  71. Args: cobra.NoArgs,
  72. RunE: func(cmd *cobra.Command, args []string) error {
  73. return runList(cmd.Context())
  74. },
  75. }
  76. return cmd
  77. }
  78. func runCreate(ctx context.Context, opts createOpts, name string, contextType string) error {
  79. switch contextType {
  80. case "aci":
  81. return createACIContext(ctx, name, opts)
  82. default:
  83. s := store.ContextStore(ctx)
  84. return s.Create(name, store.TypedContext{
  85. Type: contextType,
  86. Description: opts.description,
  87. })
  88. }
  89. }
  90. func createACIContext(ctx context.Context, name string, opts createOpts) error {
  91. s := store.ContextStore(ctx)
  92. return s.Create(name, store.TypedContext{
  93. Type: "aci",
  94. Description: opts.description,
  95. Data: store.AciContext{
  96. SubscriptionID: opts.aciSubscriptionID,
  97. Location: opts.aciLocation,
  98. ResourceGroup: opts.aciResourceGroup,
  99. },
  100. })
  101. }
  102. func runList(ctx context.Context) error {
  103. s := store.ContextStore(ctx)
  104. contexts, err := s.List()
  105. if err != nil {
  106. return err
  107. }
  108. w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
  109. fmt.Fprintln(w, "NAME\tDESCRIPTION\tTYPE")
  110. format := "%s\t%s\t%s\n"
  111. for _, c := range contexts {
  112. fmt.Fprintf(w, format, c.Name, c.Metadata.Description, c.Metadata.Type)
  113. }
  114. return w.Flush()
  115. }