create.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 context
  25. import (
  26. "context"
  27. "github.com/spf13/cobra"
  28. "github.com/docker/api/context/store"
  29. )
  30. type createOpts struct {
  31. description string
  32. aciLocation string
  33. aciSubscriptionID string
  34. aciResourceGroup string
  35. }
  36. func createCommand() *cobra.Command {
  37. var opts createOpts
  38. cmd := &cobra.Command{
  39. Use: "create CONTEXT BACKEND [OPTIONS]",
  40. Short: "Create a context",
  41. Args: cobra.ExactArgs(2),
  42. RunE: func(cmd *cobra.Command, args []string) error {
  43. return runCreate(cmd.Context(), opts, args[0], args[1])
  44. },
  45. }
  46. cmd.Flags().StringVar(&opts.description, "description", "", "Description of the context")
  47. cmd.Flags().StringVar(&opts.aciLocation, "aci-location", "eastus", "Location")
  48. cmd.Flags().StringVar(&opts.aciSubscriptionID, "aci-subscription-id", "", "Location")
  49. cmd.Flags().StringVar(&opts.aciResourceGroup, "aci-resource-group", "", "Resource group")
  50. return cmd
  51. }
  52. func runCreate(ctx context.Context, opts createOpts, name string, contextType string) error {
  53. switch contextType {
  54. case "aci":
  55. return createACIContext(ctx, name, opts)
  56. default:
  57. s := store.ContextStore(ctx)
  58. // TODO: we need to implement different contexts for known backends
  59. return s.Create(name, contextType, opts.description, store.ExampleContext{})
  60. }
  61. }