From 7bb5276f5fdb799f19d44c04f74b2f39f20f2ce2 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Fri, 5 May 2017 10:30:41 +0200 Subject: [PATCH] cmd: Fix panic if cmd ends on dash or underscore --- cobra/cmd/add.go | 6 ++++++ cobra/cmd/add_test.go | 2 ++ 2 files changed, 8 insertions(+) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index 9e30671..20c6538 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -77,6 +77,12 @@ func validateCmdName(source string) string { output = source[:i] } + // If it's last rune and it's dash or underscore, + // don't add it output and break the loop. + if i == l-1 { + break + } + // If next character is dash or underscore, // just skip the current character. if source[i+1] == '-' || source[i+1] == '_' { diff --git a/cobra/cmd/add_test.go b/cobra/cmd/add_test.go index 6c1ec0f..dacbe83 100644 --- a/cobra/cmd/add_test.go +++ b/cobra/cmd/add_test.go @@ -87,6 +87,8 @@ func TestValidateCmdName(t *testing.T) { {"cmd------Name", "cmdName"}, {"cmd______name", "cmdName"}, {"cmd------name", "cmdName"}, + {"cmdName-----", "cmdName"}, + {"cmdname-", "cmdname"}, } for _, testCase := range testCases {