2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/uplink"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cmdAccessSave struct {
|
|
|
|
access string
|
|
|
|
name string
|
|
|
|
force bool
|
|
|
|
use bool
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:50 +01:00
|
|
|
func (c *cmdAccessSave) Setup(params clingy.Parameters) {
|
|
|
|
c.access = params.Flag("access", "Access to save (prompted if unspecified)", "").(string)
|
|
|
|
c.name = params.Flag("name", "Name to save the access grant under", "default").(string)
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-25 00:11:50 +01:00
|
|
|
c.force = params.Flag("force", "Force overwrite an existing saved access grant", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Short('f'),
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-05-25 00:11:50 +01:00
|
|
|
c.use = params.Flag("use", "Set the saved access to be the one used by default", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdAccessSave) Execute(ctx clingy.Context) error {
|
2021-05-12 17:41:05 +01:00
|
|
|
accessDefault, accesses, err := gf.GetAccessInfo(false)
|
2021-03-31 16:56:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.access == "" {
|
2021-05-12 17:41:05 +01:00
|
|
|
c.access, err = gf.PromptInput(ctx, "Access:")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
if _, err := uplink.ParseAccess(c.access); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, ok := accesses[c.name]; ok && !c.force {
|
2021-05-12 17:41:05 +01:00
|
|
|
return errs.New("Access %q already exists. Overwrite by specifying --force or choose a new name with --name", c.name)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
accesses[c.name] = c.access
|
2021-05-12 17:41:05 +01:00
|
|
|
if c.use || accessDefault == "" {
|
2021-03-31 16:56:34 +01:00
|
|
|
accessDefault = c.name
|
|
|
|
}
|
|
|
|
|
|
|
|
return gf.SaveAccessInfo(accessDefault, accesses)
|
|
|
|
}
|