-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from lets-cli/fix-ref-args
Fix ref args
- Loading branch information
Showing
11 changed files
with
97 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,52 @@ | ||
package parser | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/kballard/go-shellquote" | ||
"github.com/lets-cli/lets/config/config" | ||
) | ||
|
||
func parseArgs(rawArgs interface{}, newCmd *config.Command) error { | ||
args, ok := rawArgs.(string) | ||
if !ok { | ||
switch args := rawArgs.(type) { | ||
case string: | ||
argsList, err := shellquote.Split(args) | ||
if err != nil { | ||
return parseError( | ||
"can not parse into args list", | ||
newCmd.Name, | ||
ARGS, | ||
err.Error(), | ||
) | ||
} | ||
|
||
newCmd.RefArgs = argsList | ||
case []string: | ||
newCmd.RefArgs = args | ||
case []interface{}: | ||
for _, arg := range args { | ||
newCmd.RefArgs = append(newCmd.RefArgs, fmt.Sprintf("%s", arg)) | ||
} | ||
default: | ||
return parseError( | ||
"must be a string", | ||
"must be a string or a list of string", | ||
newCmd.Name, | ||
ARGS, | ||
"", | ||
) | ||
} | ||
|
||
newCmd.RefArgs = args | ||
|
||
return nil | ||
} | ||
|
||
func postprocessRefArgs(cfg *config.Config) { | ||
for _, cmd := range cfg.Commands { | ||
for idx, arg := range cmd.RefArgs { | ||
// we have to expand env here on our own, since this args not came from users tty, and not expanded before lets | ||
cmd.RefArgs[idx] = os.Expand(arg, func(key string) string { | ||
return cfg.Env[key] | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,7 @@ commands: | |
hello-world: | ||
ref: hello | ||
args: World | ||
|
||
hello-list: | ||
ref: hello | ||
args: [Fellow, friend] |