2021-03-31 16:56:34 +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-03-31 16:56:34 +01:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
2021-03-31 16:56:34 +01:00
|
|
|
"storj.io/uplink"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cmdAccessList struct {
|
2021-05-26 21:19:29 +01:00
|
|
|
ex ulext.External
|
|
|
|
|
2021-03-31 16:56:34 +01:00
|
|
|
verbose bool
|
|
|
|
}
|
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
func newCmdAccessList(ex ulext.External) *cmdAccessList {
|
|
|
|
return &cmdAccessList{ex: ex}
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:50 +01:00
|
|
|
func (c *cmdAccessList) Setup(params clingy.Parameters) {
|
|
|
|
c.verbose = params.Flag("verbose", "Verbose output of accesses", false,
|
2021-03-31 16:56:34 +01:00
|
|
|
clingy.Short('v'),
|
2021-12-09 19:21:52 +00:00
|
|
|
clingy.Transform(strconv.ParseBool), clingy.Boolean,
|
2021-03-31 16:56:34 +01:00
|
|
|
).(bool)
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
func (c *cmdAccessList) Execute(ctx context.Context) error {
|
2021-05-26 21:19:29 +01:00
|
|
|
defaultName, accesses, err := c.ex.GetAccessInfo(true)
|
2021-03-31 16:56:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:40:04 +01:00
|
|
|
var tw *tabbedWriter
|
2021-03-31 16:56:34 +01:00
|
|
|
if c.verbose {
|
2022-08-30 10:51:31 +01:00
|
|
|
tw = newTabbedWriter(clingy.Stdout(ctx), "CURRENT", "NAME", "SATELLITE", "VALUE")
|
2021-03-31 16:56:34 +01:00
|
|
|
} else {
|
2022-08-30 10:51:31 +01:00
|
|
|
tw = newTabbedWriter(clingy.Stdout(ctx), "CURRENT", "NAME", "SATELLITE")
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
2021-04-06 04:40:04 +01:00
|
|
|
defer tw.Done()
|
2021-03-31 16:56:34 +01:00
|
|
|
|
|
|
|
var names []string
|
|
|
|
for name := range accesses {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
for _, name := range names {
|
2022-02-09 21:36:09 +00:00
|
|
|
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:]
|
|
|
|
}
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inUse := ' '
|
2021-05-26 21:19:29 +01:00
|
|
|
if name == defaultName {
|
2021-03-31 16:56:34 +01:00
|
|
|
inUse = '*'
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.verbose {
|
2021-04-06 04:40:04 +01:00
|
|
|
tw.WriteLine(inUse, name, address, accesses[name])
|
2021-03-31 16:56:34 +01:00
|
|
|
} else {
|
2021-04-06 04:40:04 +01:00
|
|
|
tw.WriteLine(inUse, name, address)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|