Skip to content

Commit

Permalink
substitute multiple '%pistol-extra%' in one argument (#58)
Browse files Browse the repository at this point in the history
Fixes #56 .
  • Loading branch information
lucas-mior authored Apr 19, 2021
1 parent e27055f commit ac92b11
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ test: pistol
@echo or verbosity, although the arguments are passed to pistol
@echo -------------------
@./pistol --config tests/config tests/renovate.json5.bz2 -v -L
@echo -------------------
@echo Checks substitution of multiple pistol-extra arguments without
@echo a space between them \(issue 56\). The output should be:
@echo
@echo " tests/multi-extra AxB"
@echo
@echo -------------------
@./pistol --config tests/config tests/multi-extra A B

deps:
go get github.com/c4milo/github-release
Expand Down
29 changes: 20 additions & 9 deletions previewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"io"
"os/exec"
// "fmt"
"fmt"
"strconv"
"strings"
"regexp"
Expand Down Expand Up @@ -163,18 +163,29 @@ func (p *Previewer) Write(w io.Writer) (error) {
re := regexp.MustCompile(`%pistol-extra([0-9]+)%`)

for _, arg := range p.Args {
argAux := arg
if(re.MatchString(arg)) {
auxStr := re.ReplaceAllString(arg, "$1")
auxInt, err := strconv.Atoi(auxStr);
if (err == nil && len(p.Extras) > auxInt) {
arg = re.ReplaceAllString(arg, p.Extras[auxInt])
} else {
continue
// We iterate all indices of matches in every command line
// argument written in the config, because the match can occur
// in multiple arguments, see #56.
allIndexes := re.FindAllStringSubmatchIndex(arg, -1)
for _, loc := range allIndexes {
// We try to convert the string found in the argument to a
// number.
auxInt, err := strconv.Atoi(arg[loc[2]:loc[3]])
if (err == nil && len(p.Extras) > auxInt) {
// substitute the %pistol-extra[#]% argument in the
// final CLI string.
current := fmt.Sprintf("%%pistol-extra%d%%", auxInt)
argAux = strings.ReplaceAll(argAux, current, p.Extras[auxInt])
} else {
continue
}
}
} else {
arg = strings.ReplaceAll(arg, "%pistol-filename%", replStr)
argAux = strings.ReplaceAll(argAux, "%pistol-filename%", replStr)
}
argsOut = append(argsOut, arg)
argsOut = append(argsOut, argAux)
}

if p.Command == "sh:" {
Expand Down
2 changes: 2 additions & 0 deletions tests/config
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ fpath .*/tests/renovate.json5.bz2$ bzcat %pistol-filename%
application/x-bzip2 not-a-real-command %pistol-filename%
# Test exit code is non-zero in case a command starts but fails to finish
fpath .*/tests$ bat %pistol-filename%
# Test substitution of multiple extra arguments without spaces between (#56)
fpath .*/tests/multi-extra echo %pistol-filename% %pistol-extra0%x%pistol-extra1%
Empty file added tests/multi-extra
Empty file.

0 comments on commit ac92b11

Please sign in to comment.