github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/edit/edit_config.go (about) 1 package edit 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 8 9 "github.com/jenkins-x/jx-logging/pkg/log" 10 "github.com/olli-ai/jx/v2/pkg/auth" 11 "github.com/spf13/cobra" 12 13 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 14 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 15 "github.com/olli-ai/jx/v2/pkg/config" 16 "github.com/olli-ai/jx/v2/pkg/util" 17 ) 18 19 const ( 20 chatKind = "chat" 21 issueKind = "issues" 22 wikiKind = "wiki" 23 ) 24 25 var ( 26 editConfigLong = templates.LongDesc(` 27 Edits the project configuration 28 `) 29 30 editConfigExample = templates.Examples(` 31 # Edit the project configuration for the current directory 32 jx edit config 33 `) 34 35 configKinds = []string{ 36 chatKind, 37 issueKind, 38 wikiKind, 39 } 40 ) 41 42 // EditConfigOptions the options for the create spring command 43 type EditConfigOptions struct { 44 EditOptions 45 46 Dir string 47 Kind string 48 49 IssuesAuthConfigSvc auth.ConfigService 50 ChatAuthConfigSvc auth.ConfigService 51 } 52 53 // NewCmdEditConfig creates a command object for the "create" command 54 func NewCmdEditConfig(commonOpts *opts.CommonOptions) *cobra.Command { 55 options := &EditConfigOptions{ 56 EditOptions: EditOptions{ 57 CommonOptions: commonOpts, 58 }, 59 } 60 61 cmd := &cobra.Command{ 62 Use: "config", 63 Short: "Edits the project configuration", 64 Aliases: []string{"project"}, 65 Long: editConfigLong, 66 Example: editConfigExample, 67 Run: func(cmd *cobra.Command, args []string) { 68 options.Cmd = cmd 69 options.Args = args 70 err := options.Run() 71 helper.CheckErr(err) 72 }, 73 } 74 cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The root project directory. Defaults to the current dir") 75 cmd.Flags().StringVarP(&options.Kind, "kind", "k", "", "The kind of configuration to edit root project directory. Possible values "+strings.Join(configKinds, ", ")) 76 77 return cmd 78 } 79 80 // Run implements the command 81 func (o *EditConfigOptions) Run() error { 82 pc, fileName, err := config.LoadProjectConfig(o.Dir) 83 if err != nil { 84 return err 85 } 86 o.IssuesAuthConfigSvc, err = o.CreateIssueTrackerAuthConfigService("") 87 if err != nil { 88 return err 89 } 90 o.ChatAuthConfigSvc, err = o.CreateChatAuthConfigService("") 91 if err != nil { 92 return err 93 } 94 95 kind := o.Kind 96 if kind == "" && !o.BatchMode { 97 kind, err = util.PickRequiredNameWithDefault(configKinds, "Which configuration do you want to edit", issueKind, "", o.GetIOFileHandles()) 98 if err != nil { 99 return err 100 } 101 } 102 if kind == "" { 103 return fmt.Errorf("No kind option!") 104 } 105 if util.StringArrayIndex(configKinds, kind) < 0 { 106 return util.InvalidOption("kind", kind, configKinds) 107 } 108 modified := false 109 switch kind { 110 case chatKind: 111 modified, err = o.EditChat(pc) 112 case issueKind: 113 modified, err = o.EditIssueTracker(pc) 114 default: 115 return fmt.Errorf("Editing %s is not yet supported!", kind) 116 } 117 if err != nil { 118 return err 119 } 120 if modified { 121 err = pc.SaveConfig(fileName) 122 if err != nil { 123 return err 124 } 125 log.Logger().Infof("Saved project configuration %s", util.ColorInfo(fileName)) 126 } 127 return nil 128 } 129 130 func (o *EditConfigOptions) EditIssueTracker(pc *config.ProjectConfig) (bool, error) { 131 answer := false 132 if pc.IssueTracker == nil { 133 pc.IssueTracker = &config.IssueTrackerConfig{} 134 answer = true 135 } 136 it := pc.IssueTracker 137 138 config := o.IssuesAuthConfigSvc.Config() 139 if len(config.Servers) == 0 { 140 return answer, fmt.Errorf("No issue tracker servers available. Please add one via: jx create tracker server") 141 } 142 server, err := config.PickServer("Issue tracker service", o.BatchMode, o.GetIOFileHandles()) 143 if err != nil { 144 return answer, err 145 } 146 if server == nil || server.URL == "" { 147 return answer, fmt.Errorf("No issue tracker server URL found!") 148 } 149 it.URL = server.URL 150 if server.Kind != "" { 151 it.Kind = server.Kind 152 } 153 answer = true 154 155 it.Project, err = util.PickValue("Issue tracker project name: ", it.Project, true, "", o.GetIOFileHandles()) 156 if err != nil { 157 return answer, err 158 } 159 return answer, nil 160 } 161 162 func (o *EditConfigOptions) EditChat(pc *config.ProjectConfig) (bool, error) { 163 answer := false 164 if pc.Chat == nil { 165 pc.Chat = &config.ChatConfig{} 166 answer = true 167 } 168 it := pc.Chat 169 170 config := o.ChatAuthConfigSvc.Config() 171 if len(config.Servers) == 0 { 172 return answer, fmt.Errorf("No chat servers available. Please add one via: jx create chat server") 173 } 174 server, err := config.PickServer("Chat service", o.BatchMode, o.GetIOFileHandles()) 175 if err != nil { 176 return answer, err 177 } 178 if server == nil || server.URL == "" { 179 return answer, fmt.Errorf("No chat server URL found!") 180 } 181 it.URL = server.URL 182 if server.Kind != "" { 183 it.Kind = server.Kind 184 } 185 answer = true 186 187 it.DeveloperChannel, err = util.PickValue("Developer channel: ", it.DeveloperChannel, false, "", o.GetIOFileHandles()) 188 if err != nil { 189 return answer, err 190 } 191 it.UserChannel, err = util.PickValue("User channel: ", it.UserChannel, false, "", o.GetIOFileHandles()) 192 if err != nil { 193 return answer, err 194 } 195 return answer, nil 196 }