create config dir if it doesn't exist

This commit is contained in:
Maidul Islam
2022-12-25 16:02:14 -05:00
parent 8c10ae78f5
commit 78d4fb2300
3 changed files with 16 additions and 3 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ var vaultSetCmd = &cobra.Command{
err = util.WriteConfigFile(&configFile) err = util.WriteConfigFile(&configFile)
if err != nil { if err != nil {
log.Errorf("Unable to set vault to [%s] because an error occurred when saving the config file [err=%s]") log.Errorf("Unable to set vault to [%s] because an error occurred when saving the config file [err=%s]", wantedVaultTypeName, err)
return return
} }
+1
View File
@@ -19,6 +19,7 @@ func GetHomeDir() (string, error) {
return directory, err return directory, err
} }
// write file to given path. If path does not exist throw error
func WriteToFile(fileName string, dataToWrite []byte, filePerm os.FileMode) error { func WriteToFile(fileName string, dataToWrite []byte, filePerm os.FileMode) error {
err := os.WriteFile(fileName, dataToWrite, filePerm) err := os.WriteFile(fileName, dataToWrite, filePerm)
if err != nil { if err != nil {
+14 -2
View File
@@ -187,7 +187,7 @@ func GetConfigFile() (models.ConfigFile, error) {
// Write a ConfigFile to disk. Raise error if unable to save the model to ask // Write a ConfigFile to disk. Raise error if unable to save the model to ask
func WriteConfigFile(configFile *models.ConfigFile) error { func WriteConfigFile(configFile *models.ConfigFile) error {
fullConfigFilePath, _, err := GetFullConfigFilePath() fullConfigFilePath, fullConfigFileDirPath, err := GetFullConfigFilePath()
if err != nil { if err != nil {
return fmt.Errorf("writeConfigFile: unable to write config file because an error occurred when getting config file path [err=%s]", err) return fmt.Errorf("writeConfigFile: unable to write config file because an error occurred when getting config file path [err=%s]", err)
} }
@@ -197,8 +197,20 @@ func WriteConfigFile(configFile *models.ConfigFile) error {
return fmt.Errorf("writeConfigFile: unable to write config file because an error occurred when marshalling the config file [err=%s]", err) return fmt.Errorf("writeConfigFile: unable to write config file because an error occurred when marshalling the config file [err=%s]", err)
} }
// check if config folder exists and if not create it
if _, err := os.Stat(fullConfigFileDirPath); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(fullConfigFileDirPath, os.ModePerm)
if err != nil {
return err
}
}
// Create file in directory // Create file in directory
err = WriteToFile(fullConfigFilePath, configFileMarshalled, os.ModePerm) err = os.WriteFile(fullConfigFilePath, configFileMarshalled, os.ModePerm)
if err != nil {
return fmt.Errorf("writeConfigFile: Unable to write to file [err=%s]", err)
}
if err != nil { if err != nil {
return fmt.Errorf("writeConfigFile: unable to write config file because an error occurred when write the config to file [err=%s]", err) return fmt.Errorf("writeConfigFile: unable to write config file because an error occurred when write the config to file [err=%s]", err)