Skip to content
Myr

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

2 files+73 added−3 removed

added:cobra/cmd/add_test.go+25 added
1package cmd23import "testing"45func 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 }1819 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 lines
16import (17 "fmt"18 "os"19 "path/filepath"20 "unicode"2122 "github.com/spf13/cobra"23 "github.com/spf13/viper"24)2526func init() {27 addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")28 RootCmd.AddCommand(addCmd)29}3031var parentName string
19 unmodified lines
51 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}5957func 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.63func 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 string6970 for i < l {71 if source[i] == '-' || source[i] == '_' {72 if output == "" {73 output = source[:i]74 }7576 // 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 }8283 // 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 }9192 // 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 }99100 if output == "" {101 return source // source is initially valid name.102 }103 return output104}105106func createCmdFile(project *Project, cmdName string) {107 template := `{{comment .copyright}}
60 unmodified lines