From 772aa234ef79d34cebebcf05d133a8b348c2199f Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 11:36:36 +0100 Subject: [PATCH 1/8] Initial implementation for framework flag shell completion --- cmd/create.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/create.go b/cmd/create.go index f9f66aa2..326e86b2 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -36,11 +36,49 @@ var ( allowedProjectTypes = []string{"chi", "gin", "fiber", "gorilla/mux", "httprouter", "standard-library", "echo"} ) +type framework string + +const ( + Chi framework = "chi" + Gin framework = "gin" + fiber framework = "fiber" + GorillaMux framework = "gorilla/mux" + HttpRouter framework = "httprouter" + StandardLibrary framework = "standard-library" + Echo framework = "echo" +) + +func (f *framework) String() string { + return string(*f) +} + +func (f *framework) Type() string { + return "framework" +} + +func (f *framework) Set(value string) error { + switch value { + case "chi", "gin", "fiber", "gorilla/mux", "httprouter", "standard-library", "echo": + *f = framework(value) + return nil + default: + return fmt.Errorf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", ")) + } +} + func init() { + var frameworkFlag = Chi rootCmd.AddCommand(createCmd) createCmd.Flags().StringP("name", "n", "", "Name of project to create") - createCmd.Flags().StringP("framework", "f", "", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", "))) + // createCmd.Flags().StringP("framework", "f", "", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", "))) + createCmd.Flags().Var(&frameworkFlag, "framework", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", "))) + + createCmd.RegisterFlagCompletionFunc("framework", myFrameworkCompletion) +} + +func myFrameworkCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return allowedProjectTypes, cobra.ShellCompDirectiveDefault } // createCmd defines the "create" command for the CLI From 0298b9702bfe8b838948ea291cdc580059e8d28d Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 12:16:44 +0100 Subject: [PATCH 2/8] Created separate file for framekwork flag and cleaned up original file --- cmd/create.go | 44 ++++++-------------------------------------- cmd/frameworkFlag.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 cmd/frameworkFlag.go diff --git a/cmd/create.go b/cmd/create.go index 326e86b2..95060f01 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -36,49 +36,17 @@ var ( allowedProjectTypes = []string{"chi", "gin", "fiber", "gorilla/mux", "httprouter", "standard-library", "echo"} ) -type framework string - -const ( - Chi framework = "chi" - Gin framework = "gin" - fiber framework = "fiber" - GorillaMux framework = "gorilla/mux" - HttpRouter framework = "httprouter" - StandardLibrary framework = "standard-library" - Echo framework = "echo" -) - -func (f *framework) String() string { - return string(*f) -} - -func (f *framework) Type() string { - return "framework" -} - -func (f *framework) Set(value string) error { - switch value { - case "chi", "gin", "fiber", "gorilla/mux", "httprouter", "standard-library", "echo": - *f = framework(value) - return nil - default: - return fmt.Errorf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", ")) - } -} - func init() { - var frameworkFlag = Chi + var frameworkFlag framework + rootCmd.AddCommand(createCmd) createCmd.Flags().StringP("name", "n", "", "Name of project to create") - // createCmd.Flags().StringP("framework", "f", "", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", "))) - createCmd.Flags().Var(&frameworkFlag, "framework", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", "))) + createCmd.Flags().VarP(&frameworkFlag, "framework", "f", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", "))) - createCmd.RegisterFlagCompletionFunc("framework", myFrameworkCompletion) -} - -func myFrameworkCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return allowedProjectTypes, cobra.ShellCompDirectiveDefault + if err := createCmd.RegisterFlagCompletionFunc("framework", frameworkCompletion); err != nil { + log.Fatal(err) + } } // createCmd defines the "create" command for the CLI diff --git a/cmd/frameworkFlag.go b/cmd/frameworkFlag.go new file mode 100644 index 00000000..d9d36dc1 --- /dev/null +++ b/cmd/frameworkFlag.go @@ -0,0 +1,42 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" +) + +type framework string + +const ( + Chi framework = "chi" + Gin framework = "gin" + fiber framework = "fiber" + GorillaMux framework = "gorilla/mux" + HttpRouter framework = "httprouter" + StandardLibrary framework = "standard-library" + Echo framework = "echo" +) + +func (f *framework) String() string { + return string(*f) +} + +func (f *framework) Type() string { + return "framework" +} + +func (f *framework) Set(value string) error { + switch value { + case "chi", "gin", "fiber", "gorilla/mux", "httprouter", "standard-library", "echo": + *f = framework(value) + return nil + default: + return fmt.Errorf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", ")) + } +} + +func frameworkCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return allowedProjectTypes, cobra.ShellCompDirectiveDefault +} From 09fa0d7548b52f8cc56fc05960017e688e715612 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 14:13:08 +0100 Subject: [PATCH 3/8] Replaced all hardcoded framework strings to Framework constants --- cmd/create.go | 18 +++++++------- cmd/frameworkFlag.go | 42 ------------------------------- cmd/frameworks/frameworkFlag.go | 44 +++++++++++++++++++++++++++++++++ cmd/program/program.go | 18 ++++++++------ cmd/steps/steps.go | 7 +++--- 5 files changed, 67 insertions(+), 62 deletions(-) delete mode 100644 cmd/frameworkFlag.go create mode 100644 cmd/frameworks/frameworkFlag.go diff --git a/cmd/create.go b/cmd/create.go index 95060f01..9c4c3f21 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -8,6 +8,7 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/melkeydev/go-blueprint/cmd/frameworks" "github.com/melkeydev/go-blueprint/cmd/program" "github.com/melkeydev/go-blueprint/cmd/steps" "github.com/melkeydev/go-blueprint/cmd/ui/multiInput" @@ -33,18 +34,17 @@ var ( logoStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#01FAC6")).Bold(true) tipMsgStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("190")).Italic(true) endingMsgStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("170")).Bold(true) - allowedProjectTypes = []string{"chi", "gin", "fiber", "gorilla/mux", "httprouter", "standard-library", "echo"} ) func init() { - var frameworkFlag framework + var frameworkFlag frameworks.Framework rootCmd.AddCommand(createCmd) createCmd.Flags().StringP("name", "n", "", "Name of project to create") - createCmd.Flags().VarP(&frameworkFlag, "framework", "f", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", "))) + createCmd.Flags().VarP(&frameworkFlag, "framework", "f", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(frameworks.AllowedProjectTypes, ", "))) - if err := createCmd.RegisterFlagCompletionFunc("framework", frameworkCompletion); err != nil { + if err := createCmd.RegisterFlagCompletionFunc("framework", frameworks.FrameworkCompletion); err != nil { log.Fatal(err) } } @@ -68,16 +68,16 @@ var createCmd = &cobra.Command{ flagFramework := cmd.Flag("framework").Value.String() if flagFramework != "" { - isValid := isValidProjectType(flagFramework, allowedProjectTypes) + isValid := isValidProjectType(flagFramework, frameworks.AllowedProjectTypes) if !isValid { - cobra.CheckErr(fmt.Errorf("Project type '%s' is not valid. Valid types are: %s", flagFramework, strings.Join(allowedProjectTypes, ", "))) + cobra.CheckErr(fmt.Errorf("Project type '%s' is not valid. Valid types are: %s", flagFramework, strings.Join(frameworks.AllowedProjectTypes, ", "))) } } project := &program.Project{ FrameworkMap: make(map[string]program.Framework), ProjectName: flagName, - ProjectType: strings.ReplaceAll(flagFramework, "-", " "), + ProjectType: flagFramework, } steps := steps.InitSteps(&options) @@ -92,7 +92,7 @@ var createCmd = &cobra.Command{ project.ExitCLI(tprogram) project.ProjectName = options.ProjectName.Output - cmd.Flag("name").Value.Set(project.ProjectName) + _ = cmd.Flag("name").Value.Set(project.ProjectName) } if project.ProjectType == "" { @@ -108,7 +108,7 @@ var createCmd = &cobra.Command{ } project.ProjectType = strings.ToLower(options.ProjectType) - cmd.Flag("framework").Value.Set(project.ProjectType) + _ = cmd.Flag("framework").Value.Set(project.ProjectType) } currentWorkingDir, err := os.Getwd() diff --git a/cmd/frameworkFlag.go b/cmd/frameworkFlag.go deleted file mode 100644 index d9d36dc1..00000000 --- a/cmd/frameworkFlag.go +++ /dev/null @@ -1,42 +0,0 @@ -package cmd - -import ( - "fmt" - "strings" - - "github.com/spf13/cobra" -) - -type framework string - -const ( - Chi framework = "chi" - Gin framework = "gin" - fiber framework = "fiber" - GorillaMux framework = "gorilla/mux" - HttpRouter framework = "httprouter" - StandardLibrary framework = "standard-library" - Echo framework = "echo" -) - -func (f *framework) String() string { - return string(*f) -} - -func (f *framework) Type() string { - return "framework" -} - -func (f *framework) Set(value string) error { - switch value { - case "chi", "gin", "fiber", "gorilla/mux", "httprouter", "standard-library", "echo": - *f = framework(value) - return nil - default: - return fmt.Errorf("Framework to use. Allowed values: %s", strings.Join(allowedProjectTypes, ", ")) - } -} - -func frameworkCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return allowedProjectTypes, cobra.ShellCompDirectiveDefault -} diff --git a/cmd/frameworks/frameworkFlag.go b/cmd/frameworks/frameworkFlag.go new file mode 100644 index 00000000..6e0987f5 --- /dev/null +++ b/cmd/frameworks/frameworkFlag.go @@ -0,0 +1,44 @@ +package frameworks + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" +) + +type Framework string + +const ( + Chi Framework = "chi" + Gin Framework = "gin" + Fiber Framework = "fiber" + GorillaMux Framework = "gorilla/mux" + HttpRouter Framework = "httprouter" + StandardLibrary Framework = "standard-library" + Echo Framework = "echo" +) + +var AllowedProjectTypes = []string{string(Chi), string(Gin), string(Fiber), string(GorillaMux), string(HttpRouter), string(StandardLibrary), string(Echo)} + +func (f Framework) String() string { + return string(f) +} + +func (f *Framework) Type() string { + return "Framework" +} + +func (f *Framework) Set(value string) error { + switch value { + case Chi.String(), Gin.String(), Fiber.String(), GorillaMux.String(), HttpRouter.String(), StandardLibrary.String(), Echo.String(): + *f = Framework(value) + return nil + default: + return fmt.Errorf("Framework to use. Allowed values: %s", strings.Join(AllowedProjectTypes, ", ")) + } +} + +func FrameworkCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return AllowedProjectTypes, cobra.ShellCompDirectiveDefault +} diff --git a/cmd/program/program.go b/cmd/program/program.go index 414caded..a43693a6 100644 --- a/cmd/program/program.go +++ b/cmd/program/program.go @@ -8,7 +8,9 @@ import ( "log" "os" "strings" + tea "github.com/charmbracelet/bubbletea" + "github.com/melkeydev/go-blueprint/cmd/frameworks" tpl "github.com/melkeydev/go-blueprint/cmd/template" "github.com/melkeydev/go-blueprint/cmd/utils" "github.com/spf13/cobra" @@ -66,37 +68,37 @@ func (p *Project) ExitCLI(tprogram *tea.Program) { // createFrameWorkMap adds the current supported // Frameworks into a Project's FrameworkMap func (p *Project) createFrameworkMap() { - p.FrameworkMap["chi"] = Framework{ + p.FrameworkMap[frameworks.Chi.String()] = Framework{ packageName: chiPackage, templater: tpl.ChiTemplates{}, } - p.FrameworkMap["standard library"] = Framework{ + p.FrameworkMap[frameworks.StandardLibrary.String()] = Framework{ packageName: []string{}, templater: tpl.StandardLibTemplate{}, } - p.FrameworkMap["gin"] = Framework{ + p.FrameworkMap[frameworks.Gin.String()] = Framework{ packageName: ginPackage, templater: tpl.GinTemplates{}, } - p.FrameworkMap["fiber"] = Framework{ + p.FrameworkMap[frameworks.Fiber.String()] = Framework{ packageName: fiberPackage, templater: tpl.FiberTemplates{}, } - p.FrameworkMap["gorilla/mux"] = Framework{ + p.FrameworkMap[frameworks.GorillaMux.String()] = Framework{ packageName: gorillaPackage, templater: tpl.GorillaTemplates{}, } - p.FrameworkMap["httprouter"] = Framework{ + p.FrameworkMap[frameworks.HttpRouter.String()] = Framework{ packageName: routerPackage, templater: tpl.RouterTemplates{}, } - p.FrameworkMap["echo"] = Framework{ + p.FrameworkMap[frameworks.Echo.String()] = Framework{ packageName: echoPackage, templater: tpl.EchoTemplates{}, } @@ -138,7 +140,7 @@ func (p *Project) CreateMainFile() error { } // Install the correct package for the selected framework - if p.ProjectType != "standard library" { + if p.ProjectType != frameworks.StandardLibrary.String() { err = utils.GoGetPackage(projectPath, p.FrameworkMap[p.ProjectType].packageName) if err != nil { log.Printf("Could not install go dependency for the chosen framework %v\n", err) diff --git a/cmd/steps/steps.go b/cmd/steps/steps.go index 23015b9d..7122761d 100644 --- a/cmd/steps/steps.go +++ b/cmd/steps/steps.go @@ -38,7 +38,7 @@ func InitSteps(options *Options) *Steps { StepName: "Go Project Framework", Options: []Item{ { - Title: "Standard library", + Title: "Standard-library", Desc: "The built-in Go standard library HTTP package", }, { @@ -61,8 +61,9 @@ func InitSteps(options *Options) *Steps { Title: "HttpRouter", Desc: "HttpRouter is a lightweight high performance HTTP request router for Go", }, - {Title: "Echo", - Desc: "High performance, extensible, minimalist Go web framework", + { + Title: "Echo", + Desc: "High performance, extensible, minimalist Go web framework", }, }, Headers: "What framework do you want to use in your Go project?", From 8e8582672ff98516357a98d31d09db08de2ca95a Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 16:02:47 +0100 Subject: [PATCH 4/8] Turned ProjectType string type into an actual Framework type to ensure all instances that are passed are a valid state --- cmd/create.go | 21 ++++++++++++--------- cmd/program/program.go | 12 ++++++------ cmd/steps/steps.go | 10 +++++++--- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 9c4c3f21..72bbabc3 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -31,9 +31,9 @@ const logo = ` ` var ( - logoStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#01FAC6")).Bold(true) - tipMsgStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("190")).Italic(true) - endingMsgStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("170")).Bold(true) + logoStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#01FAC6")).Bold(true) + tipMsgStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("190")).Italic(true) + endingMsgStyle = lipgloss.NewStyle().PaddingLeft(1).Foreground(lipgloss.Color("170")).Bold(true) ) func init() { @@ -65,15 +65,18 @@ var createCmd = &cobra.Command{ isInteractive := !utils.HasChangedFlag(cmd.Flags()) flagName := cmd.Flag("name").Value.String() - flagFramework := cmd.Flag("framework").Value.String() - if flagFramework != "" { - isValid := isValidProjectType(flagFramework, frameworks.AllowedProjectTypes) + _flagFramework := cmd.Flag("framework").Value.String() + + if _flagFramework != "" { + isValid := isValidProjectType(_flagFramework, frameworks.AllowedProjectTypes) if !isValid { - cobra.CheckErr(fmt.Errorf("Project type '%s' is not valid. Valid types are: %s", flagFramework, strings.Join(frameworks.AllowedProjectTypes, ", "))) + cobra.CheckErr(fmt.Errorf("Project type '%s' is not valid. Valid types are: %s", _flagFramework, strings.Join(frameworks.AllowedProjectTypes, ", "))) } } + flagFramework := frameworks.Framework(_flagFramework) + project := &program.Project{ FrameworkMap: make(map[string]program.Framework), ProjectName: flagName, @@ -107,8 +110,8 @@ var createCmd = &cobra.Command{ *step.Field = s.Choice } - project.ProjectType = strings.ToLower(options.ProjectType) - _ = cmd.Flag("framework").Value.Set(project.ProjectType) + project.ProjectType = options.ProjectType + _ = cmd.Flag("framework").Value.Set(project.ProjectType.String()) } currentWorkingDir, err := os.Getwd() diff --git a/cmd/program/program.go b/cmd/program/program.go index a43693a6..6c8f8911 100644 --- a/cmd/program/program.go +++ b/cmd/program/program.go @@ -22,7 +22,7 @@ type Project struct { ProjectName string Exit bool AbsolutePath string - ProjectType string + ProjectType frameworks.Framework FrameworkMap map[string]Framework } @@ -140,8 +140,8 @@ func (p *Project) CreateMainFile() error { } // Install the correct package for the selected framework - if p.ProjectType != frameworks.StandardLibrary.String() { - err = utils.GoGetPackage(projectPath, p.FrameworkMap[p.ProjectType].packageName) + if p.ProjectType != frameworks.StandardLibrary { + err = utils.GoGetPackage(projectPath, p.FrameworkMap[p.ProjectType.String()].packageName) if err != nil { log.Printf("Could not install go dependency for the chosen framework %v\n", err) cobra.CheckErr(err) @@ -284,13 +284,13 @@ func (p *Project) CreateFileWithInjection(pathToCreate string, projectPath strin switch methodName { case "main": - createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType].templater.Main()))) + createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType.String()].templater.Main()))) err = createdTemplate.Execute(createdFile, p) case "server": - createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType].templater.Server()))) + createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType.String()].templater.Server()))) err = createdTemplate.Execute(createdFile, p) case "routes": - createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType].templater.Routes()))) + createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType.String()].templater.Routes()))) err = createdTemplate.Execute(createdFile, p) } diff --git a/cmd/steps/steps.go b/cmd/steps/steps.go index 7122761d..3b32795d 100644 --- a/cmd/steps/steps.go +++ b/cmd/steps/steps.go @@ -2,7 +2,10 @@ // each step of the CLI package steps -import textinput "github.com/melkeydev/go-blueprint/cmd/ui/textinput" +import ( + "github.com/melkeydev/go-blueprint/cmd/frameworks" + textinput "github.com/melkeydev/go-blueprint/cmd/ui/textinput" +) // A StepSchema contains the data that is used // for an individual step of the CLI @@ -27,11 +30,12 @@ type Item struct { // Options contains the name and type of the created project type Options struct { ProjectName *textinput.Output - ProjectType string + ProjectType frameworks.Framework } // InitSteps initializes and returns the *Steps to be used in the CLI program func InitSteps(options *Options) *Steps { + projectType := options.ProjectType.String() steps := &Steps{ []StepSchema{ { @@ -67,7 +71,7 @@ func InitSteps(options *Options) *Steps { }, }, Headers: "What framework do you want to use in your Go project?", - Field: &options.ProjectType, + Field: &projectType, }, }, } From f52005387e8ce76e557acb59c4f116aca8eff52f Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 16:09:35 +0100 Subject: [PATCH 5/8] Removed redundant validation for framework flag --- cmd/create.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 72bbabc3..37aab730 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -66,16 +66,9 @@ var createCmd = &cobra.Command{ flagName := cmd.Flag("name").Value.String() - _flagFramework := cmd.Flag("framework").Value.String() - - if _flagFramework != "" { - isValid := isValidProjectType(_flagFramework, frameworks.AllowedProjectTypes) - if !isValid { - cobra.CheckErr(fmt.Errorf("Project type '%s' is not valid. Valid types are: %s", _flagFramework, strings.Join(frameworks.AllowedProjectTypes, ", "))) - } - } - - flagFramework := frameworks.Framework(_flagFramework) + // VarP always returns a valid option, if the option is not + // valid the program errors out + flagFramework := frameworks.Framework(cmd.Flag("framework").Value.String()) project := &program.Project{ FrameworkMap: make(map[string]program.Framework), From d59e729886046e71ec9f2c954562310c51d50666 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 16:11:49 +0100 Subject: [PATCH 6/8] Commented out unused function --- cmd/create.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 37aab730..3ac83028 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -135,11 +135,11 @@ var createCmd = &cobra.Command{ // isValidProjectType checks if the inputted project type matches // the currently supported list of project types -func isValidProjectType(input string, allowedTypes []string) bool { - for _, t := range allowedTypes { - if input == t { - return true - } - } - return false -} +// func isValidProjectType(input string, allowedTypes []string) bool { +// for _, t := range allowedTypes { +// if input == t { +// return true +// } +// } +// return false +// } From 7bef3e027fa9491d4c8a25ae1f40ff8124581bed Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 16:17:47 +0100 Subject: [PATCH 7/8] Updated FrameworkMap to index Frameworks --- cmd/create.go | 2 +- cmd/program/program.go | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 3ac83028..4126ed56 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -71,7 +71,7 @@ var createCmd = &cobra.Command{ flagFramework := frameworks.Framework(cmd.Flag("framework").Value.String()) project := &program.Project{ - FrameworkMap: make(map[string]program.Framework), + FrameworkMap: make(map[frameworks.Framework]program.Framework), ProjectName: flagName, ProjectType: flagFramework, } diff --git a/cmd/program/program.go b/cmd/program/program.go index 6c8f8911..3698a26d 100644 --- a/cmd/program/program.go +++ b/cmd/program/program.go @@ -23,7 +23,7 @@ type Project struct { Exit bool AbsolutePath string ProjectType frameworks.Framework - FrameworkMap map[string]Framework + FrameworkMap map[frameworks.Framework]Framework } // A Framework contains the name and templater for a @@ -68,37 +68,37 @@ func (p *Project) ExitCLI(tprogram *tea.Program) { // createFrameWorkMap adds the current supported // Frameworks into a Project's FrameworkMap func (p *Project) createFrameworkMap() { - p.FrameworkMap[frameworks.Chi.String()] = Framework{ + p.FrameworkMap[frameworks.Chi] = Framework{ packageName: chiPackage, templater: tpl.ChiTemplates{}, } - p.FrameworkMap[frameworks.StandardLibrary.String()] = Framework{ + p.FrameworkMap[frameworks.StandardLibrary] = Framework{ packageName: []string{}, templater: tpl.StandardLibTemplate{}, } - p.FrameworkMap[frameworks.Gin.String()] = Framework{ + p.FrameworkMap[frameworks.Gin] = Framework{ packageName: ginPackage, templater: tpl.GinTemplates{}, } - p.FrameworkMap[frameworks.Fiber.String()] = Framework{ + p.FrameworkMap[frameworks.Fiber] = Framework{ packageName: fiberPackage, templater: tpl.FiberTemplates{}, } - p.FrameworkMap[frameworks.GorillaMux.String()] = Framework{ + p.FrameworkMap[frameworks.GorillaMux] = Framework{ packageName: gorillaPackage, templater: tpl.GorillaTemplates{}, } - p.FrameworkMap[frameworks.HttpRouter.String()] = Framework{ + p.FrameworkMap[frameworks.HttpRouter] = Framework{ packageName: routerPackage, templater: tpl.RouterTemplates{}, } - p.FrameworkMap[frameworks.Echo.String()] = Framework{ + p.FrameworkMap[frameworks.Echo] = Framework{ packageName: echoPackage, templater: tpl.EchoTemplates{}, } @@ -141,7 +141,7 @@ func (p *Project) CreateMainFile() error { // Install the correct package for the selected framework if p.ProjectType != frameworks.StandardLibrary { - err = utils.GoGetPackage(projectPath, p.FrameworkMap[p.ProjectType.String()].packageName) + err = utils.GoGetPackage(projectPath, p.FrameworkMap[p.ProjectType].packageName) if err != nil { log.Printf("Could not install go dependency for the chosen framework %v\n", err) cobra.CheckErr(err) @@ -284,13 +284,13 @@ func (p *Project) CreateFileWithInjection(pathToCreate string, projectPath strin switch methodName { case "main": - createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType.String()].templater.Main()))) + createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType].templater.Main()))) err = createdTemplate.Execute(createdFile, p) case "server": - createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType.String()].templater.Server()))) + createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType].templater.Server()))) err = createdTemplate.Execute(createdFile, p) case "routes": - createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType.String()].templater.Routes()))) + createdTemplate := template.Must(template.New(fileName).Parse(string(p.FrameworkMap[p.ProjectType].templater.Routes()))) err = createdTemplate.Execute(createdFile, p) } From 765f369120115f90faf85560d05e9228ace72d52 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 12 Nov 2023 16:34:15 +0100 Subject: [PATCH 8/8] Added some comments and removed dead code --- cmd/create.go | 11 ----------- cmd/frameworks/frameworkFlag.go | 12 ++++++++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 4126ed56..26dcfb07 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -132,14 +132,3 @@ var createCmd = &cobra.Command{ } }, } - -// isValidProjectType checks if the inputted project type matches -// the currently supported list of project types -// func isValidProjectType(input string, allowedTypes []string) bool { -// for _, t := range allowedTypes { -// if input == t { -// return true -// } -// } -// return false -// } diff --git a/cmd/frameworks/frameworkFlag.go b/cmd/frameworks/frameworkFlag.go index 6e0987f5..71132c32 100644 --- a/cmd/frameworks/frameworkFlag.go +++ b/cmd/frameworks/frameworkFlag.go @@ -9,6 +9,9 @@ import ( type Framework string +// These are all the current frameworks supported, if you want to add one you +// can simply copy and past a line here. Do not forget to also add it into the +// AllowedProjectTypes slice too! const ( Chi Framework = "chi" Gin Framework = "gin" @@ -21,6 +24,13 @@ const ( var AllowedProjectTypes = []string{string(Chi), string(Gin), string(Fiber), string(GorillaMux), string(HttpRouter), string(StandardLibrary), string(Echo)} +// This interface is required on a type to make it useable as a flag +// +// type Value interface { +// String() string +// Set(string) error +// Type() string +// } func (f Framework) String() string { return string(f) } @@ -39,6 +49,8 @@ func (f *Framework) Set(value string) error { } } +// This function returns the options that are shown to the user on shell +// completions for the -f or --framework flag func FrameworkCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return AllowedProjectTypes, cobra.ShellCompDirectiveDefault }