-
Notifications
You must be signed in to change notification settings - Fork 314
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
Closed
MitchellBerend
wants to merge
8
commits into
Melkeydev:main
from
MitchellBerend:feature/framework-completion
Closed
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
772aa23
Initial implementation for framework flag shell completion
MitchellBerend 0298b97
Created separate file for framekwork flag and cleaned up original file
MitchellBerend 09fa0d7
Replaced all hardcoded framework strings to Framework constants
MitchellBerend 8e85826
Turned ProjectType string type into an actual Framework type to ensur…
MitchellBerend f520053
Removed redundant validation for framework flag
MitchellBerend d59e729
Commented out unused function
MitchellBerend 7bef3e0
Updated FrameworkMap to index Frameworks
MitchellBerend 765f369
Added some comments and removed dead code
MitchellBerend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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, ", "))) | ||
|
||
if err := createCmd.RegisterFlagCompletionFunc("framework", frameworks.FrameworkCompletion); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// createCmd defines the "create" command for the CLI | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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 == "" { | ||
|
@@ -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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.