Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
limit tag/name strings + add set-now flag to add
Browse files Browse the repository at this point in the history
  • Loading branch information
sosodev committed Aug 21, 2019
1 parent f1178c0 commit 4621988
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
23 changes: 23 additions & 0 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ type AddCommand struct {
func (c *AddCommand) Run(args []string) int {
// flags
var name, tag string
var set bool

// create the flagset
f := flag.NewFlagSet("AddFlags", flag.ContinueOnError)
f.StringVar(&name, "name", "", "name")
f.StringVar(&tag, "tag", "latest", "tag")
f.BoolVar(&set, "set-now", false, "set-now")
f.Usage = func() { c.Ui.Error(c.Help()) }

// separate the flags from the normal args so ordering doesn't matter
Expand Down Expand Up @@ -56,6 +58,16 @@ func (c *AddCommand) Run(args []string) int {
name = binary.Name()
}

if !utilities.ValidIdentifier(name) {
c.Ui.Error(fmt.Sprintf("invalid name \"%s\" - valid names can contain alphanumeric characters, hyphens, and periods", name))
return 1
}

if !utilities.ValidIdentifier(tag) {
c.Ui.Error(fmt.Sprintf("invalid tag \"%s\" - valid tags can contain alphanumeric characters, hyphens, and periods", tag))
return 1
}

// create the tagged name and path
taggedName := name + "-kktag:" + tag
taggedPath := path.Join(config.BinariesPath(), taggedName)
Expand All @@ -67,6 +79,15 @@ func (c *AddCommand) Run(args []string) int {
}

c.Ui.Output(fmt.Sprintf("Added %s tagged as %s", name, tag))

if set {
sc := &SetCommand{
Ui: c.Ui,
}

return sc.Run([]string{name, tag})
}

return 0
}

Expand All @@ -87,6 +108,8 @@ Options:
-tag=tag The tag given to the binary to differentiate it from
others of the same name. Defaults to "latest".
Kind of like a docker tag.
--set-now Immediately runs "kitkit set" after adding the specified binary
`
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
}

func realMain(args []string) int {
c := cli.NewCLI("KitKit", "1.0")
c := cli.NewCLI("KitKit", "1.1")
c.Args = args
ui := &cli.BasicUi{
Reader: os.Stdin,
Expand Down
6 changes: 6 additions & 0 deletions utilities/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utilities
import (
"io/ioutil"
"os"
"regexp"
"strings"

"github.com/simplyserenity/kitkit/config"
Expand All @@ -23,3 +24,8 @@ func SplitTrackedName(trackedName string) (string, string) {
parts := strings.Split(trackedName, "-kktag:")
return parts[0], parts[1]
}

// ValidIdentifier checks whether the given string can be used as a tag or name
func ValidIdentifier(identifier string) bool {
return regexp.MustCompile("[A-Za-z0-9-.]*").FindString(identifier) == identifier
}
24 changes: 24 additions & 0 deletions utilities/binaries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,27 @@ func TestSplitTrackedName(t *testing.T) {
})
}
}

func TestValidIdentifier(t *testing.T) {
testCases := []struct {
Have string
Want bool
}{
{
Have: "valid-identifier1.0",
Want: true,
},
{
Have: "invalid/identifier",
Want: false,
},
}

for _, tc := range testCases {
got := ValidIdentifier(tc.Have)

if tc.Want != got {
t.Errorf("incorrect validation for \"%s\" wanted: %t got: %t", tc.Have, tc.Want, got)
}
}
}

0 comments on commit 4621988

Please sign in to comment.