github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/domain/domain.go (about) 1 package domain 2 3 import ( 4 "context" 5 6 "github.com/henvic/wedeploycli/cmdflagsfromhost" 7 cmddomainadd "github.com/henvic/wedeploycli/command/domain/add" 8 "github.com/henvic/wedeploycli/command/domain/internal/commands" 9 cmddomainremove "github.com/henvic/wedeploycli/command/domain/remove" 10 cmddomainshow "github.com/henvic/wedeploycli/command/domain/show" 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 interativeDomainCmd struct { 18 ctx context.Context 19 c commands.Command 20 } 21 22 var idc = &interativeDomainCmd{} 23 24 // DomainCmd controls the domains for a given project 25 var DomainCmd = &cobra.Command{ 26 Use: "domain", 27 Short: "Show and configure domain names for services", 28 Example: ` lcp domain (to list domains) 29 lcp domain add foo.com 30 lcp domain rm foo.com`, 31 Args: cobra.NoArgs, 32 PreRunE: idc.preRun, 33 RunE: idc.run, 34 } 35 36 var setupHost = cmdflagsfromhost.SetupHost{ 37 Pattern: cmdflagsfromhost.FullHostPattern, 38 39 Requires: cmdflagsfromhost.Requires{ 40 Auth: true, 41 Project: true, 42 Service: true, 43 }, 44 45 PromptMissingService: true, 46 } 47 48 func (idc *interativeDomainCmd) preRun(cmd *cobra.Command, args []string) error { 49 idc.ctx = context.Background() 50 51 // get nice error message 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 (idc *interativeDomainCmd) run(cmd *cobra.Command, args []string) error { 60 idc.c = commands.Command{ 61 SetupHost: setupHost, 62 ServicesClient: services.New(we.Context()), 63 } 64 65 if err := idc.c.Show(idc.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 custom domain") 77 operations.Add(unsetOption, "Delete custom domain") 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 = idc.deleteCmd() 88 default: 89 err = idc.addCmd() 90 } 91 92 if err != nil { 93 return err 94 } 95 96 return idc.c.Show(idc.ctx) 97 } 98 99 func (idc *interativeDomainCmd) addCmd() error { 100 return idc.c.Add(idc.ctx, []string{}) 101 } 102 103 func (idc *interativeDomainCmd) deleteCmd() error { 104 return idc.c.Delete(idc.ctx, []string{}) 105 } 106 107 func init() { 108 setupHost.Init(DomainCmd) 109 DomainCmd.AddCommand(cmddomainshow.Cmd) 110 DomainCmd.AddCommand(cmddomainadd.Cmd) 111 DomainCmd.AddCommand(cmddomainremove.Cmd) 112 }