1
0

context.go 3.5 KB

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