From dae03cff833655a85d6663b1872741731b3d3b39 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kalynovskyi Date: Tue, 9 Mar 2021 17:05:57 +0000 Subject: [PATCH] Add WorkDir method to airshipctl config The method also creates a directory if it doesn't exist. Relates-To: #480 Closes: #480 Change-Id: I240b963c0b850bb3965396473898eb23df7c5a62 --- pkg/config/config.go | 10 ++++++++++ pkg/config/config_test.go | 8 ++++++++ pkg/phase/helper.go | 11 ++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index dd1da132c..8bf164ea8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -583,3 +583,13 @@ func (c *Config) CurrentContextManifestMetadata() (*Metadata, error) { } return meta, nil } + +// WorkDir returns working directory for airshipctl. Creates if it doesn't exist +func (c *Config) WorkDir() (dir string, err error) { + dir = filepath.Join(util.UserHomeDir(), AirshipConfigDir) + // if not dir, create it + if !c.fileSystem.IsDir(dir) { + err = c.fileSystem.MkdirAll(dir) + } + return dir, err +} diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 211334357..ccbb10e09 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -541,3 +541,11 @@ func TestModifyEncryptionConfigs(t *testing.T) { conf.ModifyEncryptionConfig(encryptionConfig, eco) assert.Equal(t, eco.DecryptionKeyPath, modifiedConfig.DecryptionKeyPath) } + +func TestWorkDir(t *testing.T) { + conf, cleanup := testutil.InitConfig(t) + defer cleanup(t) + wd, err := conf.WorkDir() + assert.NoError(t, err) + assert.NotEmpty(t, wd) +} diff --git a/pkg/phase/helper.go b/pkg/phase/helper.go index 5f84377ba..4b8a83890 100644 --- a/pkg/phase/helper.go +++ b/pkg/phase/helper.go @@ -28,7 +28,6 @@ import ( inventoryifc "opendev.org/airship/airshipctl/pkg/inventory/ifc" "opendev.org/airship/airshipctl/pkg/log" "opendev.org/airship/airshipctl/pkg/phase/ifc" - "opendev.org/airship/airshipctl/pkg/util" ) // Helper provides functions built around phase bundle to filter and build documents @@ -41,11 +40,14 @@ type Helper struct { inventory inventoryifc.Inventory metadata *config.Metadata + config *config.Config } // NewHelper constructs metadata interface based on config func NewHelper(cfg *config.Config) (ifc.Helper, error) { - helper := &Helper{} + helper := &Helper{ + config: cfg, + } var err error helper.targetPath, err = cfg.CurrentContextTargetPath() @@ -321,10 +323,9 @@ func (helper *Helper) PhaseEntryPointBasePath() string { return helper.phaseEntryPointBasePath } -// WorkDir return manifest root -// TODO add creation of WorkDir if it doesn't exist +// WorkDir return working directory for aisrhipctl, creates it, if doesn't exist func (helper *Helper) WorkDir() (string, error) { - return filepath.Join(util.UserHomeDir(), config.AirshipConfigDir), nil + return helper.config.WorkDir() } // Inventory return inventory interface