storj/cmd/uplink/cmd_access_create.go
Márton Elek ea1408f7a8 go.mod: bump clingy dependency
As a reminder: latest clingy removed the requirement of having custom context (which made the usage of context.WithValue harder) and uses simple context instead.

Clingy saves the stdin/stdout/stderr to the context (earlier to separated context type) to make it available for unit testing.

Change-Id: I8896574f4670721de43a577cd4b35952e3b5d00e
2022-08-31 10:24:27 +00:00

114 lines
2.7 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"fmt"
"io/ioutil"
"strconv"
"strings"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
"storj.io/storj/cmd/uplink/ulext"
)
type cmdAccessCreate struct {
ex ulext.External
am accessMaker
passphraseStdin bool
satelliteAddr string
apiKey string
importAs string
exportTo string
}
func newCmdAccessCreate(ex ulext.External) *cmdAccessCreate {
return &cmdAccessCreate{ex: ex}
}
func (c *cmdAccessCreate) Setup(params clingy.Parameters) {
c.passphraseStdin = params.Flag("passphrase-stdin", "If set, the passphrase is read from stdin, and all other values must be provided.", false,
clingy.Transform(strconv.ParseBool),
clingy.Boolean,
).(bool)
c.satelliteAddr = params.Flag("satellite-address", "Satellite address from satellite UI (prompted if unspecified)", "").(string)
c.apiKey = params.Flag("api-key", "API key from satellite UI (prompted if unspecified)", "").(string)
c.importAs = params.Flag("import-as", "Import the access as this name", "").(string)
c.exportTo = params.Flag("export-to", "Export the access to this file path", "").(string)
params.Break()
c.am.Setup(params, c.ex)
}
func (c *cmdAccessCreate) Execute(ctx context.Context) (err error) {
if c.satelliteAddr == "" {
if c.passphraseStdin {
return errs.New("Must specify the satellite address as a flag when passphrase-stdin is set.")
}
c.satelliteAddr, err = c.ex.PromptInput(ctx, "Satellite address:")
if err != nil {
return errs.Wrap(err)
}
}
if c.apiKey == "" {
if c.passphraseStdin {
return errs.New("Must specify the api key as a flag when passphrase-stdin is set.")
}
c.apiKey, err = c.ex.PromptInput(ctx, "API key:")
if err != nil {
return errs.Wrap(err)
}
}
var passphrase string
if c.passphraseStdin {
stdinData, err := ioutil.ReadAll(clingy.Stdin(ctx))
if err != nil {
return errs.Wrap(err)
}
passphrase = strings.TrimRight(string(stdinData), "\r\n")
} else {
passphrase, err = c.ex.PromptSecret(ctx, "Passphrase:")
if err != nil {
return errs.Wrap(err)
}
}
if passphrase == "" {
return errs.New("Encryption passphrase must be non-empty")
}
access, err := c.ex.RequestAccess(ctx, c.satelliteAddr, c.apiKey, passphrase)
if err != nil {
return errs.Wrap(err)
}
access, err = c.am.Execute(ctx, c.importAs, access)
if err != nil {
return errs.Wrap(err)
}
if c.exportTo != "" {
return c.ex.ExportAccess(ctx, access, c.exportTo)
}
if c.importAs != "" {
return nil
}
serialized, err := access.Serialize()
if err != nil {
return errs.Wrap(err)
}
fmt.Fprintln(clingy.Stdout(ctx), serialized)
return nil
}