github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/env-var/env-var.go (about) 1 package env 2 3 import ( 4 "context" 5 6 "github.com/henvic/wedeploycli/cmdflagsfromhost" 7 "github.com/henvic/wedeploycli/command/env-var/internal/commands" 8 cmdenvset "github.com/henvic/wedeploycli/command/env-var/set" 9 cmdenvshow "github.com/henvic/wedeploycli/command/env-var/show" 10 cmdenvunset "github.com/henvic/wedeploycli/command/env-var/unset" 11 "github.com/henvic/wedeploycli/command/internal/we" 12 "github.com/henvic/wedeploycli/fancy" 13 "github.com/henvic/wedeploycli/services" 14 "github.com/spf13/cobra" 15 ) 16 17 type interativeEnvCmd struct { 18 ctx context.Context 19 c commands.Command 20 } 21 22 var ie = &interativeEnvCmd{} 23 24 // EnvCmd controls the envs for a given project 25 var EnvCmd = &cobra.Command{ 26 Use: "env-var", 27 Short: "Show and configure environment variables for services", 28 Long: `Show and configure environment variables for services. You must restart the service afterwards.`, 29 Example: ` lcp env-var (to list and change your environment variables values) 30 lcp env-var set foo bar 31 lcp env-var rm foo`, 32 Args: cobra.NoArgs, 33 PreRunE: ie.preRun, 34 RunE: ie.run, 35 } 36 37 var setupHost = cmdflagsfromhost.SetupHost{ 38 Pattern: cmdflagsfromhost.FullHostPattern, 39 40 Requires: cmdflagsfromhost.Requires{ 41 Auth: true, 42 Project: true, 43 Service: true, 44 }, 45 46 PromptMissingService: true, 47 } 48 49 func (ie *interativeEnvCmd) preRun(cmd *cobra.Command, args []string) error { 50 ie.ctx = context.Background() 51 52 if _, _, err := cmd.Find(args); err != nil { 53 return err 54 } 55 56 return setupHost.Process(context.Background(), we.Context()) 57 } 58 59 func (ie *interativeEnvCmd) run(cmd *cobra.Command, args []string) error { 60 ie.c = commands.Command{ 61 SetupHost: setupHost, 62 ServicesClient: services.New(we.Context()), 63 } 64 65 if err := ie.c.Show(ie.ctx); err != nil { 66 return err 67 } 68 69 var operations = fancy.Options{} 70 71 const ( 72 addOption = "a" 73 unsetOption = "d" 74 ) 75 76 operations.Add(addOption, "Add environment variable") 77 operations.Add(unsetOption, "Delete environment variable") 78 79 op, err := operations.Ask("Select one of the operations for \"" + setupHost.Host() + "\":") 80 81 if err != nil { 82 return err 83 } 84 85 switch op { 86 case unsetOption: 87 err = ie.unsetCmd() 88 default: 89 err = ie.addCmd() 90 } 91 92 if err != nil { 93 return err 94 } 95 96 return ie.c.Show(ie.ctx) 97 } 98 99 func (ie *interativeEnvCmd) addCmd() error { 100 return ie.c.Add(ie.ctx, []string{}) 101 } 102 103 func (ie *interativeEnvCmd) unsetCmd() error { 104 return ie.c.Delete(ie.ctx, []string{}) 105 } 106 107 func init() { 108 setupHost.Init(EnvCmd) 109 EnvCmd.AddCommand(cmdenvshow.Cmd) 110 EnvCmd.AddCommand(cmdenvset.Cmd) 111 EnvCmd.AddCommand(cmdenvunset.Cmd) 112 }