cmd/uplink: return nicer error message during import

Fixes #4750

Change-Id: Icef3340dd04d1c502b1651dcc70362895f258563
This commit is contained in:
Jeff Wendling 2022-04-26 10:50:52 -04:00 committed by Ivan Fraixedes
parent f25ead5f98
commit fead0db981
2 changed files with 13 additions and 17 deletions

View File

@ -4,14 +4,10 @@
package main
import (
"bytes"
"io/ioutil"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
"storj.io/storj/cmd/uplink/ulext"
"storj.io/uplink"
)
type cmdAccessImport struct {
@ -38,16 +34,9 @@ func (c *cmdAccessImport) Execute(ctx clingy.Context) (err error) {
return errs.New("Must specify a name to import the access as.")
}
access, err := uplink.ParseAccess(c.access)
access, err := parseAccessDataOrPossiblyFile(c.access)
if err != nil {
data, err := ioutil.ReadFile(c.access)
if err != nil {
return errs.Wrap(err)
}
access, err = uplink.ParseAccess(string(bytes.TrimSpace(data)))
if err != nil {
return errs.Wrap(err)
}
return errs.Wrap(err)
}
_, err = c.am.Execute(ctx, c.name, access)

View File

@ -7,6 +7,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
@ -57,14 +58,20 @@ func (ex *external) loadAccesses() error {
}
func parseAccessDataOrPossiblyFile(accessDataOrFile string) (*uplink.Access, error) {
if access, err := uplink.ParseAccess(accessDataOrFile); err == nil {
access, parseErr := uplink.ParseAccess(accessDataOrFile)
if parseErr == nil {
return access, nil
}
accessData, err := ioutil.ReadFile(accessDataOrFile)
if err != nil {
return nil, errs.Wrap(err)
accessData, readErr := ioutil.ReadFile(accessDataOrFile)
if readErr != nil {
var pathErr *os.PathError
if errors.As(readErr, &pathErr) {
readErr = pathErr.Err
}
return nil, errs.New("unable to open or parse access: %w", errs.Combine(parseErr, readErr))
}
return uplink.ParseAccess(string(bytes.TrimSpace(accessData)))
}