/
metric_hash.go
103 lines (84 loc) · 1.78 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
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.RunAndExit(NewTool())
}
type Tool struct {
*tool.Tool
salt string
}
func NewTool() *Tool {
return &Tool{
Tool: tool.New(),
}
}
func (t *Tool) Initialize(provider application.Provider) error {
if err := t.Tool.Initialize(provider); 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(ctx *cli.Context) error {
if !t.ParseContext(ctx) {
return nil
}
return t.execute()
}
return nil
}
func (t *Tool) ParseContext(ctx *cli.Context) bool {
if parsed := t.Tool.ParseContext(ctx); !parsed {
return parsed
}
t.salt = t.ConfigReporter().WithScopes("metric").GetWithDefault("salt", "")
if ctx.IsSet(SaltFlag) {
t.salt = ctx.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
}