Skip to content

Commit

Permalink
Update sub.go
Browse files Browse the repository at this point in the history
  • Loading branch information
icy authored Jul 23, 2020
1 parent fc5baec commit 52e0127
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ var varPrefix = ""
// Internal function that replaces ${VAR_NAME} with environment value.
func repl_func(in []byte) []byte {
in_st := string(in)
// FIXME: Expecting variable in the form `${FOO_BAR}`.
// When user provides some regexp with `-p prefix`, this can be tricky.
// Ensure that input data is long enough
if 2 > len(in_st)-1 {
var_set := false
allVarSet = allVarSet && var_set
var_val := fmt.Sprintf("<%s::error::invalid_input_length>", in_st)
return []byte(var_val)
}

// FIXME: Expecting variable in the form `${FOO_BAR}`.
// FIXME: That means, there is no way to support user form e.g `$<FOO_BAR>`
// FIXME: (recall `sed` style?)
// When user provides some regexp with `-p prefix`, this can be tricky.
if in_st[0:2] != "${" || in_st[len(in_st)-1:len(in_st)] != "}" {
var_set := false
allVarSet = allVarSet && var_set
var_val := fmt.Sprintf("<%s::error::invalid_input_regexp>", in_st)
var_val := fmt.Sprintf("<%s::error::invalid_input_data>", in_st)
return []byte(var_val)
}

Expand Down Expand Up @@ -103,6 +106,7 @@ func main() {
flag.BoolVar(&scanOnly, "v", false, "Output ocurrences of variables in input.")
flag.StringVar(&varPrefix, "p", "[^}]+", "Limit substitution to variables that match this prefix.")
flag.CommandLine.Parse(os.Args[1:])
// FIXME: Does this generate any serious issue at runtime?
regVarname = regexp.MustCompile(fmt.Sprintf(`\${(%s)}`, varPrefix))
fmt.Fprintf(os.Stderr, ":: genvsub is reading from STDIN and looking for variables with regexp '%s'\n", regVarname)
eachLine(os.Stdin, doLine)
Expand Down

0 comments on commit 52e0127

Please sign in to comment.