Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework flag shell completion #80

20 changes: 13 additions & 7 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,14 +34,19 @@ 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 frameworks.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().VarP(&frameworkFlag, "framework", "f", fmt.Sprintf("Framework to use. Allowed values: %s", strings.Join(frameworks.AllowedProjectTypes, ", ")))

This comment was marked as resolved.


if err := createCmd.RegisterFlagCompletionFunc("framework", frameworks.FrameworkCompletion); err != nil {
log.Fatal(err)
}
}

// createCmd defines the "create" command for the CLI
Expand All @@ -62,16 +68,16 @@ var createCmd = &cobra.Command{
flagFramework := cmd.Flag("framework").Value.String()

if flagFramework != "" {
isValid := isValidProjectType(flagFramework, allowedProjectTypes)
isValid := isValidProjectType(flagFramework, frameworks.AllowedProjectTypes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed I believe, a Var flag will automatically error if it's not in the enum!

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)
Expand All @@ -86,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 == "" {
Expand All @@ -102,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()
Expand Down
44 changes: 44 additions & 0 deletions cmd/frameworks/frameworkFlag.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 10 additions & 8 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{},
}
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions cmd/steps/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func InitSteps(options *Options) *Steps {
StepName: "Go Project Framework",
Options: []Item{
{
Title: "Standard library",
Title: "Standard-library",

This comment was marked as resolved.

Desc: "The built-in Go standard library HTTP package",
},
{
Expand All @@ -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?",
Expand Down
Loading