mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-28 17:51:04 +00:00
Improve error handling for make-user
This commit is contained in:
parent
51e2b8ff4b
commit
b40ce46ddf
@ -65,18 +65,22 @@ func makeuser(identity, uri, user string) {
|
||||
|
||||
dir := filepath.Dir(identity)
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error creating configuration directory %s: %s", dir, err)
|
||||
fmt.Fprintf(os.Stderr, "error creating configuration directory %s: %s\n", dir, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
if saltyim.FileExists(identity) {
|
||||
fmt.Fprintf(os.Stderr, "error identity %q already exists!\n", identity)
|
||||
}
|
||||
|
||||
if err := saltyim.CreateIdentity(identity, user); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error creating identity %s for %s: %s", identity, user, err)
|
||||
fmt.Fprintf(os.Stderr, "error creating identity %q for %s: %s\n", identity, user, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
key, me, err := saltyim.GetIdentity(identity)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error reading identity %s for %s: %s", identity, user, err)
|
||||
fmt.Fprintf(os.Stderr, "error reading identity %s for %s: %s\n", identity, user, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
|
12
identity.go
12
identity.go
@ -43,7 +43,7 @@ func DefaultInbox() string {
|
||||
func CreateIdentity(fn, user string) error {
|
||||
f, err := os.OpenFile(fn, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error opening identity %s for writing: %w", fn, err)
|
||||
return fmt.Errorf("error opening identity %q for writing: %w", fn, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
@ -52,11 +52,11 @@ func CreateIdentity(fn, user string) error {
|
||||
f.Write([]byte(fmt.Sprintf("# user: %s\n", user)))
|
||||
|
||||
if err := f.Sync(); err != nil {
|
||||
return fmt.Errorf("error syncing identity %s for writing: %w", fn, err)
|
||||
return fmt.Errorf("error syncing identity %q for writing: %w", fn, err)
|
||||
}
|
||||
|
||||
if err := f.Close(); err != nil {
|
||||
return fmt.Errorf("error closing identity %s for writing: %w", fn, err)
|
||||
return fmt.Errorf("error closing identity %q for writing: %w", fn, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -66,19 +66,19 @@ func CreateIdentity(fn, user string) error {
|
||||
func GetIdentity(fn string) (*keys.EdX25519Key, Addr, error) {
|
||||
id, err := os.Open(fn)
|
||||
if err != nil {
|
||||
return nil, Addr{}, fmt.Errorf("error opening identity file: %q", fn)
|
||||
return nil, Addr{}, fmt.Errorf("error opening identity %q: %s", fn, err)
|
||||
}
|
||||
defer id.Close()
|
||||
|
||||
key, err := salty.ParseIdentity(id)
|
||||
if err != nil {
|
||||
return nil, Addr{}, fmt.Errorf("error reading private key: %q", fn)
|
||||
return nil, Addr{}, fmt.Errorf("error reading identity %q: %s", fn, err)
|
||||
}
|
||||
|
||||
id.Seek(0, 0)
|
||||
me, err := readUser(id)
|
||||
if err != nil {
|
||||
return key, Addr{}, fmt.Errorf("error reading user from keyfile: %q", fn)
|
||||
return key, Addr{}, fmt.Errorf("error reading user from identity %q: %s", fn, err)
|
||||
}
|
||||
|
||||
return key, me, nil
|
||||
|
Loading…
Reference in New Issue
Block a user