Skip to content

Commit 430920e

Browse files
feat(cmd): add Cobra framework (#90)
* feat(cmd): add Cobra framework This commit introduces the Cobra CLI framework, and reorganizes /cmd to adhere to Cobra-recommended standards. Moves primary functionality to the generate subcommand, and introduces the version subcommand, which will print the version/commit/date as injected by goreleaser at build time. * refactor(release): remove non-default main config Now that we have a root main.go per Cobra standards, we no longer require a non-default path to our main.go to be specified as the build entrypoint.
1 parent b2429ad commit 430920e

File tree

7 files changed

+473
-15
lines changed

7 files changed

+473
-15
lines changed

.goreleaser.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
builds:
2-
- main: ./cmd/kleat/main.go
2+
- ldflags:
3+
- -s -w -X cmd.version={{.Version}} -X cmd.commit={{.Commit}} -X cmd.date={{.Date}}

cmd/kleat/main.go renamed to cmd/generate.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,30 @@
1414
* limitations under the License.
1515
*/
1616

17-
package main
17+
package cmd
1818

1919
import (
20-
"fmt"
21-
"os"
22-
2320
"github.com/spinnaker/kleat/internal/fileio"
21+
22+
"github.com/spf13/cobra"
2423
)
2524

26-
func main() {
27-
if len(os.Args) != 3 {
28-
_, _ = fmt.Fprint(os.Stderr, "arguments must be <halPath> <dir>\n")
29-
os.Exit(1)
30-
}
25+
var generateCmd = &cobra.Command{
26+
Use: "generate",
27+
Short: "Generate Spinnaker microservice config files",
28+
Long: `Given a path to your top-level Spinnaker config (halconfig) and an
29+
output directory, writes each generated Spinnaker microservice config file to
30+
the output directory.
3131
32-
hal := os.Args[1]
33-
dir := os.Args[2]
34-
if err := writeServiceConfigs(hal, dir); err != nil {
35-
panic(err)
36-
}
32+
Example usage:
33+
34+
kleat /path/to/halconfig /path/to/output`,
35+
Args: cobra.ExactArgs(2),
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
hal := args[0]
38+
dir := args[1]
39+
return writeServiceConfigs(hal, dir)
40+
},
3741
}
3842

3943
func writeServiceConfigs(halPath string, dir string) error {
@@ -44,3 +48,7 @@ func writeServiceConfigs(halPath string, dir string) error {
4448

4549
return fileio.WriteConfigs(h, dir)
4650
}
51+
52+
func init() {
53+
rootCmd.AddCommand(generateCmd)
54+
}

cmd/root.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright The Spinnaker Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"os"
22+
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var rootCmd = &cobra.Command{
27+
Use: "kleat",
28+
Short: "Kleat is a tool for managing Spinnaker configuration files.",
29+
Long: `Kleat is a tool for generating configuration files for each Spinnaker
30+
microservice from one top-level Spinnaker configuration file. It is intended
31+
for use with the Spinnaker Kustomization base
32+
(https://github.com/spinnaker/kustomization-base)
33+
as an alternative to the Halyard-managed configuration and deployment
34+
experience. Please check out the README at https://github.com/spinnaker/kleat
35+
for more details.`,
36+
}
37+
38+
func Execute() {
39+
if err := rootCmd.Execute(); err != nil {
40+
fmt.Println(err)
41+
os.Exit(1)
42+
}
43+
}

cmd/version.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright The Spinnaker Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
)
22+
23+
// Dynamically inserted at build time by goreleaser. See `ldflags` in
24+
// .goreleaser.yml.
25+
var (
26+
version = "unknown"
27+
commit = "unknown"
28+
date = "unknown"
29+
)
30+
31+
var versionCmd = &cobra.Command{
32+
Use: "version",
33+
Short: "Print kleat version",
34+
Long: "Print the version, commit, and release date of your kleat",
35+
Run: func(cmd *cobra.Command, args []string) {
36+
cmd.Printf("Kleat has version %s built from %s on %s\n", version, commit, date)
37+
},
38+
}
39+
40+
func init() {
41+
rootCmd.AddCommand(versionCmd)
42+
}

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@ module github.com/spinnaker/kleat
33
go 1.14
44

55
require (
6+
github.com/fsnotify/fsnotify v1.4.9 // indirect
67
github.com/golang/protobuf v1.4.1
78
github.com/google/go-cmp v0.4.0
89
github.com/kylelemons/godebug v1.1.0
10+
github.com/mitchellh/mapstructure v1.3.2 // indirect
11+
github.com/pelletier/go-toml v1.8.0 // indirect
12+
github.com/spf13/afero v1.2.2 // indirect
13+
github.com/spf13/cast v1.3.1 // indirect
14+
github.com/spf13/cobra v1.0.0
15+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
16+
github.com/spf13/pflag v1.0.5 // indirect
17+
github.com/spf13/viper v1.7.0 // indirect
18+
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
919
google.golang.org/protobuf v1.22.0
20+
gopkg.in/ini.v1 v1.57.0 // indirect
1021
sigs.k8s.io/yaml v1.2.0
1122
)

0 commit comments

Comments
 (0)