-
Notifications
You must be signed in to change notification settings - Fork 22
/
parsing.go
82 lines (63 loc) · 2.1 KB
/
parsing.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
81
82
package main
import (
"fmt"
"io/fs"
"strings"
"github.com/filecoin-project/go-address"
"github.com/google/uuid"
cli "github.com/urfave/cli/v2"
)
// Read a single miner from the CLI, returning address.Undef if none is
// provided.
func parseMiner(cctx *cli.Context) (address.Address, error) {
minerStringRaw := cctx.String(flagMiner.Name)
miner, err := address.NewFromString(minerStringRaw)
if err != nil {
return address.Undef, fmt.Errorf("failed to parse miner: %s: %w", minerStringRaw, err)
}
return miner, nil
}
// Read a comma-separated or multi flag list of miners from the CLI.
func parseMiners(cctx *cli.Context) ([]address.Address, error) {
// Each minerStringsRaw element may contain multiple comma-separated values
minerStringsRaw := cctx.StringSlice(flagMiners.Name)
// Split any comma-separated minerStringsRaw elements
var minerStrings []string
for _, raw := range minerStringsRaw {
minerStrings = append(minerStrings, strings.Split(raw, ",")...)
}
var miners []address.Address
for _, ms := range minerStrings {
miner, err := address.NewFromString(ms)
if err != nil {
return nil, fmt.Errorf("failed to parse miner %s: %w", ms, err)
}
miners = append(miners, miner)
}
return miners, nil
}
// Get whether to use a verified deal or not.
func parseVerified(cctx *cli.Context) bool {
return cctx.Bool(flagVerified.Name)
}
// Get the destination file to write the output to, erroring if not a valid
// path. This early error check is important because you don't want to do a
// bunch of work, only to end up crashing when you try to write the file.
func parseOutput(cctx *cli.Context) (string, error) {
path := cctx.String(flagOutput.Name)
if path != "" && !fs.ValidPath(path) {
return "", fmt.Errorf("invalid output location '%s'", path)
}
return path, nil
}
func parseDealUUID(cctx *cli.Context) (uuid.UUID, error) {
dealUUIDStr := cctx.String(flagDealUUID.Name)
if dealUUIDStr == "" {
return uuid.Nil, nil
}
dealUUID, err := uuid.Parse(dealUUIDStr)
if err != nil {
return uuid.Nil, fmt.Errorf("failed to parse deal UUID '%s'", dealUUIDStr)
}
return dealUUID, nil
}