Skip to content

Commit

Permalink
Merge pull request #745 from suzuki-shunsuke/feat/flag-env
Browse files Browse the repository at this point in the history
feat: support passing command line arguments via environment variables
  • Loading branch information
suzuki-shunsuke authored Apr 19, 2023
2 parents b915901 + f2bef37 commit b2910c6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
50 changes: 41 additions & 9 deletions pkg/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,47 @@ func New(flags *LDFlags) *cli.App {
app.Usage = "Notify the execution result of terraform command"
app.Version = flags.AppVersion()
app.Flags = []cli.Flag{
&cli.StringFlag{Name: "owner", Usage: "GitHub Repository owner name"},
&cli.StringFlag{Name: "repo", Usage: "GitHub Repository name"},
&cli.StringFlag{Name: "sha", Usage: "commit SHA (revision)"},
&cli.StringFlag{Name: "build-url", Usage: "build url"},
&cli.StringFlag{Name: "log-level", Usage: "log level"},
&cli.IntFlag{Name: "pr", Usage: "pull request number"},
&cli.StringFlag{Name: "config", Usage: "config path"},
&cli.StringSliceFlag{Name: "var", Usage: "template variables. The format of value is '<name>:<value>'"},
&cli.StringFlag{Name: "output", Usage: "specify file to output result instead of post comment"},
&cli.StringFlag{
Name: "owner",
Usage: "GitHub Repository owner name",
EnvVars: []string{"TFCMT_REPO_OWNER"},
},
&cli.StringFlag{
Name: "repo",
Usage: "GitHub Repository name",
EnvVars: []string{"TFCMT_REPO_NAME"},
},
&cli.StringFlag{
Name: "sha",
Usage: "commit SHA (revision)",
EnvVars: []string{"TFCMT_SHA"},
},
&cli.StringFlag{
Name: "build-url",
Usage: "build url",
},
&cli.StringFlag{
Name: "log-level",
Usage: "log level",
},
&cli.IntFlag{
Name: "pr",
Usage: "pull request number",
EnvVars: []string{"TFCMT_PR_NUMBER"},
},
&cli.StringFlag{
Name: "config",
Usage: "config path",
EnvVars: []string{"TFCMT_CONFIG"},
},
&cli.StringSliceFlag{
Name: "var",
Usage: "template variables. The format of value is '<name>:<value>'",
},
&cli.StringFlag{
Name: "output",
Usage: "specify file to output result instead of post comment",
},
}
app.Commands = []*cli.Command{
{
Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/apply.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"os"

"github.com/suzuki-shunsuke/tfcmt/pkg/controller"
"github.com/suzuki-shunsuke/tfcmt/pkg/terraform"
"github.com/urfave/cli/v2"
Expand All @@ -20,7 +22,7 @@ func cmdApply(ctx *cli.Context) error {
setLogLevel(logLevel)
}

if err := parseOpts(ctx, &cfg); err != nil {
if err := parseOpts(ctx, &cfg, os.Environ()); err != nil {
return err
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/cli/plan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"os"

"github.com/suzuki-shunsuke/tfcmt/pkg/controller"
"github.com/suzuki-shunsuke/tfcmt/pkg/terraform"
"github.com/urfave/cli/v2"
Expand All @@ -19,7 +21,7 @@ func cmdPlan(ctx *cli.Context) error {
setLogLevel(logLevel)
}

if err := parseOpts(ctx, &cfg); err != nil {
if err := parseOpts(ctx, &cfg, os.Environ()); err != nil {
return err
}

Expand Down
18 changes: 16 additions & 2 deletions pkg/cli/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/urfave/cli/v2"
)

func parseVars(vars []string, envs []string, varsM map[string]string) error {
parseVarEnvs(envs, varsM)
return parseVarOpts(vars, varsM)
}

func parseVarOpts(vars []string, varsM map[string]string) error {
for _, v := range vars {
a := strings.Index(v, ":")
Expand All @@ -19,7 +24,16 @@ func parseVarOpts(vars []string, varsM map[string]string) error {
return nil
}

func parseOpts(ctx *cli.Context, cfg *config.Config) error {
func parseVarEnvs(envs []string, m map[string]string) {
for _, kv := range envs {
k, v, _ := strings.Cut(kv, "=")
if a := strings.TrimPrefix(k, "TFCMT_VAR_"); k != a {
m[a] = v
}
}
}

func parseOpts(ctx *cli.Context, cfg *config.Config, envs []string) error {
if owner := ctx.String("owner"); owner != "" {
cfg.CI.Owner = owner
}
Expand Down Expand Up @@ -50,7 +64,7 @@ func parseOpts(ctx *cli.Context, cfg *config.Config) error {

vars := ctx.StringSlice("var")
vm := make(map[string]string, len(vars))
if err := parseVarOpts(vars, vm); err != nil {
if err := parseVars(vars, envs, vm); err != nil {
return err
}
cfg.Vars = vm
Expand Down

0 comments on commit b2910c6

Please sign in to comment.