Skip to content

Commit

Permalink
Generate a default config.json if one is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
ody-ext-ccasteel committed Sep 16, 2023
1 parent d203b34 commit 164e5aa
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,46 @@ func getClient() (*gmail.Service, error) {
func readConfig() Configuration {
configFile, err := os.Open("config.json")
if err != nil {
log.Fatal(err)
if os.IsNotExist(err) {
// Create a default config.json file if it doesn't exist
defaultConfig := Configuration{
SteamCmdPath: "./steamcmd/steamcmd.exe",
}
err := createDefaultConfigFile(defaultConfig)
if err != nil {
log.Fatalf("Error creating default config file: %v", err)
}
return defaultConfig
}
log.Fatalf("Error opening config file: %v", err)
}
defer configFile.Close()

decoder := json.NewDecoder(configFile)
config := Configuration{}
err = decoder.Decode(&config)
if err != nil {
log.Fatal(err)
log.Fatalf("Error decoding config file: %v", err)
}
return config
}

func createDefaultConfigFile(config Configuration) error {
configFile, err := os.Create("config.json")
if err != nil {
return err
}
defer configFile.Close()

encoder := json.NewEncoder(configFile)
encoder.SetIndent("", " ")
err = encoder.Encode(config)
if err != nil {
return err
}
return nil
}

func fetchSteamGuardCode(srv *gmail.Service) string {
user := "me"
query := "from:[email protected] is:unread"
Expand Down

0 comments on commit 164e5aa

Please sign in to comment.