cmd: Validate cmd names

Fix #269
This commit is contained in:
Albert Nigmatzianov 2017-05-01 22:52:58 +02:00
parent 903e5b7073
commit 6bcf163261
2 changed files with 73 additions and 3 deletions

View File

@ -17,12 +17,14 @@ import (
"fmt"
"os"
"path/filepath"
"unicode"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")
RootCmd.AddCommand(addCmd)
}
@ -50,12 +52,55 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
er(err)
}
project := NewProjectFromPath(wd)
createCmdFile(project, args[0])
cmdName := validateCmdName(args[0])
createCmdFile(project, cmdName)
},
}
func init() {
addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")
// validateCmdName returns source without any dashes and underscore.
// If there will be dash or underscore, next letter will be uppered.
// It supports only ASCII (1-byte character) strings.
func validateCmdName(source string) string {
i := 0
l := len(source)
// The output is initialized on demand, then first dash or underscore
// occurs.
var output string
for i < l {
if source[i] == '-' || source[i] == '_' {
if output == "" {
output = source[:i]
}
// If next character is dash or underscore,
// just skip the current character.
if source[i+1] == '-' || source[i+1] == '_' {
i++
continue
}
// If the current character is dash or underscore,
// upper next letter and add to output.
output += string(unicode.ToUpper(rune(source[i+1])))
// We know, what source[i] is dash or underscore and source[i+1] is
// uppered character, so make i = i+2.
i += 2
continue
}
// If the current character isn't dash or underscore,
// just add it.
if output != "" {
output += string(source[i])
}
i++
}
if output == "" {
return source // source is initially valid name.
}
return output
}
func createCmdFile(project *Project, cmdName string) {

25
cobra/cmd/add_test.go Normal file
View File

@ -0,0 +1,25 @@
package cmd
import "testing"
func TestValidateCmdName(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"cmdName", "cmdName"},
{"cmd_name", "cmdName"},
{"cmd-name", "cmdName"},
{"cmd______Name", "cmdName"},
{"cmd------Name", "cmdName"},
{"cmd______name", "cmdName"},
{"cmd------name", "cmdName"},
}
for _, testCase := range testCases {
got := validateCmdName(testCase.input)
if testCase.expected != got {
t.Errorf("Expected %q, got %q", testCase.expected, got)
}
}
}