Resolved missing configuration folder / file on initial run

This commit is contained in:
mitch 2022-02-25 20:11:25 -05:00
parent 8b24af46e1
commit 9d34e7f6f5
2 changed files with 40 additions and 5 deletions

View File

@ -18,6 +18,21 @@ const (
func main() { func main() {
homeDir, _ := os.UserHomeDir() homeDir, _ := os.UserHomeDir()
configDirString := homeDir + shortConfigDirString configDirString := homeDir + shortConfigDirString
_, err := os.Stat(configDirString)
if os.IsNotExist(err) {
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
}
_, err = os.Stat(configDirString + "/" + configFileLocation)
if os.IsNotExist(err) {
err = versionedTerraform.CreateConfig(configDirString, configFileLocation)
}
if err != nil {
fmt.Printf("Unable to create config directory: %v", err)
}
configDir := os.DirFS(configDirString) configDir := os.DirFS(configDirString)
workingDir := os.DirFS(pwd) workingDir := os.DirFS(pwd)
var versionsFromConfig []versionedTerraform.SemVersion var versionsFromConfig []versionedTerraform.SemVersion
@ -25,17 +40,23 @@ func main() {
flag.Parse() flag.Parse()
args := flag.Args() args := flag.Args()
versionsFromConfig, err = versionedTerraform.LoadVersionsFromConfig(configDir, configFileLocation)
if err != nil {
fmt.Printf("Unable to read config: %v", err)
os.Exit(1)
}
needsUpdate, err := versionedTerraform.NeedToUpdateAvailableVersions(configDir, configFileLocation) needsUpdate, err := versionedTerraform.NeedToUpdateAvailableVersions(configDir, configFileLocation)
if os.ErrNotExist == err {
fmt.Println("Unable to update version: %v", err)
}
if needsUpdate { if needsUpdate {
fileHandle, _ := os.OpenFile(configDirString+"/"+configFileLocation, os.O_RDWR, 0666) fileHandle, _ := os.OpenFile(configDirString+"/"+configFileLocation, os.O_RDWR, 0666)
defer fileHandle.Close() defer fileHandle.Close()
versionedTerraform.UpdateConfig(*fileHandle) versionedTerraform.UpdateConfig(*fileHandle)
} }
versionsFromConfig, err = versionedTerraform.LoadVersionsFromConfig(configDir, configFileLocation)
if err != nil {
fmt.Printf("Unable to read config: %v", err)
os.Exit(1)
}
installedVersions, err := versionedTerraform.LoadInstalledVersions(configDir) installedVersions, err := versionedTerraform.LoadInstalledVersions(configDir)
if err != nil { if err != nil {

View File

@ -109,3 +109,17 @@ func UpdateConfig(File os.File) error {
File.Write(lineToByte) File.Write(lineToByte)
return nil return nil
} }
func CreateConfig(directory string, configFile string) error {
configFileName := directory + "/" + configFile
err := os.MkdirAll(directory, 0755)
if err != nil {
return err
}
fileHandler, err := os.Create(configFileName)
defer fileHandler.Close()
err = UpdateConfig(*fileHandler)
return err
}