-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
metric_hash.go
108 lines (88 loc) · 1.84 KB
/
metric_hash.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/urfave/cli"
"github.com/tidepool-org/platform/application"
"github.com/tidepool-org/platform/errors"
"github.com/tidepool-org/platform/metric"
"github.com/tidepool-org/platform/tool"
)
const SaltFlag = "salt"
func main() {
application.Run(NewTool())
}
type Tool struct {
*tool.Tool
salt string
}
func NewTool() (*Tool, error) {
tuel, err := tool.New("TIDEPOOL")
if err != nil {
return nil, err
}
return &Tool{
Tool: tuel,
}, nil
}
func (t *Tool) Initialize() error {
if err := t.Tool.Initialize(); err != nil {
return err
}
t.CLI().Usage = "Generate metric hash"
t.CLI().Authors = []cli.Author{
{
Name: "Darin Krauss",
Email: "darin@tidepool.org",
},
}
t.CLI().Flags = append(t.CLI().Flags,
cli.StringFlag{
Name: SaltFlag,
Usage: "metric salt",
},
)
t.CLI().Action = func(context *cli.Context) error {
if !t.ParseContext(context) {
return nil
}
return t.execute()
}
return nil
}
func (t *Tool) ParseContext(context *cli.Context) bool {
if parsed := t.Tool.ParseContext(context); !parsed {
return parsed
}
t.salt = t.ConfigReporter().WithScopes("metric").GetWithDefault("salt", "")
if context.IsSet(SaltFlag) {
t.salt = context.String(SaltFlag)
}
return true
}
func (t *Tool) execute() error {
if t.salt == "" {
return errors.New("salt is missing")
}
var reader io.Reader
if len(t.Args()) > 0 {
reader = strings.NewReader(strings.Join(t.Args(), "\n"))
} else {
reader = os.Stdin
}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
result, err := metric.HashFromUserID(scanner.Text(), t.salt)
if err != nil {
return errors.Wrap(err, "unable to process input")
}
fmt.Println(result)
}
if err := scanner.Err(); err != nil {
return errors.Wrap(err, "unable to read input")
}
return nil
}