Exemplo n.º 1
0
// Forces the session to be saved to disk. Note that the sessions are saved to disk on each change currently, since there are very few changes.
func (s *Session) Save() (err error) {
	filename := "data/shared/sessions/" + strconv.FormatInt(s.ID, 10)
	fmt.Println(filename)
	err = ini.Save(filename, s.settings)
	if err != nil {
		return
	}
	return
}
Exemplo n.º 2
0
// Set finds the file corresponding to id, loads it into memory, sets the corresponding key to the specified val, and then writes it back to disk. Returns an error, if any.
func (fsm *FSManager) Set(id, key, val string) error {
	filename := path.Join(fsm.path, id)
	vals, err := ini.Load(filename)
	if err != nil {
		// Assume no session file exists yet
		vals = make(map[string]string, 1)
	}
	vals[key] = val
	err = ini.Save(filename, vals)
	if err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: krpors/stats
// Prepares configuration by reading the config file from the current user's
// home directory. If the ~/.config/stats/config file does not exist, create it,
// and write the default configuration keys. The file is automatically chmodded to 0600,
// to prevent world readable permissions (it stores a plaintext password).
func ReadConfiguration() (map[string]string, error) {
	// get the current user, so we can get the home dir.
	u, err := user.Current()
	if err != nil {
		return nil, fmt.Errorf("Cannot fetch current user")
	}

	var configFilePath string = path.Join(u.HomeDir, ".config", "stats")
	var configFile string = path.Join(configFilePath, "config")
	var settings map[string]string = make(map[string]string)

	file, err := os.Open(configFile)
	if err != nil {
		// If it doesn't exist, or the like, create it. First, create the directories
		// required, if necessary.
		if os.MkdirAll(configFilePath, 0700) != nil {
			return nil, fmt.Errorf("Failed to create configuration directory `%s'", configFilePath)
		}
		fmt.Printf("Creating default configuration file `%s'\n", configFile)
		file, err = os.Create(configFile)
		if err != nil {
			// We need a config file, so Exit(1) when it failed.
			return nil, fmt.Errorf("Failed to create configuration file `%s'\n", configFile)
		}
		// change permissions to be r/w to current user only. This file is
		// storing a plain text password, so we must not make it world readable.
		if err = file.Chmod(0600); err != nil {
			return nil, fmt.Errorf("Failed to change permissions on configuration file `%s'\n", configFile)
		}

		defer file.Close()

		// write some default settings:
		settings[SETTING_USERNAME] = "username"
		settings[SETTING_PASSWORD] = "password"
		settings[SETTING_MAIL_FROM] = "Server report <*****@*****.**>"
		settings[SETTING_MAIL_TO] = "Name <*****@*****.**>"
		settings[SETTING_MAIL_HOST] = "smtp.gmail.com:587"
		settings[SETTING_MAIL_SUBJECT] = "Server report"
		settings[SETTING_FROM_ADDR] = "*****@*****.**"
		settings[SETTING_TO_ADDR] = "*****@*****.**"

		if err = ini.Save(configFile, settings); err != nil {
			return nil, fmt.Errorf("Unable to write to configuration file.")
		}
	}

	// If the file does exist though, read the properties:
	return ini.Load(configFile)
}
Exemplo n.º 4
0
func (u *User) Save() error {
	filename := u.fileName()
	err := ini.Save(filename, u.settings)
	return err
}