Skip to content

Commit

Permalink
Implement the values shell expansion. Can be used for templating values.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Dementiev committed May 17, 2018
1 parent ce1428f commit f9a79e2
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ var runCmd = &cobra.Command{
}
}
for key, value := range megamap {
if expand {
if stringValue, ok := value.(string); ok {
value = expandValue(stringValue)
}
}
os.Setenv(key, fmt.Sprintf("%v", value))
}

Expand Down Expand Up @@ -87,19 +92,25 @@ var runCmd = &cobra.Command{
func expandArgs(args []string) []string {
var expanded []string
for _, arg := range args {
e, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("echo %s", arg)).Output()
// error is not expected.
// in the rare case that this errors
// the original arg is still used.
if err == nil {
arg = strings.TrimSpace(string(e))
}
arg = expandValue(arg)
expanded = append(expanded, arg)
}
return expanded
}

func expandValue(val string) string {
e, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("echo %s", val)).Output()
// error is not expected.
// in the rare case that this errors
// the original arg is still used.
if err == nil {
return strings.TrimSpace(string(e))
}
return val

}

func init() {
runCmd.Flags().BoolVarP(&expand, "expand", "e", false, "Expand arguments using /bin/sh")
runCmd.Flags().BoolVarP(&expand, "expand", "e", false, "Expand arguments and values using /bin/sh")
rootCmd.AddCommand(runCmd)
}

0 comments on commit f9a79e2

Please sign in to comment.