storj/cmd/uplink/cmd_access_restrict.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

63 lines
1.3 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"fmt"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
"storj.io/storj/cmd/uplink/ulext"
)
type cmdAccessRestrict struct {
ex ulext.External
am accessMaker
access string
importAs string
exportTo string
}
func newCmdAccessRestrict(ex ulext.External) *cmdAccessRestrict {
return &cmdAccessRestrict{ex: ex}
}
func (c *cmdAccessRestrict) Setup(params clingy.Parameters) {
c.access = params.Flag("access", "Access name or value to restrict", "").(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)
params.Break()
c.am.perms.Setup(params, true)
}
func (c *cmdAccessRestrict) Execute(ctx context.Context) error {
access, err := c.ex.OpenAccess(c.access)
if err != nil {
return err
}
access, err = c.am.Execute(ctx, c.importAs, access)
if err != nil {
return err
}
if c.exportTo != "" {
return c.ex.ExportAccess(ctx, access, c.exportTo)
}
serialized, err := access.Serialize()
if err != nil {
return errs.Wrap(err)
}
fmt.Fprintln(clingy.Stdout(ctx), serialized)
return nil
}