cmd: Validate cmd names
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #269
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 903e5b7 · Browse files · 2 files·+73 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+73 added−3 removed
- other:+73 added−3 removed
Provenance
- Commit
6bcf163- Tree
5c5b2ef- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
903e5b7
2 files changed+73 added−3 removed
added:cobra/cmd/add_test.go+25 added
1+package cmd2+3+import "testing"4+5+func TestValidateCmdName(t *testing.T) {6+ testCases := []struct {7+ input string8+ expected string9+ }{10+ {"cmdName", "cmdName"},11+ {"cmd_name", "cmdName"},12+ {"cmd-name", "cmdName"},13+ {"cmd______Name", "cmdName"},14+ {"cmd------Name", "cmdName"},15+ {"cmd______name", "cmdName"},16+ {"cmd------name", "cmdName"},17+ }18+19+ for _, testCase := range testCases {20+ got := validateCmdName(testCase.input)21+ if testCase.expected != got {22+ t.Errorf("Expected %q, got %q", testCase.expected, got)23+ }24+ }25+}modified:cobra/cmd/add.go+48 added-3 removed
15 unmodified lines16 import (17 "fmt"18 "os"19 "path/filepath"20+ "unicode"21 22 "github.com/spf13/cobra"23 "github.com/spf13/viper"24 )25 26 func init() {27+ addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")28 RootCmd.AddCommand(addCmd)29 }30 31 var parentName string19 unmodified lines51 if err != nil {52 er(err)53 }54 project := NewProjectFromPath(wd)53- createCmdFile(project, args[0])55+ cmdName := validateCmdName(args[0])56+ createCmdFile(project, cmdName)57 },58 }59 57-func init() {58- addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")60+// validateCmdName returns source without any dashes and underscore.61+// If there will be dash or underscore, next letter will be uppered.62+// It supports only ASCII (1-byte character) strings.63+func validateCmdName(source string) string {64+ i := 065+ l := len(source)66+ // The output is initialized on demand, then first dash or underscore67+ // occurs.68+ var output string69+70+ for i < l {71+ if source[i] == '-' || source[i] == '_' {72+ if output == "" {73+ output = source[:i]74+ }75+76+ // If next character is dash or underscore,77+ // just skip the current character.78+ if source[i+1] == '-' || source[i+1] == '_' {79+ i++80+ continue81+ }82+83+ // If the current character is dash or underscore,84+ // upper next letter and add to output.85+ output += string(unicode.ToUpper(rune(source[i+1])))86+ // We know, what source[i] is dash or underscore and source[i+1] is87+ // uppered character, so make i = i+2.88+ i += 289+ continue90+ }91+92+ // If the current character isn't dash or underscore,93+ // just add it.94+ if output != "" {95+ output += string(source[i])96+ }97+ i++98+ }99+100+ if output == "" {101+ return source // source is initially valid name.102+ }103+ return output104 }105 106 func createCmdFile(project *Project, cmdName string) {107 template := `{{comment .copyright}}60 unmodified lines