-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyusage_value.go
49 lines (41 loc) · 966 Bytes
/
keyusage_value.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
package cmd
import (
"crypto/x509"
"fmt"
"strings"
"github.com/dvob/pcert"
"github.com/spf13/cobra"
)
type keyUsageValue struct {
value *x509.KeyUsage
}
func newKeyUsageValue(ku *x509.KeyUsage) *keyUsageValue {
return &keyUsageValue{
value: ku,
}
}
func (ku *keyUsageValue) Type() string {
return "usage"
}
func (ku *keyUsageValue) String() string {
return pcert.KeyUsageToString(*ku.value)
}
func (ku *keyUsageValue) Set(usageStr string) error {
var newUsages x509.KeyUsage
for _, u := range strings.Split(usageStr, ",") {
x509KeyUsage, ok := pcert.KeyUsages[u]
if !ok {
return fmt.Errorf("unknown usage: %s", u)
}
newUsages |= x509KeyUsage
}
*ku.value |= newUsages
return nil
}
func keyUsageCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
out := []string{}
for u := range pcert.KeyUsages {
out = append(out, u)
}
return out, cobra.ShellCompDirectiveNoFileComp
}