6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-29 18:21:06 +00:00

Simplify identity creationg and management

This commit is contained in:
James Mills 2022-03-20 02:34:39 +10:00
parent a1b00f1bd8
commit 2a93983603
2 changed files with 5 additions and 40 deletions

@ -2,8 +2,8 @@ package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -63,13 +63,9 @@ func makeuser(identity, uri, user string) {
os.Exit(2)
}
config := saltyim.GetConfigDir()
if config == "" {
log.Fatal("unable to find a suitable configurtion directory")
}
if err := os.MkdirAll(config, 0700); err != nil {
fmt.Fprintf(os.Stderr, "error creating configuration directory %s: %s", config, err)
dir := filepath.Dir(identity)
if err := os.MkdirAll(dir, 0700); err != nil {
fmt.Fprintf(os.Stderr, "error creating configuration directory %s: %s", dir, err)
os.Exit(2)
}

@ -5,25 +5,12 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/keys-pub/keys"
"go.mills.io/salty"
)
var (
defaultConfigPaths = []string{
"$HOME/.config/salty/",
"$HOME/.salty/",
}
defaultIdentityPaths = []string{
"$HOME/.config/salty/$USER.key",
"$HOME/.salty/$USER.key",
}
)
func readUser(fd io.Reader) (Addr, error) {
scan := bufio.NewScanner(fd)
@ -40,28 +27,10 @@ func readUser(fd io.Reader) (Addr, error) {
return a, scan.Err()
}
// GetConfigDir returns a suitable configuration directory
func GetConfigDir() string {
if configDir := os.Getenv("XDG_CONFIG_HOME"); configDir != "" {
return filepath.Join(configDir, "salty")
}
for _, p := range defaultConfigPaths {
if p := os.ExpandEnv(p); DirExists(p) || DirExists(filepath.Join(p, "..")) {
return p
}
}
return ""
}
// DefaultIdentity returns a default identity file (if one exists) otherwise
// returns an empty string
func DefaultIdentity() string {
for _, p := range defaultIdentityPaths {
if fn := os.ExpandEnv(p); FileExists(fn) {
return fn
}
}
return ""
return os.ExpandEnv("$HOME/.salty/$USER.key")
}
// CreateIdentity ...