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
This commit is contained in:
Ian Howell
2019-05-21 11:04:05 -05:00
parent d75fac8e35
commit becd53903c
6 changed files with 40 additions and 26 deletions

View File

@@ -8,10 +8,12 @@ import (
"github.com/ian-howell/airshipctl/cmd/workflow"
// "github.com/ian-howell/exampleplugin"
"github.com/ian-howell/airshipctl/pkg/environment"
)
// pluginCommands are the functions that create the entrypoint command for a plugin
var pluginCommands = []func(io.Writer, []string) *cobra.Command{
var pluginCommands = []func(io.Writer, *environment.AirshipCTLSettings, []string) *cobra.Command{
// exampleplugin.NewExampleCommand, // This is an example and shouldn't be enabled in production builds
workflow.NewWorkflowCommand,
}

View File

@@ -11,7 +11,6 @@ import (
"github.com/ian-howell/airshipctl/pkg/log"
)
var settings environment.AirshipCTLSettings
// NewRootCmd creates the root `airshipctl` command. All other commands are
// subcommands branching from this one
@@ -23,15 +22,17 @@ func NewRootCmd(out io.Writer, args []string) (*cobra.Command, error) {
rootCmd.SetOutput(out)
// Settings flags - This section should probably be moved to pkg/environment
rootCmd.PersistentFlags().BoolVar(&settings.Debug, "debug", false, "enable verbose output")
settings := &environment.AirshipCTLSettings{}
settings.InitFlags(rootCmd)
rootCmd.AddCommand(NewVersionCommand(out))
loadPluginCommands(rootCmd, out, args)
loadPluginCommands(rootCmd, out, settings, args)
rootCmd.PersistentFlags().Parse(args)
log.Init(&settings, out)
log.Init(settings, out)
return rootCmd, nil
}
@@ -50,8 +51,8 @@ func Execute(out io.Writer) {
}
// loadPluginCommands loads all of the plugins as subcommands to cmd
func loadPluginCommands(cmd *cobra.Command, out io.Writer, args []string) {
func loadPluginCommands(cmd *cobra.Command, out io.Writer, settings *environment.AirshipCTLSettings, args []string) {
for _, subcmd := range pluginCommands {
cmd.AddCommand(subcmd(out, args))
cmd.AddCommand(subcmd(out, settings, args))
}
}

View File

@@ -4,26 +4,20 @@ import (
"io"
"github.com/spf13/cobra"
)
var (
kubeConfigFilePath string
namespace string
"github.com/ian-howell/airshipctl/pkg/environment"
)
// NewWorkflowCommand creates a new command for working with argo workflows
func NewWorkflowCommand(out io.Writer, args []string) *cobra.Command {
func NewWorkflowCommand(out io.Writer, settings *environment.AirshipCTLSettings, args []string) *cobra.Command {
workflowRootCmd := &cobra.Command{
Use: "workflow",
Short: "Access to argo workflows",
Aliases: []string{"workflows", "wf"},
}
workflowRootCmd.PersistentFlags().StringVar(&kubeConfigFilePath, "kubeconfig", "", "path to kubeconfig")
workflowRootCmd.PersistentFlags().StringVar(&namespace, "namespace", "default", "kubernetes namespace to use for the context of this command")
workflowRootCmd.AddCommand(NewWorkflowInitCommand(out, args))
workflowRootCmd.AddCommand(NewWorkflowListCommand(out, args))
workflowRootCmd.AddCommand(NewWorkflowInitCommand(out, settings, args))
workflowRootCmd.AddCommand(NewWorkflowListCommand(out, settings, args))
return workflowRootCmd
}

View File

@@ -15,6 +15,8 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"github.com/ian-howell/airshipctl/pkg/environment"
)
const (
@@ -32,7 +34,7 @@ type workflowInitCmd struct {
}
// NewWorkflowInitCommand is a command for bootstrapping a kubernetes cluster with the necessary components for Argo workflows
func NewWorkflowInitCommand(out io.Writer, args []string) *cobra.Command {
func NewWorkflowInitCommand(out io.Writer, settings *environment.AirshipCTLSettings, args []string) *cobra.Command {
workflowInit := &workflowInitCmd{
out: out,
}
@@ -40,10 +42,10 @@ func NewWorkflowInitCommand(out io.Writer, args []string) *cobra.Command {
Use: "init [flags]",
Short: "bootstraps the kubernetes cluster with the Workflow CRDs and controller",
Run: func(cmd *cobra.Command, args []string) {
if kubeConfigFilePath == "" {
kubeConfigFilePath = clientcmd.RecommendedHomeFile
if settings.KubeConfigFilePath == "" {
settings.KubeConfigFilePath = clientcmd.RecommendedHomeFile
}
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigFilePath)
config, err := clientcmd.BuildConfigFromFlags("", settings.KubeConfigFilePath)
if err != nil {
fmt.Fprintf(out, "Could not create kubernetes config: %s\n", err.Error())
return

View File

@@ -9,19 +9,21 @@ import (
"github.com/spf13/cobra"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"github.com/ian-howell/airshipctl/pkg/environment"
)
// NewWorkflowListCommand is a command for listing argo workflows
func NewWorkflowListCommand(out io.Writer, args []string) *cobra.Command {
func NewWorkflowListCommand(out io.Writer, settings *environment.AirshipCTLSettings, args []string) *cobra.Command {
workflowListCmd := &cobra.Command{
Use: "list",
Short: "list workflows",
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
if kubeConfigFilePath == "" {
kubeConfigFilePath = clientcmd.RecommendedHomeFile
if settings.KubeConfigFilePath == "" {
settings.KubeConfigFilePath = clientcmd.RecommendedHomeFile
}
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigFilePath)
config, err := clientcmd.BuildConfigFromFlags("", settings.KubeConfigFilePath)
if err != nil {
panic(err.Error())
}
@@ -31,7 +33,7 @@ func NewWorkflowListCommand(out io.Writer, args []string) *cobra.Command {
panic(err.Error())
}
wflist, err := clientSet.Workflows(namespace).List(v1.ListOptions{})
wflist, err := clientSet.Workflows(settings.Namespace).List(v1.ListOptions{})
if err != nil {
panic(err.Error())
}