2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-30 10:51:31 +01:00
|
|
|
"context"
|
2021-06-25 17:51:05 +01:00
|
|
|
"fmt"
|
|
|
|
|
2021-03-31 16:56:34 +01:00
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"github.com/zeebo/errs"
|
2021-05-26 21:19:29 +01:00
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
type cmdAccessRemove struct {
|
2021-05-26 21:19:29 +01:00
|
|
|
ex ulext.External
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
access string
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
func newCmdAccessRemove(ex ulext.External) *cmdAccessRemove {
|
|
|
|
return &cmdAccessRemove{ex: ex}
|
2021-05-26 21:19:29 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
func (c *cmdAccessRemove) Setup(params clingy.Parameters) {
|
2021-06-25 17:51:05 +01:00
|
|
|
c.access = params.Arg("name", "Access name to delete").(string)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdAccessRemove) Execute(ctx context.Context) error {
|
2021-05-26 21:19:29 +01:00
|
|
|
defaultName, accesses, err := c.ex.GetAccessInfo(true)
|
2021-03-31 16:56:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-25 17:51:05 +01:00
|
|
|
|
|
|
|
if c.access == defaultName {
|
2021-03-31 16:56:34 +01:00
|
|
|
return errs.New("cannot delete current access")
|
|
|
|
}
|
2021-06-25 17:51:05 +01:00
|
|
|
if _, ok := accesses[c.access]; !ok {
|
|
|
|
return errs.New("unknown access: %q", c.access)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
2021-06-25 17:51:05 +01:00
|
|
|
|
|
|
|
delete(accesses, c.access)
|
|
|
|
if err := c.ex.SaveAccessInfo(defaultName, accesses); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
fmt.Fprintf(clingy.Stdout(ctx), "Removed access %q from %q\n", c.access, c.ex.AccessInfoFile())
|
2021-06-25 17:51:05 +01:00
|
|
|
|
|
|
|
return nil
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|