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
changes/42/779542/2
Kostiantyn Kalynovskyi 2021-03-09 17:05:57 +00:00
parent f0523a3808
commit dae03cff83
3 changed files with 24 additions and 5 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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