44 "fmt"
55 "io"
66 "os"
7+ "sort"
8+ "strconv"
79
810 "github.com/facebookgo/stackerr"
911 "github.com/spf13/cobra"
@@ -119,6 +121,70 @@ func (c *configureCmd) parserEmail(e *env, args []string) error {
119121 return nil
120122}
121123
124+ func (c * configureCmd ) projectType (e * env , args []string ) error {
125+ config , err := configFromDir (e .Root )
126+ if err != nil {
127+ return err
128+ }
129+ if len (args ) > 1 {
130+ return stackerr .Newf ("Invalid args: %v, only an optional project type argument is expected." , args )
131+ }
132+ validTypes := map [string ]int {"parse" : parseFormat }
133+ invertedTypes := map [int ]string {parseFormat : "parse" }
134+ numKeys := len (validTypes )
135+ var validKeys []string
136+ for key := range validTypes {
137+ validKeys = append (validKeys , key )
138+ }
139+ sort .Strings (validKeys )
140+ var selectionString string
141+ for n , key := range validKeys {
142+ selectionString += fmt .Sprintf ("%d: %s\n " , 1 + n , key )
143+ }
144+
145+ selectedProjectType := - 1
146+ if len (args ) != 0 {
147+ projectType , ok := validTypes [args [0 ]]
148+ if ! ok {
149+ return stackerr .Newf ("Invalid projectType: %v, valid types are: \n %s" , selectionString )
150+ }
151+ selectedProjectType = projectType
152+ }
153+
154+ for i := 0 ; i < 3 ; i ++ {
155+ fmt .Fprintf (e .Out , `Select from the listed project types:
156+ %s
157+ Enter a number between 1 and %d: ` ,
158+ selectionString ,
159+ numKeys ,
160+ )
161+ var selection string
162+ fmt .Fscanf (e .In , "%s\n " , & selection )
163+ num , err := strconv .Atoi (selection )
164+ if err != nil || num < 1 || num > numKeys {
165+ fmt .Fprintf (e .Err , "Invalid selection. Please enter a number between 1 and %d\n " , numKeys )
166+ continue
167+ }
168+ projectType , ok := validTypes [validKeys [num - 1 ]]
169+ if ! ok {
170+ return stackerr .Newf ("Invalid projectType: %v, valid types are: \n %s" , selectionString )
171+ }
172+ selectedProjectType = projectType
173+ break
174+ }
175+ if selectedProjectType == - 1 {
176+ return stackerr .Newf ("Could not make a selection. Please try again." )
177+ }
178+
179+ config .getProjectConfig ().Type = selectedProjectType
180+ if err := storeProjectConfig (e , config ); err != nil {
181+ fmt .Fprintln (e .Err , "Could not save selected project type to project config" )
182+ return err
183+ }
184+ fmt .Fprintf (e .Out , "Successfully set project type to: %v\n " , invertedTypes [selectedProjectType ])
185+ return nil
186+ }
187+
122188func newConfigureCmd (e * env ) * cobra.Command {
123189 var c configureCmd
124190
@@ -143,12 +209,21 @@ func newConfigureCmd(e *env) *cobra.Command {
143209 cmd .AddCommand (keyCmd )
144210
145211 emailCmd := & cobra.Command {
146- Use : "email" ,
147- Short : "Configures the parser email for this project" ,
148- Long : "Configures the parser email for current project." ,
149- Run : runWithArgs (e , c .parserEmail ),
212+ Use : "email" ,
213+ Short : "Configures the parser email for this project" ,
214+ Long : "Configures the parser email for current project." ,
215+ Run : runWithArgs (e , c .parserEmail ),
216+ Aliases : []string {"user" },
150217 }
151218 cmd .AddCommand (emailCmd )
152219
220+ projectCmd := & cobra.Command {
221+ Use : "project" ,
222+ Short : "Set the project type to one among listed options" ,
223+ Long : "Set the project type to one among listed options. For instance, 'parse'" ,
224+ Run : runWithArgs (e , c .projectType ),
225+ }
226+ cmd .AddCommand (projectCmd )
227+
153228 return cmd
154229}
0 commit comments