Keine Beschreibung

account.go 3.6KB

    package main import ( "encoding/json" "errors" "io/ioutil" "log" "os" "path/filepath" "strings" "time" ) func getAccounts() ([]Account, error) { file := UserFile // Check to see if the file exists if _, err := os.Stat(file); os.IsNotExist(err) { return []Account{}, nil } // Open the file fi, err := os.Open(file) if err != nil { return nil, err } // Read all of its contents raw, err := ioutil.ReadAll(fi) if len(raw) == 0 { return []Account{}, nil } // Translate the JSON to internal structures var accounts []Account err = json.Unmarshal(raw, &accounts) if err != nil { return nil, err } fi.Close() return accounts, nil } func (a *Account) Validate() (bool, error) { file := UserFile // Determine if the "users" file exists // Attempt to create the file if it does not if _, err := os.Stat(file); os.IsNotExist(err) { _, err = os.Create(file) if err != nil { return false, err } } // Get a list of current accounts accounts, err := getAccounts() if err != nil { return false, err } // Search if an account and passphrase match is found for _, x := range accounts { if x.Username == a.Username && x.Passphrase == a.Passphrase { return true, nil } } return false, nil } func (a *Account) Create() error { // Ensure there are no accounts of that name already valid, err := a.Validate() if err != nil { return err } if valid { return errors.New("Account already exists") } // Create directory structure baseDir := os.Getenv("LOOP_DATA") userDir := strings.ToLower(a.Username) s := string(filepath.Separator) paths := []string{"content", "blog", "calendar"} for _, p := range paths { err = os.MkdirAll(baseDir+s+userDir+s+p, 0755) if err != nil { return err } } // Add the account to all of the accounts accounts, err := getAccounts() if err != nil { return err } accounts = append(accounts, *a) // Create user entry in "users" file // Marshal the JSON marshalled, err := json.Marshal(accounts) if err != nil { return err } // Write to the file fi, err := os.OpenFile(UserFile, os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { return err } _, err = fi.Write(marshalled) if err != nil { return err } fi.Close() return nil } func (a *Account) Delete() error { // Ensure there are no accounts of that name already valid, err := a.Validate() if err != nil { return err } if !valid { return errors.New("Account does not exist") } // Remove user entry in "users" file accounts, err := getAccounts() if err != nil { return err } for i, account := range accounts { if account.Username == a.Username { accounts = append(accounts[:i], accounts[i+1:]...) break } } marshalled, err := json.Marshal(accounts) if err != nil { return err } log.Printf(string(marshalled)) // Write to the file fi, err := os.OpenFile(UserFile, os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { return err } _, err = fi.Write(marshalled) if err != nil { return err } fi.Close() // Delete directory structure baseDir := os.Getenv("LOOP_DATA") userDir := strings.ToLower(a.Username) s := string(filepath.Separator) err = os.RemoveAll(baseDir + s + userDir) if err != nil { return err } return nil } func (a *Account) Add(content, filename string, file []byte) (string, error) { // TODO: Create new content directory // TODO: Check if filename already exists // TODO: Write file to content directory return "", nil } func (a *Account) Publish(title, content string, date time.Time) (string, error) { // TODO: Check if symlink already exists // TODO: Create symbolic link in "blog" directory referencing content directory return "", nil }