Skip to content

Commit

Permalink
feat: print available environment variables support (resolves #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Oct 1, 2020
1 parent ac7b489 commit 04cb2bf
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func Execute() {
}

func onCommand(ctx *cli.Context) error {
// 0. If there are not args show all environment variables
if ctx.NArg() == 0 {
return printAllAction(ctx)
}

// 1. Version flag
v := ctx.Bool("version")

Expand All @@ -54,24 +59,39 @@ func onCommand(ctx *cli.Context) error {
}
}

// 3. Execute the given command
if ctx.NArg() > 0 {
args := ctx.Args().Slice()
cmdIn := args[0]
return execCmdAction(ctx)
}

_, err := exec.LookPath(cmdIn)
return nil
}

if err != nil {
return fmt.Errorf("executable \"%s\" was not found\n%s", cmdIn, err)
}
// printAllAction prints all environment variables in plain text
func printAllAction(ctx *cli.Context) (err error) {
for _, s := range os.Environ() {
fmt.Println(s)
}

cmd := exec.Command(cmdIn, args[1:]...)
return nil
}

cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
// execCmdAction executes a command along with its env variables
func execCmdAction(ctx *cli.Context) (err error) {
args := ctx.Args().Slice()
cmdIn := args[0]

return cmd.Run()
_, err = exec.LookPath(cmdIn)

if err != nil {
return fmt.Errorf("executable \"%s\" was not found\n%s", cmdIn, err)
}

return nil
cmd := exec.Command(cmdIn, args[1:]...)

cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout

return cmd.Run()
}

0 comments on commit 04cb2bf

Please sign in to comment.