Skip to content

Commit

Permalink
Try to output simpler error msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
quite committed May 2, 2024
1 parent 884b00a commit 15a219c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
16 changes: 6 additions & 10 deletions cmd/age-plugin-tkey/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/quite/age-plugin-tkey/internal/identity"
)

func convert(in io.Reader, out io.Writer) bool {
func convert(in io.Reader, out io.Writer) error {
pluginPrefix := fmt.Sprintf("AGE-PLUGIN-%s-", strings.ToUpper(pluginName))

scanner := bufio.NewScanner(in)
Expand All @@ -28,31 +28,27 @@ func convert(in io.Reader, out io.Writer) bool {

name, rawID, err := plugin.ParseIdentity(line)
if err != nil {
le.Printf("ParseIdentity failed on line %d: %s\n", n, err)
return false
return fmt.Errorf("ParseIdentity failed on line %d: %w", n, err)
}
if name != pluginName {
continue
}

id, err := identity.NewIdentityFromRawID(rawID)
if err != nil {
le.Printf("NewIdentityFromRawID failed: %s\n", err)
return false
return fmt.Errorf("NewIdentityFromRawID failed: %w", err)
}

recipient, err := id.EncodeRecipient()
if err != nil {
le.Printf("EncodeRecipient failed: %s\n", err)
return false
return fmt.Errorf("EncodeRecipient failed: %w", err)
}
fmt.Fprintf(out, "%s\n", recipient)
}

if err := scanner.Err(); err != nil {
le.Printf("Scan failed: %s\n", err)
return false
return fmt.Errorf("Scan failed: %w", err)
}

return true
return nil
}
16 changes: 6 additions & 10 deletions cmd/age-plugin-tkey/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,27 @@ import (
"golang.org/x/term"
)

func generate(out *os.File, requireTouch bool) bool {
func generate(out *os.File, requireTouch bool) error {
// Generate a privkey on the TKey and get hold of the pubkey

var userSecret [tkeyx25519.UserSecretSize]byte
if _, err := rand.Read(userSecret[:]); err != nil {
le.Printf("rand.Read failed: %s\n", err)
return false
return fmt.Errorf("rand.Read failed: %w", err)
}

udi, id, err := identity.NewIdentity(userSecret[:], requireTouch)
if err != nil {
le.Printf("NewIdentity failed: %s\n", err)
return false
return fmt.Errorf("NewIdentity failed: %w", err)
}

idStr, err := id.EncodeIdentity(pluginName)
if err != nil {
le.Printf("%s\n", err)
return false
return fmt.Errorf("EncodeIdentity failed: %w", err)
}

recipient, err := id.EncodeRecipient()
if err != nil {
le.Printf("encodeRecipient failed: %s\n", err)
return false
return fmt.Errorf("EncodeRecipient failed: %w", err)
}

if udi == nil {
Expand All @@ -53,5 +49,5 @@ func generate(out *os.File, requireTouch bool) bool {
fmt.Fprintf(out, "# touch required: %t\n", requireTouch)
fmt.Fprintf(out, "%s\n", idStr)

return true
return nil
}
38 changes: 29 additions & 9 deletions cmd/age-plugin-tkey/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
"strings"

"github.com/quite/age-plugin-tkey/internal/identity"
"github.com/quite/age-plugin-tkey/internal/tkey"
"github.com/tillitis/tkeyclient"
)

const (
Expand Down Expand Up @@ -103,30 +106,32 @@ func run() int {
if outputFlag != "" {
f, err := os.OpenFile(outputFlag, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
le.Printf("OpenFile failed: %s\n", err)
le.Printf("%s\n", err)
return 1
}
out = f
}
success := generate(out, noTouchFlag == false)
if outputFlag != "" {
if err := out.Close(); err != nil {
le.Printf("Close failed: %s\n", err)
}
}
if !success {
if err := generate(out, noTouchFlag == false); err != nil {
err = baseErr(err)
le.Printf("generate failed: %s\n", err)
if outputFlag != "" {
if err := os.Remove(outputFlag); err != nil {
le.Printf("Remove failed: %s\n", err)
}
}
return 1
} else if outputFlag != "" {
if err := out.Close(); err != nil {
le.Printf("Close failed: %s\n", err)
}
}
return 0
}

if convertFlag {
if !convert(os.Stdin, os.Stdout) {
if err := convert(os.Stdin, os.Stdout); err != nil {
err = baseErr(err)
le.Printf("convert failed: %s\n", err)
return 1
}
return 0
Expand Down Expand Up @@ -164,3 +169,18 @@ func wrap(s string, cols int, indent int) string {
}
return out
}

func baseErr(err error) error {
candidates := []error{
tkeyclient.ErrNoDevice,
tkeyclient.ErrManyDevices,
tkey.ErrWrongDeviceApp,
identity.ErrWrongDevice,
}
for _, baseErr := range candidates {
if errors.Is(err, baseErr) {
return baseErr
}
}
return err
}

0 comments on commit 15a219c

Please sign in to comment.