Skip to content

Commit

Permalink
feat: add interactive terminal input for generating module
Browse files Browse the repository at this point in the history
  • Loading branch information
mukezhz committed Dec 3, 2023
1 parent ef32de8 commit 7717ce6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var rootCmd = &cobra.Command{
var newModuleCmd = &cobra.Command{
Use: "gen features [name]",
Short: "Create a new features",
Args: cobra.ExactArgs(2),
Args: cobra.MaximumNArgs(2),
Run: createModule,
}

Expand Down Expand Up @@ -67,8 +67,22 @@ func createModule(_ *cobra.Command, args []string) {
if err != nil {
panic(err)
}
var moduleName string
if len(args) == 1 {
questions := []terminal.ProjectQuestion{
terminal.NewShortQuestion(constant.ModueleNameKEY, constant.ModueleNameKEY+" *", "Enter Module Name:"),
}
terminal.StartInteractiveTerminal(questions)

moduleName := args[1]
for _, q := range questions {
switch q.Key {
case constant.ModueleNameKEY:
moduleName = q.Answer
}
}
} else {
moduleName = args[1]
}
if !utility.CheckGolangIdentifier(moduleName) {
color.Redln("Error: module name is invalid")
return
Expand Down
1 change: 1 addition & 0 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const (
ProjectDescriptionKEY = "projectDescription"
GoVersionKEY = "goVersion"
DirectoryKEY = "directory"
ModueleNameKEY = "moduleName"
)

const (
Expand Down
4 changes: 1 addition & 3 deletions pkg/utility/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func CheckVersion(goVersion string) string {
func GetModuleNameFromGoModFile() (model.GoMod, error) {
file, err := os.Open("go.mod")
goMod := model.GoMod{}

if err != nil {
return goMod, err
}
Expand Down Expand Up @@ -212,6 +211,5 @@ func GetModuleNameFromGoModFile() (model.GoMod, error) {
return goMod, err
}

abs, _ := filepath.Abs("go.mod")
return goMod, fmt.Errorf("module directive not found in %s", abs)
return goMod, nil
}

0 comments on commit 7717ce6

Please sign in to comment.