set.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package context
  2. import (
  3. "log"
  4. "github.com/gravitl/netmaker/cli/config"
  5. "github.com/spf13/cobra"
  6. )
  7. var (
  8. endpoint string
  9. username string
  10. password string
  11. masterKey string
  12. )
  13. var contextSetCmd = &cobra.Command{
  14. Use: "set [NAME]",
  15. Args: cobra.ExactArgs(1),
  16. Short: "Create a context or update an existing one",
  17. Long: `Create a context or update an existing one`,
  18. Run: func(cmd *cobra.Command, args []string) {
  19. ctx := config.Context{
  20. Endpoint: endpoint,
  21. Username: username,
  22. Password: password,
  23. MasterKey: masterKey,
  24. }
  25. if ctx.Username == "" && ctx.MasterKey == "" {
  26. cmd.Usage()
  27. log.Fatal("Either username/password or master key is required")
  28. }
  29. config.SetContext(args[0], ctx)
  30. },
  31. }
  32. func init() {
  33. contextSetCmd.Flags().StringVar(&endpoint, "endpoint", "", "Endpoint of the API Server")
  34. contextSetCmd.Flags().StringVar(&username, "username", "", "Username")
  35. contextSetCmd.Flags().StringVar(&password, "password", "", "Password")
  36. contextSetCmd.MarkFlagsRequiredTogether("username", "password")
  37. contextSetCmd.Flags().StringVar(&masterKey, "master_key", "", "Master Key")
  38. rootCmd.AddCommand(contextSetCmd)
  39. }