cmd/uplinkng: implement revoke and normalize some language
this makes the distinction between an "access name" and an "access value" and talks about which is expected for commands. most are "access name or value". Change-Id: I43c0043a17d37e89ab5f87388ae9e890a8b59958
This commit is contained in:
parent
08d860570b
commit
59b8422318
@ -14,10 +14,17 @@ import (
|
||||
"storj.io/uplink"
|
||||
)
|
||||
|
||||
type amSaveKind int
|
||||
|
||||
const (
|
||||
amSaveDefaultFalse amSaveKind = iota
|
||||
amSaveDefaultTrue
|
||||
amSaveForced
|
||||
)
|
||||
|
||||
type accessMaker struct {
|
||||
ex ulext.External
|
||||
|
||||
print bool
|
||||
save bool
|
||||
name string
|
||||
force bool
|
||||
@ -26,29 +33,30 @@ type accessMaker struct {
|
||||
perms accessPermissions
|
||||
}
|
||||
|
||||
func (am *accessMaker) Setup(params clingy.Parameters, ex ulext.External, forceSave bool) {
|
||||
func (am *accessMaker) Setup(params clingy.Parameters, ex ulext.External, saveKind amSaveKind) {
|
||||
am.ex = ex
|
||||
am.save = forceSave
|
||||
am.print = !forceSave
|
||||
am.save = saveKind == amSaveForced
|
||||
|
||||
if !forceSave {
|
||||
am.save = params.Flag("save", "Save the access", true,
|
||||
if saveKind != amSaveForced {
|
||||
am.save = params.Flag("save", "Save the access", saveKind == amSaveDefaultTrue,
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
).(bool)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
am.name = params.Flag("name", "Name to save newly created access, if --save is true", "").(string)
|
||||
|
||||
am.force = params.Flag("force", "Force overwrite an existing saved access grant", false,
|
||||
am.force = params.Flag("force", "Force overwrite an existing saved access", false,
|
||||
clingy.Short('f'),
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
).(bool)
|
||||
|
||||
am.use = params.Flag("use", "Set the saved access to be the one used by default", false,
|
||||
am.use = params.Flag("use", "Switch the access to be the default", false,
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
).(bool)
|
||||
|
||||
if !forceSave {
|
||||
if saveKind != amSaveForced {
|
||||
params.Break()
|
||||
am.perms.Setup(params)
|
||||
}
|
||||
@ -89,10 +97,6 @@ func (am *accessMaker) Execute(ctx clingy.Context, access *uplink.Access) (err e
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
|
||||
if am.print {
|
||||
fmt.Fprintln(ctx, accessValue)
|
||||
}
|
||||
|
||||
if am.save {
|
||||
accesses[am.name] = accessValue
|
||||
if am.use || defaultName == "" {
|
||||
@ -103,7 +107,9 @@ func (am *accessMaker) Execute(ctx clingy.Context, access *uplink.Access) (err e
|
||||
return errs.Wrap(err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(ctx, "Access %q saved to %q\n", am.name, am.ex.AccessInfoFile())
|
||||
fmt.Fprintf(ctx, "Saved access %q to %q\n", am.name, am.ex.AccessInfoFile())
|
||||
} else {
|
||||
fmt.Fprintln(ctx, accessValue)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -27,7 +27,7 @@ func (c *cmdAccessCreate) Setup(params clingy.Parameters) {
|
||||
c.passphrase = params.Flag("passphrase", "Passphrase used for encryption (prompted if unspecified)", "").(string)
|
||||
|
||||
params.Break()
|
||||
c.am.Setup(params, c.ex, false)
|
||||
c.am.Setup(params, c.ex, amSaveDefaultTrue)
|
||||
}
|
||||
|
||||
func (c *cmdAccessCreate) Execute(ctx clingy.Context) (err error) {
|
||||
|
@ -4,6 +4,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/zeebo/clingy"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
@ -13,7 +15,7 @@ import (
|
||||
type cmdAccessDelete struct {
|
||||
ex ulext.External
|
||||
|
||||
name string
|
||||
access string
|
||||
}
|
||||
|
||||
func newCmdAccessDelete(ex ulext.External) *cmdAccessDelete {
|
||||
@ -21,7 +23,7 @@ func newCmdAccessDelete(ex ulext.External) *cmdAccessDelete {
|
||||
}
|
||||
|
||||
func (c *cmdAccessDelete) Setup(params clingy.Parameters) {
|
||||
c.name = params.Arg("name", "Access to delete").(string)
|
||||
c.access = params.Arg("name", "Access name to delete").(string)
|
||||
}
|
||||
|
||||
func (c *cmdAccessDelete) Execute(ctx clingy.Context) error {
|
||||
@ -29,12 +31,20 @@ func (c *cmdAccessDelete) Execute(ctx clingy.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c.name == defaultName {
|
||||
|
||||
if c.access == defaultName {
|
||||
return errs.New("cannot delete current access")
|
||||
}
|
||||
if _, ok := accesses[c.name]; !ok {
|
||||
return errs.New("unknown access: %q", c.name)
|
||||
if _, ok := accesses[c.access]; !ok {
|
||||
return errs.New("unknown access: %q", c.access)
|
||||
}
|
||||
delete(accesses, c.name)
|
||||
return c.ex.SaveAccessInfo(defaultName, accesses)
|
||||
|
||||
delete(accesses, c.access)
|
||||
if err := c.ex.SaveAccessInfo(defaultName, accesses); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(ctx, "Removed access %q from %q\n", c.access, c.ex.AccessInfoFile())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ func newCmdAccessRestrict(ex ulext.External) *cmdAccessRestrict {
|
||||
}
|
||||
|
||||
func (c *cmdAccessRestrict) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to restrict", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to restrict", "").(string)
|
||||
|
||||
params.Break()
|
||||
c.am.Setup(params, c.ex, false)
|
||||
c.am.Setup(params, c.ex, amSaveDefaultFalse)
|
||||
}
|
||||
|
||||
func (c *cmdAccessRestrict) Execute(ctx clingy.Context) error {
|
||||
|
@ -4,6 +4,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/zeebo/clingy"
|
||||
|
||||
"storj.io/storj/cmd/uplinkng/ulext"
|
||||
@ -11,6 +13,9 @@ import (
|
||||
|
||||
type cmdAccessRevoke struct {
|
||||
ex ulext.External
|
||||
|
||||
access string
|
||||
revokee string
|
||||
}
|
||||
|
||||
func newCmdAccessRevoke(ex ulext.External) *cmdAccessRevoke {
|
||||
@ -18,8 +23,27 @@ func newCmdAccessRevoke(ex ulext.External) *cmdAccessRevoke {
|
||||
}
|
||||
|
||||
func (c *cmdAccessRevoke) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Access name or value performing the revoke", "").(string)
|
||||
c.revokee = params.Arg("revokee", "Access name or value revoke").(string)
|
||||
}
|
||||
|
||||
func (c *cmdAccessRevoke) Execute(ctx clingy.Context) error {
|
||||
project, err := c.ex.OpenProject(ctx, c.access)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = project.Close() }()
|
||||
|
||||
access, err := c.ex.OpenAccess(c.revokee)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := project.RevokeAccess(ctx, access); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(ctx, "Revoked access %q\n", c.revokee)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -23,10 +23,10 @@ func newCmdAccessSave(ex ulext.External) *cmdAccessSave {
|
||||
}
|
||||
|
||||
func (c *cmdAccessSave) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Access value to save (prompted if unspecified)", "").(string)
|
||||
c.access = params.Flag("access", "Serialized access value to save (prompted if unspecified)", "").(string)
|
||||
|
||||
params.Break()
|
||||
c.am.Setup(params, c.ex, true)
|
||||
c.am.Setup(params, c.ex, amSaveForced)
|
||||
}
|
||||
|
||||
func (c *cmdAccessSave) Execute(ctx clingy.Context) (err error) {
|
||||
|
@ -4,6 +4,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/zeebo/clingy"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
@ -13,7 +15,7 @@ import (
|
||||
type cmdAccessUse struct {
|
||||
ex ulext.External
|
||||
|
||||
name string
|
||||
access string
|
||||
}
|
||||
|
||||
func newCmdAccessUse(ex ulext.External) *cmdAccessUse {
|
||||
@ -21,7 +23,7 @@ func newCmdAccessUse(ex ulext.External) *cmdAccessUse {
|
||||
}
|
||||
|
||||
func (c *cmdAccessUse) Setup(params clingy.Parameters) {
|
||||
c.name = params.Arg("name", "Access to use").(string)
|
||||
c.access = params.Arg("access", "Access name to use").(string)
|
||||
}
|
||||
|
||||
func (c *cmdAccessUse) Execute(ctx clingy.Context) error {
|
||||
@ -29,8 +31,14 @@ func (c *cmdAccessUse) Execute(ctx clingy.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, ok := accesses[c.name]; !ok {
|
||||
return errs.New("unknown access: %q", c.name)
|
||||
if _, ok := accesses[c.access]; !ok {
|
||||
return errs.New("unknown access: %q", c.access)
|
||||
}
|
||||
return c.ex.SaveAccessInfo(c.name, accesses)
|
||||
if err := c.ex.SaveAccessInfo(c.access, accesses); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(ctx, "Switched default access to access %q\n", c.access)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ func newCmdCp(ex ulext.External) *cmdCp {
|
||||
}
|
||||
|
||||
func (c *cmdCp) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to use", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
||||
c.recursive = params.Flag("recursive", "Peform a recursive copy", false,
|
||||
clingy.Short('r'),
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
|
@ -31,7 +31,7 @@ func newCmdLs(ex ulext.External) *cmdLs {
|
||||
}
|
||||
|
||||
func (c *cmdLs) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to use", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
||||
c.recursive = params.Flag("recursive", "List recursively", false,
|
||||
clingy.Short('r'),
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
|
@ -23,7 +23,7 @@ func newCmdMb(ex ulext.External) *cmdMb {
|
||||
}
|
||||
|
||||
func (c *cmdMb) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to use", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
||||
|
||||
c.name = params.Arg("name", "Bucket name (sj://BUCKET)").(string)
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ func newCmdMetaGet(ex ulext.External) *cmdMetaGet {
|
||||
}
|
||||
|
||||
func (c *cmdMetaGet) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to use", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
||||
c.encrypted = params.Flag("encrypted", "Shows keys base64 encoded without decrypting", false,
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
).(bool)
|
||||
|
@ -28,7 +28,7 @@ func newCmdRb(ex ulext.External) *cmdRb {
|
||||
}
|
||||
|
||||
func (c *cmdRb) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to use", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
||||
c.force = params.Flag("force", "Deletes any objects in bucket first", false,
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
).(bool)
|
||||
|
@ -29,7 +29,7 @@ func newCmdRm(ex ulext.External) *cmdRm {
|
||||
}
|
||||
|
||||
func (c *cmdRm) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to use", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
||||
c.recursive = params.Flag("recursive", "Remove recursively", false,
|
||||
clingy.Short('r'),
|
||||
clingy.Transform(strconv.ParseBool),
|
||||
|
@ -20,7 +20,7 @@ func newCmdShare(ex ulext.External) *cmdShare {
|
||||
}
|
||||
|
||||
func (c *cmdShare) Setup(params clingy.Parameters) {
|
||||
c.access = params.Flag("access", "Which access to use", "").(string)
|
||||
c.access = params.Flag("access", "Access name or value to use", "").(string)
|
||||
}
|
||||
|
||||
func (c *cmdShare) Execute(ctx clingy.Context) error {
|
||||
|
Loading…
Reference in New Issue
Block a user