Skip to content

Commit 22f4361

Browse files
author
David Cavazos
committed
use variadic argument prints
1 parent 5012283 commit 22f4361

File tree

1 file changed

+36
-55
lines changed
  • .github/cloud-samples-tools/cmd

1 file changed

+36
-55
lines changed

.github/cloud-samples-tools/cmd/main.go

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,107 +19,88 @@ package main
1919
import (
2020
"cloud-samples-tools/pkg/utils"
2121
"encoding/json"
22+
"flag"
2223
"fmt"
2324
"log"
2425
"os"
2526
"strings"
2627
)
2728

29+
var usage = `usage: tools <command> ...
30+
31+
commands:
32+
affected path/to/config.jsonc path/to/diffs.txt
33+
run-all path/to/config.jsonc path/to/script.sh
34+
`
35+
2836
func main() {
29-
command := ""
30-
if len(os.Args) > 1 {
31-
command = os.Args[1]
32-
} else {
33-
fmt.Fprintf(os.Stderr, "❌ no command specified\n")
34-
printUsage(os.Stderr)
35-
os.Exit(1)
37+
flag.Parse()
38+
39+
command := flag.Arg(0)
40+
if command == "" {
41+
log.Fatalln("❌ no command specified\n", usage)
3642
}
3743

3844
switch command {
3945
case "affected":
40-
configFile := ""
41-
if len(os.Args) > 2 {
42-
configFile = os.Args[2]
43-
} else {
44-
fmt.Fprintf(os.Stderr, "❌ no config file specified\n")
45-
printUsage(os.Stderr)
46-
os.Exit(1)
46+
configFile := flag.Arg(1)
47+
if configFile == "" {
48+
log.Fatalln("❌ no config file specified\n", usage)
4749
}
4850

49-
diffsFile := ""
50-
if len(os.Args) > 3 {
51-
diffsFile = os.Args[3]
52-
} else {
53-
fmt.Fprintf(os.Stderr, "❌ no diffs file specified\n")
54-
printUsage(os.Stderr)
55-
os.Exit(1)
51+
diffsFile := flag.Arg(2)
52+
if diffsFile == "" {
53+
log.Fatalln("❌ no diffs file specified\n", usage)
5654
}
5755

5856
affectedCmd(configFile, diffsFile)
5957

6058
case "run-all":
61-
configFile := ""
62-
if len(os.Args) > 2 {
63-
configFile = os.Args[2]
64-
} else {
65-
fmt.Fprintf(os.Stderr, "❌ no config file specified\n")
66-
printUsage(os.Stderr)
67-
os.Exit(1)
59+
configFile := flag.Arg(1)
60+
if configFile == "" {
61+
log.Fatalln("❌ no config file specified\n", usage)
6862
}
6963

70-
script := ""
71-
if len(os.Args) > 3 {
72-
script = os.Args[3]
73-
} else {
74-
fmt.Fprintf(os.Stderr, "❌ no script file specified\n")
75-
printUsage(os.Stderr)
76-
os.Exit(1)
64+
script := flag.Arg(2)
65+
if script == "" {
66+
log.Fatalln("❌ no script file specified\n", usage)
7767
}
7868

7969
runAllCmd(configFile, script)
8070

8171
default:
82-
fmt.Fprintf(os.Stderr, "❌ unknown command: %s\n", command)
83-
printUsage(os.Stderr)
84-
os.Exit(1)
72+
log.Fatalln("❌ unknown command: ", command, "\n", usage)
8573
}
8674
}
8775

88-
func printUsage(f *os.File) {
89-
fmt.Fprintf(f, "usage: tools <command> ...\n")
90-
fmt.Fprintf(f, "\n")
91-
fmt.Fprintf(f, "commands:\n")
92-
fmt.Fprintf(f, " affected path/to/config.jsonc path/to/diffs.txt\n")
93-
fmt.Fprintf(f, " run-all path/to/config.jsonc path/to/script.sh\n")
94-
}
95-
9676
func affectedCmd(configFile string, diffsFile string) {
9777
config, err := utils.LoadConfig(configFile)
9878
if err != nil {
99-
log.Fatalf("❌ error loading the config file: %v\n%v\n", configFile, err)
79+
log.Fatalln("❌ error loading the config file: ", configFile, "\n", err)
10080
}
10181

10282
diffsBytes, err := os.ReadFile(diffsFile)
10383
if err != nil {
104-
log.Fatalf("❌ error getting the diffs: %v\n%v\n", diffsFile, err)
84+
log.Fatalln("❌ error getting the diffs: ", diffsFile, "\n", err)
10585
}
10686
diffs := strings.Split(string(diffsBytes), "\n")
10787

10888
packages, err := utils.Affected(config, diffs)
10989
if err != nil {
110-
log.Fatalf("❌ error finding the affected packages.\n%v\n", err)
90+
log.Fatalln("❌ error finding the affected packages.\n", err)
11191
}
11292
if len(packages) > 256 {
113-
log.Fatalf(
114-
"❌ Error: GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n",
93+
log.Fatalln(
94+
"❌ Error: GitHub Actions only supports up to 256 packages, got ",
11595
len(packages),
96+
" packages, for more details see:\n",
11697
"https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow",
11798
)
11899
}
119100

120101
packagesJson, err := json.Marshal(packages)
121102
if err != nil {
122-
log.Fatalf("❌ error marshaling packages to JSON.\n%v\n", err)
103+
log.Fatalln("❌ error marshaling packages to JSON.\n", err)
123104
}
124105

125106
fmt.Println(string(packagesJson))
@@ -128,12 +109,12 @@ func affectedCmd(configFile string, diffsFile string) {
128109
func runAllCmd(configFile string, script string) {
129110
config, err := utils.LoadConfig(configFile)
130111
if err != nil {
131-
log.Fatalf("❌ error loading the config file: %v\n%v\n", configFile, err)
112+
log.Fatalln("❌ error loading the config file: ", configFile, "\n", err)
132113
}
133114

134115
packages, err := utils.FindAllPackages(".", config)
135116
if err != nil {
136-
fmt.Fprintf(os.Stderr, "❌ error finding packages.\n%v\n", err)
117+
log.Fatalln("❌ error finding packages.\n", err)
137118
}
138119

139120
maxGoroutines := 16
@@ -144,6 +125,6 @@ func runAllCmd(configFile string, script string) {
144125
fmt.Printf("Failed tests: %v\n", failed)
145126

146127
if failed > 0 {
147-
log.Fatalf("❌ some tests failed, exit with code 1.")
128+
log.Fatalln("❌ some tests failed, exit with code 1.")
148129
}
149130
}

0 commit comments

Comments
 (0)