package main import ( "concord.hectabit.org/Hectabit/burgerbackup/lib/client" "github.com/spf13/viper" "log" "os" "strconv" "strings" "time" ) func main() { var configPath = "./config.ini" if len(os.Args) > 1 { if os.Args[1] == "-h" || os.Args[1] == "--help" { log.Println("[INFO] Usage: burgerbackup-client ") os.Exit(0) } else { configPath = os.Args[1] } } if _, err := os.Stat(configPath); err == nil { log.Println("[INFO] Config loaded at", time.Now().Unix()) } else if os.IsNotExist(err) { originalConfigPath := configPath configPath = "/etc/burgerbackup/client.ini" if _, err := os.Stat(configPath); err == nil { log.Println("[INFO] Config loaded at", time.Now().Unix()) log.Println("[WARN] The config file was not found at", originalConfigPath, "so the default config path of", configPath, "was used.") } else if os.IsNotExist(err) { log.Fatalln("[FATAL]", originalConfigPath, "does not exist") } else { log.Fatalln("[FATAL] File is in quantum uncertainty:", err) } } else { log.Fatalln("[FATAL] File is in quantum uncertainty:", err) } viper.SetConfigType("ini") configPathSlice := strings.Split(configPath, "/") configPathNoIni := strings.Split(configPathSlice[len(configPathSlice)-1], ".")[0] viper.SetConfigName(configPathNoIni) configPathNoFile := strings.Join(configPathSlice[:len(configPathSlice)-1], "/") + "/" viper.AddConfigPath(configPathNoFile) viper.AutomaticEnv() err := viper.ReadInConfig() if err != nil { log.Fatalln("[FATAL] Error in config file at", strconv.FormatInt(time.Now().Unix(), 10)+":", err) } backupKey := viper.GetString("config.BACKUP_KEY") cryptoKey := viper.GetString("config.CRYPTO_KEY") backupInterval := viper.GetInt("config.BACKUP_INTERVAL") fileLocation := viper.GetString("config.FILE_LOCATION") remoteURL := viper.GetString("config.REMOTE_URL") for { err, errCode := client.PerformBackup(fileLocation, backupKey, cryptoKey, remoteURL) if err != nil { switch errCode { case 0: log.Println("[CRITICAL] Unknown in PerformBackup() file read:", err) case 1: log.Println("[CRITICAL] Unknown in PerformBackup() file stat:", err) case 2: log.Println("[CRITICAL] Unknown in PerformBackup() content marshal:", err) case 3: log.Println("[CRITICAL] Unknown in PerformBackup() content encryption:", err) case 4: log.Println("[CRITICAL] Unknown in SendFileToServer() form writer creation:", err) case 5: log.Println("[CRITICAL] Unknown in SendFileToServer() IO Copying:", err) case 6: log.Println("[CRITICAL] Unknown in SendFileToServer() writer closing:", err) case 7: log.Println("[CRITICAL] Unknown in SendFileToServer() request creation:", err) case 8: log.Println("[CRITICAL] Unknown in SendFileToServer() hash creation:", err) case 9: log.Println("[CRITICAL] Unknown in SendFileToServer() request execution:", err) case 10: log.Println("[CRITICAL] Unknown in SendFileToServer() response read:", err) case 11: log.Println("[CRITICAL] Unknown in SendFileToServer() response marshalling:", err) case 12: log.Println("[CRITICAL] Server sent message in SendFileToServer():", err) default: log.Println("[CRITICAL] Unknown error in main():", err) } } time.Sleep(time.Duration(backupInterval) * 1000000000) } }