2021-06-22 23:41:22 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulext"
|
|
|
|
"storj.io/uplink"
|
|
|
|
)
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
type amSaveKind int
|
|
|
|
|
|
|
|
const (
|
|
|
|
amSaveDefaultFalse amSaveKind = iota
|
|
|
|
amSaveDefaultTrue
|
|
|
|
amSaveForced
|
|
|
|
)
|
|
|
|
|
2021-06-22 23:41:22 +01:00
|
|
|
type accessMaker struct {
|
|
|
|
ex ulext.External
|
|
|
|
|
|
|
|
save bool
|
|
|
|
name string
|
|
|
|
force bool
|
|
|
|
use bool
|
|
|
|
|
|
|
|
perms accessPermissions
|
|
|
|
}
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
func (am *accessMaker) Setup(params clingy.Parameters, ex ulext.External, saveKind amSaveKind) {
|
2021-06-22 23:41:22 +01:00
|
|
|
am.ex = ex
|
2021-06-25 17:51:05 +01:00
|
|
|
am.save = saveKind == amSaveForced
|
2021-06-22 23:41:22 +01:00
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
if saveKind != amSaveForced {
|
|
|
|
am.save = params.Flag("save", "Save the access", saveKind == amSaveDefaultTrue,
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-06-22 23:41:22 +01:00
|
|
|
).(bool)
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
am.name = params.Flag("name", "Name to save the access value under, if --save is true", "").(string)
|
|
|
|
} else {
|
|
|
|
am.name = params.Flag("name", "Name to save the access value under", "").(string)
|
|
|
|
}
|
2021-06-22 23:41:22 +01:00
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
am.force = params.Flag("force", "Force overwrite an existing saved access", false,
|
2021-06-22 23:41:22 +01:00
|
|
|
clingy.Short('f'),
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-06-22 23:41:22 +01:00
|
|
|
).(bool)
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
am.use = params.Flag("use", "Switch the access to be the default", false,
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-06-22 23:41:22 +01:00
|
|
|
).(bool)
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
if saveKind != amSaveForced {
|
2021-06-22 23:41:22 +01:00
|
|
|
params.Break()
|
|
|
|
am.perms.Setup(params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *accessMaker) Execute(ctx clingy.Context, access *uplink.Access) (err error) {
|
|
|
|
defaultName, accesses, err := am.ex.GetAccessInfo(false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if am.save {
|
|
|
|
// pick a default name for the access if we're saving and there are
|
|
|
|
// no saved accesses. otherwise, prompt.
|
|
|
|
if am.name == "" && len(accesses) == 0 {
|
|
|
|
am.name = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
if am.name == "" {
|
|
|
|
am.name, err = am.ex.PromptInput(ctx, "Name:")
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := accesses[am.name]; ok && !am.force {
|
|
|
|
return errs.New("Access %q already exists. Overwrite by specifying --force or choose a new name with --name", am.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
access, err = am.perms.Apply(access)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accessValue, err := access.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if am.save {
|
|
|
|
accesses[am.name] = accessValue
|
|
|
|
if am.use || defaultName == "" {
|
|
|
|
defaultName = am.name
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := am.ex.SaveAccessInfo(defaultName, accesses); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
fmt.Fprintf(ctx, "Saved access %q to %q\n", am.name, am.ex.AccessInfoFile())
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(ctx, accessValue)
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|