Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

substitute multiple '%pistol-extra%' in one argument #58

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.