-
Notifications
You must be signed in to change notification settings - Fork 394
/
operator.go
87 lines (72 loc) · 2.42 KB
/
operator.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
83
84
85
86
87
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package operator
import (
"fmt"
"regexp"
"strings"
"github.com/spf13/pflag"
"go.uber.org/zap"
"storj.io/storj/private/nodeoperator"
)
// Config defines properties related to storage node operator metadata.
type Config struct {
Email string `user:"true" help:"operator email address" default:""`
Wallet string `user:"true" help:"operator wallet address" default:""`
WalletFeatures WalletFeatures `user:"true" help:"operator wallet features" default:""`
}
// Verify verifies whether operator config is valid.
func (c Config) Verify(log *zap.Logger) error {
if err := isOperatorEmailValid(log, c.Email); err != nil {
return err
}
if err := isOperatorWalletValid(log, c.Wallet); err != nil {
return err
}
if err := isOperatorWalletFeaturesValid(log, c.WalletFeatures); err != nil {
return err
}
return nil
}
func isOperatorEmailValid(log *zap.Logger, email string) error {
if email == "" {
log.Warn("Operator email address isn't specified.")
} else {
log.Info("Operator email", zap.String("Address", email))
}
return nil
}
func isOperatorWalletValid(log *zap.Logger, wallet string) error {
if wallet == "" {
return fmt.Errorf("operator wallet address isn't specified")
}
r := regexp.MustCompile("^0x[a-fA-F0-9]{40}$")
if match := r.MatchString(wallet); !match {
return fmt.Errorf("operator wallet address isn't valid")
}
log.Info("Operator wallet", zap.String("Address", wallet))
return nil
}
// isOperatorWalletFeaturesValid checks if wallet features list does not exceed length limits.
func isOperatorWalletFeaturesValid(log *zap.Logger, features WalletFeatures) error {
return nodeoperator.DefaultWalletFeaturesValidation.Validate(features)
}
// ensure WalletFeatures implements pflag.Value.
var _ pflag.Value = (*WalletFeatures)(nil)
// WalletFeatures payout opt-in wallet features list.
type WalletFeatures []string
// String returns the comma separated list of wallet features.
func (features WalletFeatures) String() string {
return strings.Join(features, ",")
}
// Set implements pflag.Value by parsing a comma separated list of wallet features.
func (features *WalletFeatures) Set(value string) error {
if value != "" {
*features = strings.Split(value, ",")
}
return nil
}
// Type returns the type of the pflag.Value.
func (features WalletFeatures) Type() string {
return "wallet-features"
}