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

Commit 4621988

Browse files
committed
limit tag/name strings + add set-now flag to add
1 parent f1178c0 commit 4621988

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

add.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ type AddCommand struct {
2323
func (c *AddCommand) Run(args []string) int {
2424
// flags
2525
var name, tag string
26+
var set bool
2627

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

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

61+
if !utilities.ValidIdentifier(name) {
62+
c.Ui.Error(fmt.Sprintf("invalid name \"%s\" - valid names can contain alphanumeric characters, hyphens, and periods", name))
63+
return 1
64+
}
65+
66+
if !utilities.ValidIdentifier(tag) {
67+
c.Ui.Error(fmt.Sprintf("invalid tag \"%s\" - valid tags can contain alphanumeric characters, hyphens, and periods", tag))
68+
return 1
69+
}
70+
5971
// create the tagged name and path
6072
taggedName := name + "-kktag:" + tag
6173
taggedPath := path.Join(config.BinariesPath(), taggedName)
@@ -67,6 +79,15 @@ func (c *AddCommand) Run(args []string) int {
6779
}
6880

6981
c.Ui.Output(fmt.Sprintf("Added %s tagged as %s", name, tag))
82+
83+
if set {
84+
sc := &SetCommand{
85+
Ui: c.Ui,
86+
}
87+
88+
return sc.Run([]string{name, tag})
89+
}
90+
7091
return 0
7192
}
7293

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

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func main() {
1414
}
1515

1616
func realMain(args []string) int {
17-
c := cli.NewCLI("KitKit", "1.0")
17+
c := cli.NewCLI("KitKit", "1.1")
1818
c.Args = args
1919
ui := &cli.BasicUi{
2020
Reader: os.Stdin,

utilities/binaries.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utilities
33
import (
44
"io/ioutil"
55
"os"
6+
"regexp"
67
"strings"
78

89
"github.com/simplyserenity/kitkit/config"
@@ -23,3 +24,8 @@ func SplitTrackedName(trackedName string) (string, string) {
2324
parts := strings.Split(trackedName, "-kktag:")
2425
return parts[0], parts[1]
2526
}
27+
28+
// ValidIdentifier checks whether the given string can be used as a tag or name
29+
func ValidIdentifier(identifier string) bool {
30+
return regexp.MustCompile("[A-Za-z0-9-.]*").FindString(identifier) == identifier
31+
}

utilities/binaries_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,27 @@ func TestSplitTrackedName(t *testing.T) {
5656
})
5757
}
5858
}
59+
60+
func TestValidIdentifier(t *testing.T) {
61+
testCases := []struct {
62+
Have string
63+
Want bool
64+
}{
65+
{
66+
Have: "valid-identifier1.0",
67+
Want: true,
68+
},
69+
{
70+
Have: "invalid/identifier",
71+
Want: false,
72+
},
73+
}
74+
75+
for _, tc := range testCases {
76+
got := ValidIdentifier(tc.Have)
77+
78+
if tc.Want != got {
79+
t.Errorf("incorrect validation for \"%s\" wanted: %t got: %t", tc.Have, tc.Want, got)
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)