-
Notifications
You must be signed in to change notification settings - Fork 394
/
cmd_access_list.go
80 lines (63 loc) · 1.58 KB
/
cmd_access_list.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
76
77
78
79
80
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"sort"
"strconv"
"strings"
"github.com/zeebo/clingy"
"storj.io/storj/cmd/uplink/ulext"
"storj.io/uplink"
)
type cmdAccessList struct {
ex ulext.External
verbose bool
}
func newCmdAccessList(ex ulext.External) *cmdAccessList {
return &cmdAccessList{ex: ex}
}
func (c *cmdAccessList) Setup(params clingy.Parameters) {
c.verbose = params.Flag("verbose", "Verbose output of accesses", false,
clingy.Short('v'),
clingy.Transform(strconv.ParseBool), clingy.Boolean,
).(bool)
}
func (c *cmdAccessList) Execute(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
defaultName, accesses, err := c.ex.GetAccessInfo(true)
if err != nil {
return err
}
var tw *tabbedWriter
if c.verbose {
tw = newTabbedWriter(clingy.Stdout(ctx), "CURRENT", "NAME", "SATELLITE", "VALUE")
} else {
tw = newTabbedWriter(clingy.Stdout(ctx), "CURRENT", "NAME", "SATELLITE")
}
defer tw.Done()
var names []string
for name := range accesses {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
address := "<access parse error>"
if access, err := uplink.ParseAccess(accesses[name]); err == nil {
address = access.SatelliteAddress()
if idx := strings.IndexByte(address, '@'); !c.verbose && idx >= 0 {
address = address[idx+1:]
}
}
inUse := ' '
if name == defaultName {
inUse = '*'
}
if c.verbose {
tw.WriteLine(inUse, name, address, accesses[name])
} else {
tw.WriteLine(inUse, name, address)
}
}
return nil
}