-
Notifications
You must be signed in to change notification settings - Fork 402
/
access_maker.go
75 lines (57 loc) · 1.61 KB
/
access_maker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"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 clingy.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(ctx, "Imported access %q to %q\n", name, am.ex.AccessInfoFile())
}
return access, nil
}