Skip to content

Commit

Permalink
Merge pull request #896 from suzuki-shunsuke/fix/change-env-name-and-…
Browse files Browse the repository at this point in the history
…support-var-env

feat: rename environment variables and support passing variables via environment variables
  • Loading branch information
suzuki-shunsuke authored Apr 19, 2023
2 parents 712c4b9 + ba353e4 commit e5fd429
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 74 deletions.
36 changes: 18 additions & 18 deletions pkg/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "org",
Usage: "GitHub organization name",
EnvVars: []string{"GITHUB_COMMENT_REPO_ORG", "GHCMT_REPO_ORG"},
EnvVars: []string{"GH_COMMENT_REPO_ORG"},
},
&cli.StringFlag{
Name: "repo",
Usage: "GitHub repository name",
EnvVars: []string{"GITHUB_COMMENT_REPO_NAME", "GHCMT_REPO_NAME"},
EnvVars: []string{"GH_COMMENT_REPO_NAME"},
},
&cli.StringFlag{
Name: "token",
Expand All @@ -53,7 +53,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "sha1",
Usage: "commit sha1",
EnvVars: []string{"GITHUB_COMMENT_SHA1", "GHCMT_SHA1"},
EnvVars: []string{"GH_COMMENT_SHA1"},
},
&cli.StringFlag{
Name: "template",
Expand All @@ -68,12 +68,12 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "config",
Usage: "configuration file path",
EnvVars: []string{"GITHUB_COMMENT_CONFIG", "GHCMT_CONFIG"},
EnvVars: []string{"GH_COMMENT_CONFIG"},
},
&cli.IntFlag{
Name: "pr",
Usage: "GitHub pull request number",
EnvVars: []string{"GITHUB_COMMENT_PR_NUMBER", "GHCMT_PR_NUMBER"},
EnvVars: []string{"GH_COMMENT_PR_NUMBER"},
},
&cli.StringSliceFlag{
Name: "var",
Expand All @@ -91,7 +91,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
Name: "skip-no-token",
Aliases: []string{"n"},
Usage: "works like dry-run if the GitHub Access Token isn't set",
EnvVars: []string{"GITHUB_COMMENT_SKIP_NO_TOKEN"},
EnvVars: []string{"GH_COMMENT_SKIP_NO_TOKEN", "GITHUB_COMMENT_SKIP_NO_TOKEN"},
},
&cli.BoolFlag{
Name: "silent",
Expand All @@ -117,12 +117,12 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "org",
Usage: "GitHub organization name",
EnvVars: []string{"GITHUB_COMMENT_REPO_ORG", "GHCMT_REPO_ORG"},
EnvVars: []string{"GH_COMMENT_REPO_ORG"},
},
&cli.StringFlag{
Name: "repo",
Usage: "GitHub repository name",
EnvVars: []string{"GITHUB_COMMENT_REPO_NAME", "GHCMT_REPO_NAME"},
EnvVars: []string{"GH_COMMENT_REPO_NAME"},
},
&cli.StringFlag{
Name: "token",
Expand All @@ -132,7 +132,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "sha1",
Usage: "commit sha1",
EnvVars: []string{"GITHUB_COMMENT_SHA1", "GHCMT_SHA1"},
EnvVars: []string{"GH_COMMENT_SHA1"},
},
&cli.StringFlag{
Name: "template",
Expand All @@ -147,12 +147,12 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "config",
Usage: "configuration file path",
EnvVars: []string{"GITHUB_COMMENT_CONFIG", "GHCMT_CONFIG"},
EnvVars: []string{"GH_COMMENT_CONFIG"},
},
&cli.IntFlag{
Name: "pr",
Usage: "GitHub pull request number",
EnvVars: []string{"GITHUB_COMMENT_PR_NUMBER", "GHCMT_PR_NUMBER"},
EnvVars: []string{"GH_COMMENT_PR_NUMBER"},
},
&cli.StringSliceFlag{
Name: "var",
Expand All @@ -170,7 +170,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
Name: "skip-no-token",
Aliases: []string{"n"},
Usage: "works like dry-run if the GitHub Access Token isn't set",
EnvVars: []string{"GITHUB_COMMENT_SKIP_NO_TOKEN"},
EnvVars: []string{"GH_COMMENT_SKIP_NO_TOKEN", "GITHUB_COMMENT_SKIP_NO_TOKEN"},
},
&cli.BoolFlag{
Name: "silent",
Expand All @@ -192,12 +192,12 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "org",
Usage: "GitHub organization name",
EnvVars: []string{"GITHUB_COMMENT_REPO_ORG", "GHCMT_REPO_ORG"},
EnvVars: []string{"GH_COMMENT_REPO_ORG"},
},
&cli.StringFlag{
Name: "repo",
Usage: "GitHub repository name",
EnvVars: []string{"GITHUB_COMMENT_REPO_NAME", "GHCMT_REPO_NAME"},
EnvVars: []string{"GH_COMMENT_REPO_NAME"},
},
&cli.StringFlag{
Name: "token",
Expand All @@ -207,7 +207,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "config",
Usage: "configuration file path",
EnvVars: []string{"GITHUB_COMMENT_CONFIG", "GHCMT_CONFIG"},
EnvVars: []string{"GH_COMMENT_CONFIG"},
},
&cli.StringFlag{
Name: "condition",
Expand All @@ -222,7 +222,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.IntFlag{
Name: "pr",
Usage: "GitHub pull request number",
EnvVars: []string{"GITHUB_COMMENT_PR_NUMBER", "GHCMT_PR_NUMBER"},
EnvVars: []string{"GH_COMMENT_PR_NUMBER"},
},
&cli.StringFlag{
Name: "sha1",
Expand All @@ -244,7 +244,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
Name: "skip-no-token",
Aliases: []string{"n"},
Usage: "works like dry-run if the GitHub Access Token isn't set",
EnvVars: []string{"GITHUB_COMMENT_SKIP_NO_TOKEN"},
EnvVars: []string{"GH_COMMENT_SKIP_NO_TOKEN", "GITHUB_COMMENT_SKIP_NO_TOKEN"},
},
&cli.BoolFlag{
Name: "silent",
Expand All @@ -258,7 +258,7 @@ func (runner *Runner) Run(ctx context.Context, args []string) error { //nolint:f
&cli.StringFlag{
Name: "log-level",
Usage: "log level",
EnvVars: []string{"GITHUB_COMMENT_LOG_LEVEL"},
EnvVars: []string{"GH_COMMENT_LOG_LEVEL"},
},
},
}
Expand Down
9 changes: 1 addition & 8 deletions pkg/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,10 @@ func parseExecOptions(opts *option.ExecOptions, c *cli.Context) error {
opts.Silent = c.Bool("silent")
opts.LogLevel = c.String("log-level")

vars, err := parseVarsFlag(c.StringSlice("var"))
vars, err := parseVars(c)
if err != nil {
return err
}
varFiles, err := parseVarFilesFlag(c.StringSlice("var-file"))
if err != nil {
return err
}
for k, v := range varFiles {
vars[k] = v
}
opts.Vars = vars

return nil
Expand Down
9 changes: 1 addition & 8 deletions pkg/cmd/hide.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,10 @@ func parseHideOptions(opts *option.HideOptions, c *cli.Context) error {
opts.Condition = c.String("condition")
opts.SHA1 = c.String("sha1")

vars, err := parseVarsFlag(c.StringSlice("var"))
vars, err := parseVars(c)
if err != nil {
return err
}
varFiles, err := parseVarFilesFlag(c.StringSlice("var-file"))
if err != nil {
return err
}
for k, v := range varFiles {
vars[k] = v
}
opts.Vars = vars

return nil
Expand Down
70 changes: 70 additions & 0 deletions pkg/cmd/parse_vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"

"github.com/urfave/cli/v2"
)

func parseVarEnvs() map[string]string {
m := map[string]string{}
for _, kv := range os.Environ() {
k, v, _ := strings.Cut(kv, "=")
if a := strings.TrimPrefix(k, "GH_COMMENT_VAR_"); k != a {
m[a] = v
}
}
return m
}

func parseVarsFlag(varsSlice []string) (map[string]string, error) {
vars := make(map[string]string, len(varsSlice))
for _, v := range varsSlice {
a := strings.SplitN(v, ":", 2) //nolint:gomnd
if len(a) < 2 { //nolint:gomnd
return nil, errors.New("invalid var flag. The format should be '--var <key>:<value>")
}
vars[a[0]] = a[1]
}
return vars, nil
}

func parseVarFilesFlag(varsSlice []string) (map[string]string, error) {
vars := make(map[string]string, len(varsSlice))
for _, v := range varsSlice {
a := strings.SplitN(v, ":", 2) //nolint:gomnd
if len(a) < 2 { //nolint:gomnd
return nil, errors.New("invalid var flag. The format should be '--var <key>:<value>")
}
name := a[0]
filePath := a[1]
b, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("read the value of the variable %s from the file %s: %w", name, filePath, err)
}
vars[name] = string(b)
}
return vars, nil
}

func parseVars(c *cli.Context) (map[string]string, error) {
vars := parseVarEnvs()
flagVars, err := parseVarsFlag(c.StringSlice("var"))
if err != nil {
return nil, err
}
for k, v := range flagVars {
vars[k] = v
}
varFiles, err := parseVarFilesFlag(c.StringSlice("var-file"))
if err != nil {
return nil, err
}
for k, v := range varFiles {
vars[k] = v
}
return vars, nil
}
43 changes: 3 additions & 40 deletions pkg/cmd/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/sirupsen/logrus"
"github.com/suzuki-shunsuke/github-comment/pkg/api"
Expand All @@ -20,36 +18,6 @@ import (
"golang.org/x/term"
)

func parseVarsFlag(varsSlice []string) (map[string]string, error) {
vars := make(map[string]string, len(varsSlice))
for _, v := range varsSlice {
a := strings.SplitN(v, ":", 2) //nolint:gomnd
if len(a) < 2 { //nolint:gomnd
return nil, errors.New("invalid var flag. The format should be '--var <key>:<value>")
}
vars[a[0]] = a[1]
}
return vars, nil
}

func parseVarFilesFlag(varsSlice []string) (map[string]string, error) {
vars := make(map[string]string, len(varsSlice))
for _, v := range varsSlice {
a := strings.SplitN(v, ":", 2) //nolint:gomnd
if len(a) < 2 { //nolint:gomnd
return nil, errors.New("invalid var flag. The format should be '--var <key>:<value>")
}
name := a[0]
filePath := a[1]
b, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("read the value of the variable %s from the file %s: %w", name, filePath, err)
}
vars[name] = string(b)
}
return vars, nil
}

// parsePostOptions parses the command line arguments of the subcommand "post".
func parsePostOptions(opts *option.PostOptions, c *cli.Context) error {
opts.Org = c.String("org")
Expand All @@ -66,18 +34,13 @@ func parsePostOptions(opts *option.PostOptions, c *cli.Context) error {
opts.StdinTemplate = c.Bool("stdin-template")
opts.LogLevel = c.String("log-level")
opts.UpdateCondition = c.String("update-condition")
vars, err := parseVarsFlag(c.StringSlice("var"))
if err != nil {
return err
}
varFiles, err := parseVarFilesFlag(c.StringSlice("var-file"))

vars, err := parseVars(c)
if err != nil {
return err
}
for k, v := range varFiles {
vars[k] = v
}
opts.Vars = vars

return nil
}

Expand Down

0 comments on commit e5fd429

Please sign in to comment.