storj/cmd/uplink/cmd_access_import.go
Jeff Wendling 9061dd309f cmd/uplinkng: become cmd/uplink
Change-Id: If426c32219d32044d715ab6dfa9718807f32cb9f
2022-02-09 17:02:21 +00:00

56 lines
1.1 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
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 {
ex ulext.External
am accessMaker
name string
access string
}
func newCmdAccessImport(ex ulext.External) *cmdAccessImport {
return &cmdAccessImport{ex: ex}
}
func (c *cmdAccessImport) Setup(params clingy.Parameters) {
c.am.Setup(params, c.ex)
c.name = params.Arg("name", "Name to save the access as").(string)
c.access = params.Arg("access|filename", "Serialized access value or file path to save").(string)
}
func (c *cmdAccessImport) Execute(ctx clingy.Context) (err error) {
if c.name == "" {
return errs.New("Must specify a name to import the access as.")
}
access, err := uplink.ParseAccess(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)
}
}
_, err = c.am.Execute(ctx, c.name, access)
return err
}