Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit 2b9cc4c

Browse files
author
Pavan Kumar
committed
[configure] allow configuring project type
* allow user to configure the project type for given code
1 parent 919c4f5 commit 2b9cc4c

File tree

2 files changed

+111
-4
lines changed

2 files changed

+111
-4
lines changed

configure_cmd.go

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
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+
122188
func 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
}

configure_cmd_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,35 @@ func TestParserEmail(t *testing.T) {
118118
ensure.Err(t, c.parserEmail(h.env, nil), regexp.MustCompile("Invalid args:"))
119119
ensure.Err(t, c.parserEmail(h.env, []string{"a", "b"}), regexp.MustCompile("Invalid args:"))
120120
}
121+
122+
func TestProjectType(t *testing.T) {
123+
t.Parallel()
124+
h := newHarness(t)
125+
defer h.Stop()
126+
127+
h.makeEmptyRoot()
128+
ensure.Nil(t, (&newCmd{}).cloneSampleCloudCode(h.env, &app{Name: "test"}, false, false))
129+
130+
c := &configureCmd{}
131+
err := c.projectType(h.env, []string{"1", "2"})
132+
ensure.Err(t, err, regexp.MustCompile("only an optional project type argument is expected"))
133+
134+
h.env.In = ioutil.NopCloser(strings.NewReader("invalid\n"))
135+
err = c.projectType(h.env, nil)
136+
ensure.StringContains(t, h.Err.String(), "Invalid selection. Please enter a number")
137+
ensure.Err(t, err, regexp.MustCompile("Could not make a selection. Please try again."))
138+
h.Err.Reset()
139+
h.Out.Reset()
140+
141+
h.env.In = ioutil.NopCloser(strings.NewReader("0\n"))
142+
err = c.projectType(h.env, nil)
143+
ensure.StringContains(t, h.Err.String(), "Please enter a number between 1 and")
144+
ensure.Err(t, err, regexp.MustCompile("Could not make a selection. Please try again."))
145+
h.Err.Reset()
146+
h.Out.Reset()
147+
148+
h.env.In = ioutil.NopCloser(strings.NewReader("1\n"))
149+
err = c.projectType(h.env, nil)
150+
ensure.StringContains(t, h.Out.String(), "Successfully set project type to: parse")
151+
ensure.Nil(t, err)
152+
}

0 commit comments

Comments
 (0)