From 164e5aaef2118fac5638ed127e385a9f0d652b4c Mon Sep 17 00:00:00 2001 From: ody-ext-ccasteel Date: Sat, 16 Sep 2023 19:09:19 -0400 Subject: [PATCH] Generate a default config.json if one is not present --- main.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 0b93c52..c607846 100644 --- a/main.go +++ b/main.go @@ -137,7 +137,18 @@ 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() @@ -145,11 +156,27 @@ func readConfig() Configuration { 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:noreply@steampowered.com is:unread"