Skip to content

Commit becd539

Browse files
committed
Send the settings into each command.
This will allows external commands to be able to see internal settings. This will be important when more commands begin to need shared settings, e.g. kubernetes configuration This also has the downside of causing a compatibility issue - backwards compatibility changes to AirshipCTLSettings will need to wait for each major version upgrade
1 parent d75fac8 commit becd539

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

cmd/plugins.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"github.com/ian-howell/airshipctl/cmd/workflow"
99

1010
// "github.com/ian-howell/exampleplugin"
11+
12+
"github.com/ian-howell/airshipctl/pkg/environment"
1113
)
1214

1315
// pluginCommands are the functions that create the entrypoint command for a plugin
14-
var pluginCommands = []func(io.Writer, []string) *cobra.Command{
16+
var pluginCommands = []func(io.Writer, *environment.AirshipCTLSettings, []string) *cobra.Command{
1517
// exampleplugin.NewExampleCommand, // This is an example and shouldn't be enabled in production builds
1618
workflow.NewWorkflowCommand,
1719
}

cmd/root.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/ian-howell/airshipctl/pkg/log"
1212
)
1313

14-
var settings environment.AirshipCTLSettings
1514

1615
// NewRootCmd creates the root `airshipctl` command. All other commands are
1716
// subcommands branching from this one
@@ -23,15 +22,17 @@ func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) {
2322
rootCmd.SetOutput(out)
2423

2524
// Settings flags - This section should probably be moved to pkg/environment
26-
rootCmd.PersistentFlags().BoolVar(&settings.Debug, "debug", false, "enable verbose output")
25+
settings := &environment.AirshipCTLSettings{}
26+
settings.InitFlags(rootCmd)
2727

2828
rootCmd.AddCommand(NewVersionCommand(out))
2929

30-
loadPluginCommands(rootCmd, out, args)
30+
loadPluginCommands(rootCmd, out, settings, args)
3131

3232
rootCmd.PersistentFlags().Parse(args)
3333

34-
log.Init(&settings, out)
34+
log.Init(settings, out)
35+
3536

3637
return rootCmd, nil
3738
}
@@ -50,8 +51,8 @@ func Execute(out io.Writer) {
5051
}
5152

5253
// loadPluginCommands loads all of the plugins as subcommands to cmd
53-
func loadPluginCommands(cmd *cobra.Command, out io.Writer, args []string) {
54+
func loadPluginCommands(cmd *cobra.Command, out io.Writer, settings *environment.AirshipCTLSettings, args []string) {
5455
for _, subcmd := range pluginCommands {
55-
cmd.AddCommand(subcmd(out, args))
56+
cmd.AddCommand(subcmd(out, settings, args))
5657
}
5758
}

cmd/workflow/workflow.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,20 @@ import (
44
"io"
55

66
"github.com/spf13/cobra"
7-
)
87

9-
var (
10-
kubeConfigFilePath string
11-
namespace string
8+
"github.com/ian-howell/airshipctl/pkg/environment"
129
)
1310

1411
// NewWorkflowCommand creates a new command for working with argo workflows
15-
func NewWorkflowCommand(out io.Writer, args []string) *cobra.Command {
12+
func NewWorkflowCommand(out io.Writer, settings *environment.AirshipCTLSettings, args []string) *cobra.Command {
1613
workflowRootCmd := &cobra.Command{
1714
Use: "workflow",
1815
Short: "Access to argo workflows",
1916
Aliases: []string{"workflows", "wf"},
2017
}
2118

22-
workflowRootCmd.PersistentFlags().StringVar(&kubeConfigFilePath, "kubeconfig", "", "path to kubeconfig")
23-
workflowRootCmd.PersistentFlags().StringVar(&namespace, "namespace", "default", "kubernetes namespace to use for the context of this command")
24-
25-
workflowRootCmd.AddCommand(NewWorkflowInitCommand(out, args))
26-
workflowRootCmd.AddCommand(NewWorkflowListCommand(out, args))
19+
workflowRootCmd.AddCommand(NewWorkflowInitCommand(out, settings, args))
20+
workflowRootCmd.AddCommand(NewWorkflowListCommand(out, settings, args))
2721

2822
return workflowRootCmd
2923
}

cmd/workflow/workflow_init.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"k8s.io/client-go/kubernetes"
1616
"k8s.io/client-go/rest"
1717
"k8s.io/client-go/tools/clientcmd"
18+
19+
"github.com/ian-howell/airshipctl/pkg/environment"
1820
)
1921

2022
const (
@@ -32,18 +34,18 @@ type workflowInitCmd struct {
3234
}
3335

3436
// NewWorkflowInitCommand is a command for bootstrapping a kubernetes cluster with the necessary components for Argo workflows
35-
func NewWorkflowInitCommand(out io.Writer, args []string) *cobra.Command {
37+
func NewWorkflowInitCommand(out io.Writer, settings *environment.AirshipCTLSettings, args []string) *cobra.Command {
3638
workflowInit := &workflowInitCmd{
3739
out: out,
3840
}
3941
workflowInitCommand := &cobra.Command{
4042
Use: "init [flags]",
4143
Short: "bootstraps the kubernetes cluster with the Workflow CRDs and controller",
4244
Run: func(cmd *cobra.Command, args []string) {
43-
if kubeConfigFilePath == "" {
44-
kubeConfigFilePath = clientcmd.RecommendedHomeFile
45+
if settings.KubeConfigFilePath == "" {
46+
settings.KubeConfigFilePath = clientcmd.RecommendedHomeFile
4547
}
46-
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigFilePath)
48+
config, err := clientcmd.BuildConfigFromFlags("", settings.KubeConfigFilePath)
4749
if err != nil {
4850
fmt.Fprintf(out, "Could not create kubernetes config: %s\n", err.Error())
4951
return

cmd/workflow/workflow_list.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ import (
99
"github.com/spf13/cobra"
1010
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
"k8s.io/client-go/tools/clientcmd"
12+
13+
"github.com/ian-howell/airshipctl/pkg/environment"
1214
)
1315

1416
// NewWorkflowListCommand is a command for listing argo workflows
15-
func NewWorkflowListCommand(out io.Writer, args []string) *cobra.Command {
17+
func NewWorkflowListCommand(out io.Writer, settings *environment.AirshipCTLSettings, args []string) *cobra.Command {
1618
workflowListCmd := &cobra.Command{
1719
Use: "list",
1820
Short: "list workflows",
1921
Aliases: []string{"ls"},
2022
Run: func(cmd *cobra.Command, args []string) {
21-
if kubeConfigFilePath == "" {
22-
kubeConfigFilePath = clientcmd.RecommendedHomeFile
23+
if settings.KubeConfigFilePath == "" {
24+
settings.KubeConfigFilePath = clientcmd.RecommendedHomeFile
2325
}
24-
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigFilePath)
26+
config, err := clientcmd.BuildConfigFromFlags("", settings.KubeConfigFilePath)
2527
if err != nil {
2628
panic(err.Error())
2729
}
@@ -31,7 +33,7 @@ func NewWorkflowListCommand(out io.Writer, args []string) *cobra.Command {
3133
panic(err.Error())
3234
}
3335

34-
wflist, err := clientSet.Workflows(namespace).List(v1.ListOptions{})
36+
wflist, err := clientSet.Workflows(settings.Namespace).List(v1.ListOptions{})
3537
if err != nil {
3638
panic(err.Error())
3739
}

pkg/environment/settings.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package environment
22

33
import (
44
restclient "k8s.io/client-go/rest"
5+
6+
"github.com/spf13/cobra"
57
)
68

79
// AirshipCTLSettings is a container for all of the settings needed by airshipctl
@@ -15,6 +17,17 @@ type AirshipCTLSettings struct {
1517
// with the cluster
1618
KubeConfig *restclient.Config
1719

20+
// Namespace is the kubernetes namespace to be used during the context of this action
21+
Namespace string
22+
1823
// Debug is used for verbose output
1924
Debug bool
2025
}
26+
27+
// InitFlags adds the default settings flags to cmd
28+
func (a *AirshipCTLSettings) InitFlags(cmd *cobra.Command) {
29+
flags := cmd.PersistentFlags()
30+
flags.BoolVar(&a.Debug, "debug", false, "enable verbose output")
31+
flags.StringVar(&a.KubeConfigFilePath, "kubeconfig", "", "path to kubeconfig")
32+
flags.StringVar(&a.Namespace, "namespace", "default", "kubernetes namespace to use for the context of this command")
33+
}

0 commit comments

Comments
 (0)