forked from bazil/bazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyparam.go
53 lines (46 loc) · 1.04 KB
/
keyparam.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
package flagx
import (
"encoding/hex"
"errors"
"flag"
"bazil.org/bazil/cas"
)
// KeyParam is a wrapper for cas.Key that is compatible with the
// flag.Value interface, without compromising the immutability promise
// of Key.
type KeyParam struct {
key cas.Key
}
var _ = flag.Value(&KeyParam{})
// String returns a hex representation of the key.
//
// See flag.Value.String and cas.Key.String.
func (kp KeyParam) String() string {
return kp.key.String()
}
// Set updates the contents of the key based on the given hex-encoded
// string.
//
// See flag.Value.Set.
func (kp *KeyParam) Set(s string) error {
buf, err := hex.DecodeString(s)
if err != nil {
return err
}
if len(buf) != cas.KeySize {
return &cas.BadKeySizeError{Key: buf}
}
k := cas.NewKey(buf)
if k == cas.Invalid {
return errors.New("bad key format")
}
kp.key = k
return nil
}
// Key returns a copy of the current value of the KeyParam.
//
// As usual with cas.Key, the returned value is promised to be
// immutable.
func (kp *KeyParam) Key() cas.Key {
return kp.key
}