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

77 lines
1.6 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"fmt"
"strconv"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
"storj.io/storj/cmd/uplink/ulext"
"storj.io/uplink"
)
type accessMaker struct {
ex ulext.External
force bool
use bool
perms accessPermissions
}
func (am *accessMaker) Setup(params clingy.Parameters, ex ulext.External) {
am.ex = ex
am.force = params.Flag("force", "Force overwrite an existing saved access", false,
clingy.Short('f'),
clingy.Transform(strconv.ParseBool), clingy.Boolean,
).(bool)
am.use = params.Flag("use", "Switch the default access to the newly created one", false,
clingy.Transform(strconv.ParseBool), clingy.Boolean,
).(bool)
}
func (am *accessMaker) Execute(ctx context.Context, name string, access *uplink.Access) (_ *uplink.Access, err error) {
defaultName, accesses, err := am.ex.GetAccessInfo(false)
if err != nil {
return nil, err
}
if name != "" {
if _, ok := accesses[name]; ok && !am.force {
return nil, errs.New("Access %q already exists. Overwrite by specifying --force or choose a new name.", name)
}
}
access, err = am.perms.Apply(access)
if err != nil {
return nil, errs.Wrap(err)
}
accessValue, err := access.Serialize()
if err != nil {
return nil, errs.Wrap(err)
}
if name != "" {
accesses[name] = accessValue
if am.use || defaultName == "" {
defaultName = name
}
if err := am.ex.SaveAccessInfo(defaultName, accesses); err != nil {
return nil, errs.Wrap(err)
}
fmt.Fprintf(clingy.Stdout(ctx), "Imported access %q to %q\n", name, am.ex.AccessInfoFile())
}
return access, nil
}