2021-06-22 23:41:22 +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-22 23:41:22 +01:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
2021-06-22 23:41:22 +01:00
|
|
|
"storj.io/uplink"
|
|
|
|
)
|
|
|
|
|
|
|
|
type accessMaker struct {
|
|
|
|
ex ulext.External
|
|
|
|
|
|
|
|
force bool
|
|
|
|
use bool
|
|
|
|
|
|
|
|
perms accessPermissions
|
|
|
|
}
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
func (am *accessMaker) Setup(params clingy.Parameters, ex ulext.External) {
|
2021-06-22 23:41:22 +01:00
|
|
|
am.ex = ex
|
|
|
|
|
2021-06-25 17:51:05 +01:00
|
|
|
am.force = params.Flag("force", "Force overwrite an existing saved access", false,
|
2021-06-22 23:41:22 +01:00
|
|
|
clingy.Short('f'),
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-06-22 23:41:22 +01:00
|
|
|
).(bool)
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
am.use = params.Flag("use", "Switch the default access to the newly created one", false,
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-06-22 23:41:22 +01:00
|
|
|
).(bool)
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (am *accessMaker) Execute(ctx context.Context, name string, access *uplink.Access) (_ *uplink.Access, err error) {
|
2021-06-22 23:41:22 +01:00
|
|
|
defaultName, accesses, err := am.ex.GetAccessInfo(false)
|
|
|
|
if err != nil {
|
2022-01-25 23:39:26 +00:00
|
|
|
return nil, err
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
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)
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
access, err = am.perms.Apply(access)
|
|
|
|
if err != nil {
|
2022-01-25 23:39:26 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
accessValue, err := access.Serialize()
|
|
|
|
if err != nil {
|
2022-01-25 23:39:26 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
if name != "" {
|
|
|
|
accesses[name] = accessValue
|
2021-06-22 23:41:22 +01:00
|
|
|
if am.use || defaultName == "" {
|
2022-01-25 23:39:26 +00:00
|
|
|
defaultName = name
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := am.ex.SaveAccessInfo(defaultName, accesses); err != nil {
|
2022-01-25 23:39:26 +00:00
|
|
|
return nil, errs.Wrap(err)
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
fmt.Fprintf(clingy.Stdout(ctx), "Imported access %q to %q\n", name, am.ex.AccessInfoFile())
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 23:39:26 +00:00
|
|
|
return access, nil
|
2021-06-22 23:41:22 +01:00
|
|
|
}
|