Store everything in XDG_CONFIG_HOME, add `status` command

This commit is contained in:
emersion 2019-03-22 22:35:10 +02:00
parent acf2a5ee5b
commit e6f814952d
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 76 additions and 5 deletions

View File

@ -13,10 +13,13 @@ import (
"golang.org/x/crypto/nacl/secretbox"
"golang.org/x/crypto/openpgp"
"github.com/emersion/hydroxide/config"
"github.com/emersion/hydroxide/protonmail"
)
const authFile = "auth.json"
func authFilePath() (string, error) {
return config.Path("auth.json")
}
type CachedAuth struct {
protonmail.Auth
@ -26,7 +29,11 @@ type CachedAuth struct {
}
func readCachedAuths() (map[string]string, error) {
f, err := os.Open(authFile)
p, err := authFilePath()
if err != nil {
return nil, err
}
f, err := os.Open(p)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
@ -40,7 +47,11 @@ func readCachedAuths() (map[string]string, error) {
}
func saveAuths(auths map[string]string) error {
f, err := os.Create(authFile)
p, err := authFilePath()
if err != nil {
return err
}
f, err := os.Create(p)
if err != nil {
return err
}
@ -123,6 +134,19 @@ func authenticate(c *protonmail.Client, cachedAuth *CachedAuth, username string)
return c.Unlock(auth, cachedAuth.MailboxPassword)
}
func ListUsernames() ([]string, error) {
auths, err := readCachedAuths()
if err != nil {
return nil, err
}
l := make([]string, 0, len(auths))
for username, _ := range auths {
l = append(l, username)
}
return l, nil
}
func GeneratePassword() (secretKey *[32]byte, password string, err error) {
var key [32]byte
if _, err = io.ReadFull(rand.Reader, key[:]); err != nil {

View File

@ -138,6 +138,20 @@ func main() {
}
fmt.Println("Bridge password:", bridgePassword)
case "status":
usernames, err := auth.ListUsernames()
if err != nil {
log.Fatal(err)
}
if len(usernames) == 0 {
fmt.Printf("No logged in user.\n")
} else {
fmt.Printf("%v logged in user(s):\n", len(usernames))
for _, u := range usernames {
fmt.Printf("- %v\n", u)
}
}
case "smtp":
port := os.Getenv("PORT")
if port == "" {

27
config/config.go Normal file
View File

@ -0,0 +1,27 @@
package config
import (
"errors"
"os"
"path/filepath"
)
func Path(filename string) (string, error) {
configHome := os.Getenv("XDG_CONFIG_HOME")
if configHome == "" {
home := os.Getenv("HOME")
if home == "" {
return "", errors.New("HOME not set")
}
configHome = filepath.Join(home, ".config")
}
p := filepath.Join(configHome, "hydroxide", filename)
dirname, _ := filepath.Split(p)
if err := os.MkdirAll(dirname, 0700); err != nil {
return "", err
}
return p, nil
}

View File

@ -6,6 +6,7 @@ import (
"github.com/boltdb/bolt"
"github.com/emersion/hydroxide/config"
"github.com/emersion/hydroxide/protonmail"
)
@ -223,8 +224,13 @@ func (u *User) Close() error {
return u.db.Close()
}
func Open(path string) (*User, error) {
db, err := bolt.Open(path, 0700, nil)
func Open(filename string) (*User, error) {
p, err := config.Path(filename)
if err != nil {
return nil, err
}
db, err := bolt.Open(p, 0700, nil)
if err != nil {
return nil, err
}