Skip to content

Commit

Permalink
adding option to choose a different tag vector separator
Browse files Browse the repository at this point in the history
  • Loading branch information
matfax committed Jan 5, 2019
1 parent b4d6e93 commit 81ea749
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,21 @@ echo "_:1.2.3" | tuplip exec addLatest
1.2.3
latest
```

### vectorSeparator

`vectorSeparator` sets a different tag vector separator than the default space character.

#### Example

```bash
echo "something; fancy" | tuplip exec vectorSeparator=";"
```

#### Result

```bash
something
fancy
fancy-something
```
11 changes: 10 additions & 1 deletion cmd/tuplip/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ func (c *ExecCommand) Run(args []string) int {
if strings.Contains(lowerArg, "addlatest") {
tuplip.AddLatest = true
}
if strings.Contains(lowerArg, "vectorseparator") {
sepArg := strings.SplitAfter(arg, "=")
if len(sepArg) < 2 || len(sepArg[1]) == 0 {
fmt.Fprintln(os.Stderr, "warning: the given tag vector separator is invalid, falling back to space")
} else {
tuplip.Separator = sepArg[1]
}
}
}
return execute(tuplip)
}
Expand All @@ -49,7 +57,8 @@ func (c *ExecCommand) Help() string {
"excludeMajor to exclude the major versions from the result set\n" +
"excludeMinor to exclude the minor versions from the result set\n" +
"excludeBase to exclude the base alias without version suffix from the result set\n" +
"addLatest to add an additional 'latest' tag to the result set\n"
"addLatest to add an additional 'latest' tag to the result set\n" +
"vectorSeparator=% to choose a different tag vector separator than the default space character\n"
}

// Synopsis gives a short description of the purpose.
Expand Down
27 changes: 20 additions & 7 deletions pkg/tupliplib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,26 @@ type Tuplip struct {
ExcludeBase bool
// AddLatest adds an additional 'latest' tag to the result set.
AddLatest bool
// Separator to split the separate tag vector aliases. The default separator is single space.
Separator string
}

// The separator that separates the alias form the semantic version.
// VersionSeparator is the separator that separates the alias form the semantic version.
const VersionSeparator = ":"

// The alias for a wildcard dependency to build a base tag (i.e., semantic version without a prefix).
// WildcardDependency is the alias for a wildcard dependency to build a root tag vector
// (i.e., semantic version without a prefix).
const WildcardDependency = "_"

// The separator that separates the digits of a semantic version.
// VersionDot is the separator that separates the digits of a semantic version.
const VersionDot = "."

// The separator that separates the sub tags in a Docker tag.
// DockerTagSeparator is the separator that separates the sub tags in a Docker tag.
const DockerTagSeparator = "-"

// VectorSeparator is the default tag vector separator.
const VectorSeparator = " "

// buildTag parses a semantic version with the given version digits. Optionally, prefix an alias tag.
func (t Tuplip) buildTag(withBase bool, alias string, versionDigits ...uint64) (string, error) {
var builder strings.Builder
Expand Down Expand Up @@ -132,9 +138,16 @@ func (t Tuplip) nonEmpty(input string) bool {
return input != ""
}

// splitBySeparator separates the input string by an empty char.
// splitBySeparator separates the input string by the chosen character and trims superfluous spaces.
func (t Tuplip) splitBySeparator(input string) (result []string) {
return strings.Split(input, " ")
if t.Separator == "" {
t.Separator = VectorSeparator
}
result = strings.Split(input, t.Separator)
for i, el := range result {
result[i] = strings.TrimSpace(el)
}
return
}

// packInSet packs a set as subset into a new set.
Expand Down Expand Up @@ -162,7 +175,7 @@ func (t Tuplip) failOnEmpty(inputSet mapset.Set) (mapset.Set, error) {

// join joins all subtags (i.e., elements of the given set) to all possible representations by building a cartesian
// product of them. The subtags are separated by the given Docker separator. The subtags are ordered alphabetically
// to ensure that a base tag (i.e., a tag without an alias) is mentioned before alias tags.
// to ensure that a root tag vector (i.e., a tag without an alias) is mentioned before alias tags.
func (t Tuplip) join(inputSet mapset.Set) (result mapset.Set) {
result = mapset.NewSet()
inputSlice := inputSet.ToSlice()
Expand Down
12 changes: 12 additions & 0 deletions pkg/tupliplib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ func TestTuplipStream_FromReader(t *testing.T) {
args: args{[]string{"_:2.0.0", "foo"}},
want: []string{"latest", "foo", "2", "2.0", "2.0.0", "2-foo", "2.0-foo", "2.0.0-foo"},
},
{
name: "Wildcard Unary Tag With Long Version And a Different Separator",
t: Tuplip{Separator: ";"},
args: args{[]string{" _:2.0.0; foo "}},
want: []string{"foo", "2", "2.0", "2.0.0", "2-foo", "2.0-foo", "2.0.0-foo"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -391,6 +397,12 @@ func TestTuplip_splitBySeparator(t *testing.T) {
args: args{"foo boo hoo"},
wantResult: []string{"foo", "boo", "hoo"},
},
{
name: "Split Tuple With Different Separator",
t: Tuplip{Separator: ","},
args: args{"foo, boo,hoo"},
wantResult: []string{"foo", "boo", "hoo"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 81ea749

Please sign in to comment.