Beispiel #1
0
func (config *Config) ConfigureSettingsDirectory() error {
	usr, err := user.Current()

	if err != nil {
		return err
	}

	config.SettingsDir = filepath.Join(usr.HomeDir, ".config/"+TITLE)

	if ok, _ := utils.Exists(config.SettingsDir); !ok {
		log.Println("Creating settings directory: ", config.SettingsDir)
		if err := os.Mkdir(config.SettingsDir, 0755); err != nil {
			return err
		}
	}

	config.SavesDir = filepath.Join(config.SettingsDir, "saves")

	if ok, _ := utils.Exists(config.SavesDir); !ok {
		log.Println("Creating saves directory: ", config.SavesDir)
		if err := os.Mkdir(config.SavesDir, 0755); err != nil {
			return err
		}
	}

	logPath := filepath.Join(config.SettingsDir, "log")
	logFile, err := os.Create(logPath)
	if err != nil {
		return err
	}

	log.SetOutput(logFile)

	return nil
}
Beispiel #2
0
func (c *Config) LoadConfig() error {
	settingsFilepath := filepath.Join(c.SettingsDir, "config.json")
	if ok, _ := utils.Exists(settingsFilepath); ok {
		log.Println("Loading configuration from file:", settingsFilepath)

		file, err := ioutil.ReadFile(settingsFilepath)
		if err != nil {
			return err
		}

		//Make sure all settings keys are present
		var initialMap map[string]interface{}
		err = json.Unmarshal(file, &initialMap)

		if err != nil {
			return ConfigSettingsParseError(fmt.Sprintf("%v", err))
		}

		for _, v := range []string{"Title", "ScreenSize", "ColorMode", "SkipBoot", "DisplayFPS"} {
			if _, ok := initialMap[v]; !ok {
				return ConfigValidationError("Could not find settings key: \"" + v + "\" in settings file")
			}
		}

		//Now parse into struct
		err = json.Unmarshal(file, &c)

		if err != nil {
			return err
		}

		//Perform validations
		err = c.Validate()
		if err != nil {
			return err
		}

		//these are defaults
		c.Debug = *debug
		c.BreakOn = *breakOn
		c.DumpState = *dumpState
	} else {
		log.Println("Could not find settings file at", settingsFilepath, "using default values instead...")
		c.LoadDefaultConfig()
	}
	return nil
}