From 6af3da8d7b78da442f31d233ed1ff24c545c2bbe Mon Sep 17 00:00:00 2001 From: Matt McEuen Date: Mon, 22 Jun 2020 17:50:01 -0500 Subject: [PATCH] Remove config file requirement for plugin command Change 725820 [1] configured airshipctl to fail an airship config file isn't present. The present change makes an exception to that behavior for the document plugin subcommand, and moves the plugin loading from the parent document command into the pull subcommand. There are multiple reasons we don't want config enforcement in place for plugins: - If a non-standard config file location is configured via an environment variable, the plugin won't know about it (since it's invoked separately by kustomize), and it would look for a potentially non-existant default config file, and fail. - If a user is using the kustomize cli by hand along with airshipctl plugins (as opposed to driving the process via airshipctl itself), then they do not need the config anyway -- the plugin subcommand doesn't actually use it. Forcing the user to create a config file seems awkward here. [1]: https://review.opendev.org/#/c/725820/16 Change-Id: Ic1b652efb14439ed2757eb9fb0a86feb8b3ef21c --- cmd/document/document.go | 7 +++---- cmd/document/pull.go | 8 +++++++- cmd/document/pull_test.go | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/document/document.go b/cmd/document/document.go index 49132548a..1cdaf7029 100644 --- a/cmd/document/document.go +++ b/cmd/document/document.go @@ -27,14 +27,13 @@ func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com Use: "document", Short: "Manage deployment documents", PersistentPreRun: func(cmd *cobra.Command, args []string) { + // Note: config is not loaded here; the kustomize plugin command doesn't + // require it, and multiple use cases fail if we assume the file is there. log.Init(rootSettings.Debug, cmd.OutOrStderr()) - - // Load or Initialize airship Config - rootSettings.InitConfig() }, } - documentRootCmd.AddCommand(NewPullCommand(rootSettings)) + documentRootCmd.AddCommand(NewPullCommand(rootSettings, true)) documentRootCmd.AddCommand(NewPluginCommand(rootSettings)) return documentRootCmd diff --git a/cmd/document/pull.go b/cmd/document/pull.go index a14712d26..9dd9c1a2e 100644 --- a/cmd/document/pull.go +++ b/cmd/document/pull.go @@ -22,12 +22,18 @@ import ( ) // NewPullCommand creates a new command for pulling airship document repositories -func NewPullCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { +// initConfig determines whether it's appropriate to load configuration from +// disk; e.g. this is skipped when unit testing the command. +func NewPullCommand(rootSettings *environment.AirshipCTLSettings, initConfig bool) *cobra.Command { settings := pull.Settings{AirshipCTLSettings: rootSettings} documentPullCmd := &cobra.Command{ Use: "pull", Short: "Pulls documents from remote git repository", RunE: func(cmd *cobra.Command, args []string) error { + if initConfig { + // Load or Initialize airship Config + rootSettings.InitConfig() + } return settings.Pull() }, } diff --git a/cmd/document/pull_test.go b/cmd/document/pull_test.go index 58d48f070..e55cfa07e 100644 --- a/cmd/document/pull_test.go +++ b/cmd/document/pull_test.go @@ -50,7 +50,7 @@ func TestPull(t *testing.T) { { Name: "document-pull-cmd", CmdLine: "", - Cmd: NewPullCommand(getDummyAirshipSettings()), + Cmd: NewPullCommand(getDummyAirshipSettings(), false), }, }