// Loads a session from disk with the given ID. Returns an error if the session does not exist on the server, or if the file cannot be opened. func Load(id int64) (s *Session, err error) { s = new(Session) filename := "data/shared/sessions/" + strconv.FormatInt(id, 10) s.settings, err = ini.Load(filename) if err != nil { return } return }
// Get attempts to find the file corresponding to id, searches through it for key, and returns the corresponding value if it exists. Returns an empty string and an error if the specified id or key does not exist. func (fsm *FSManager) Get(id, key string) (string, error) { filename := path.Join(fsm.path, id) vals, err := ini.Load(filename) if err != nil { return "", err } val, ok := vals[key] if !ok { return "", fmt.Errorf("Specified key '%s' does not exist in file '%s' (for session '%s').", key, filename, id) } return val, nil }
// 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 }
// 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) }
func (u *User) Load() (err error) { filename := u.fileName() u.settings, err = ini.Load(filename) return err }