Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 60367c0

Browse files
committed
refactor: automatic GitHub env var handling
1 parent 8947f8b commit 60367c0

File tree

5 files changed

+48
-53
lines changed

5 files changed

+48
-53
lines changed

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ branding:
77
color: orange
88

99
inputs:
10-
in:
10+
args:
1111
description: input file name
1212
required: true
1313

cmd/action.go

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,57 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9+
10+
"github.com/google/shlex"
11+
"github.com/spf13/pflag"
912
)
1013

1114
// AddGitHubArgs adds GitHub input parameters as arguments.
12-
func AddGitHubArgs(args []string) []string {
15+
func AddGitHubArgs(args []string, flags *pflag.FlagSet) ([]string, error) {
1316
if !isGitHubAction() {
14-
return args
17+
return args, nil
1518
}
1619

17-
args = ghinput(args, "distro_name", "--distro-name")
18-
args = ghinput(args, "distro_version", "--distro-version")
19-
args = ghinput(args, "executable", "--executable")
20-
args = ghinput(args, "archive", "--archive")
21-
args = ghinput(args, "docker", "--docker")
22-
args = ghinput(args, "docker_template", "--docker-template")
23-
args = ghinput(args, "notes", "--notes")
24-
args = ghinput(args, "notes_template", "--notes-template")
25-
args = ghinput(args, "notes_latest", "--notes-latest")
26-
args = ghinput(args, "readme", "--readme")
27-
args = ghinput(args, "license", "--license")
28-
args = ghinput(args, "platform", "--platform")
29-
30-
if getenv("INPUT_VERBOSE", "false") == "true" {
31-
args = append(args, "--verbose")
32-
}
20+
flags.Visit(func(flag *pflag.Flag) {
21+
args = ghinput(args, flag)
22+
})
3323

34-
if getenv("INPUT_QUIET", "false") == "true" {
35-
args = append(args, "--quiet")
36-
}
24+
if iargs := os.Getenv("INPUT_ARGS"); len(iargs) > 0 { //nolint:forbidigo
25+
items, err := shlex.Split(iargs)
26+
if err != nil {
27+
return nil, err
28+
}
3729

38-
if in := getenv("INPUT_IN", ""); len(in) > 0 {
39-
args = append(args, in)
30+
args = append(args, items...)
4031
}
4132

42-
return args
33+
return args, nil
4334
}
4435

4536
//nolint:forbidigo
4637
func isGitHubAction() bool {
4738
return os.Getenv("GITHUB_ACTIONS") == "true"
4839
}
4940

50-
//nolint:forbidigo
51-
func getenv(name string, defval string) string {
52-
value, found := os.LookupEnv(name)
53-
if found {
54-
return value
55-
}
56-
57-
return defval
58-
}
59-
60-
func ghinput(args []string, name string, flag string) []string {
61-
val := getenv("INPUT_"+strings.ToUpper(name), "")
62-
if len(val) > 0 {
63-
args = append(args, flag, val)
41+
func ghinput(args []string, flag *pflag.Flag) []string {
42+
name := "INPUT_" + strings.ToUpper(strings.ReplaceAll(flag.Name, "-", "_"))
43+
44+
if val := os.Getenv(name); len(val) > 0 { //nolint:forbidigo
45+
if flag.Value.Type() == "bool" {
46+
if val == "true" {
47+
args = append(args, "--"+flag.Name)
48+
}
49+
} else {
50+
args = append(args, "--"+flag.Name, val)
51+
}
6452
}
6553

6654
return args
6755
}
6856

6957
//nolint:forbidigo
7058
func emitOutput(changed bool, version string) error {
71-
ghOutput := getenv("GITHUB_OUTPUT", "")
59+
ghOutput := os.Getenv("GITHUB_OUTPUT")
7260
if len(ghOutput) == 0 {
7361
return nil
7462
}

cmd/k6dist/main.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,29 @@ func initLogging() *slog.LevelVar {
3030
}
3131

3232
func main() {
33-
runCmd(newCmd(getArgs(), initLogging()))
33+
root, err := newCmd(os.Args[1:], initLogging()) //nolint:forbidigo
34+
checkErr(err)
35+
checkErr(root.Execute())
3436
}
3537

36-
func newCmd(args []string, levelVar *slog.LevelVar) *cobra.Command {
37-
cmd := cmd.New(levelVar)
38+
func newCmd(args []string, levelVar *slog.LevelVar) (*cobra.Command, error) {
39+
root := cmd.New(levelVar)
3840

39-
cmd.Version = version
40-
cmd.SetArgs(args)
41+
root.Version = version
4142

42-
return cmd
43+
args, err := cmd.AddGitHubArgs(args, root.Flags())
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
root.SetArgs(args)
49+
50+
return root, nil
4351
}
4452

45-
func runCmd(cmd *cobra.Command) {
46-
if err := cmd.Execute(); err != nil {
53+
func checkErr(err error) {
54+
if err != nil {
4755
slog.Error(err.Error())
4856
os.Exit(1) //nolint:forbidigo
4957
}
5058
}
51-
52-
func getArgs() []string {
53-
return cmd.AddGitHubArgs(os.Args[1:]) //nolint:forbidigo
54-
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ go 1.22.4
44

55
require (
66
github.com/go-task/slim-sprig/v3 v3.0.0
7+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
78
github.com/grafana/clireadme v0.1.0
89
github.com/grafana/k6foundry v0.2.0
910
github.com/samber/slog-logrus/v2 v2.5.0
1011
github.com/sirupsen/logrus v1.9.3
1112
github.com/spf13/cobra v1.8.1
13+
github.com/spf13/pflag v1.0.5
1214
)
1315

1416
require (
1517
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1618
github.com/samber/lo v1.44.0 // indirect
1719
github.com/samber/slog-common v0.17.0 // indirect
18-
github.com/spf13/pflag v1.0.5 // indirect
1920
golang.org/x/mod v0.17.0 // indirect
2021
golang.org/x/sys v0.1.0 // indirect
2122
golang.org/x/text v0.16.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
66
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
7+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
8+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
79
github.com/grafana/clireadme v0.1.0 h1:KYEYSnYdSzmHf3bufaK6fQZ5j4dzvM/T+G6Ba+qNnAM=
810
github.com/grafana/clireadme v0.1.0/go.mod h1:Wy4KIG2ZBGMYAYyF9l7qAy+yoJVasqk/txsRgoRI3gc=
911
github.com/grafana/k6foundry v0.2.0 h1:+aE5wuCP0XNGNsxM7UiPj9hyw4RdWeW929PuGwLWIlg=

0 commit comments

Comments
 (0)