Debian Codegraph @

cmdline/token/token.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package token
18
19 import (
20 "errors"
21 "fmt"
22 "os"
23 "sort"
24 "strings"
25
26 "github.com/spf13/cobra"
27
28 "github.com/sassoftware/relic/v7/cmdline/shared"
29 "github.com/sassoftware/relic/v7/config"
30 "github.com/sassoftware/relic/v7/token"
31 "github.com/sassoftware/relic/v7/token/open"
32 )
33
34 var TokenCmd = &cobra.Command{
35 Use: "token",
36 Short: "View and manipulate token objects",
37 }
38
39 var TokensCmd = &cobra.Command{
40 Use: "list",
41 Short: "List tokens provided by a driver",
42 RunE: tokensCmd,
43 }
44
45 var ContentsCmd = &cobra.Command{
46 Use: "contents",
47 Short: "List keys in a token",
48 RunE: contentsCmd,
49 }
50
51 var (
52 argType string
53 argProvider string
54 argId string
55 argValues bool
56 )
57
58 func init() {
59 shared.RootCmd.AddCommand(TokenCmd)
60 TokenCmd.PersistentFlags().StringVarP(&argToken, "token", "t", "", "Name of token")
61 TokenCmd.PersistentFlags().StringVar(&argProvider, "provider", "", "Provider module path")
62
63 TokenCmd.AddCommand(TokensCmd)
64
65 TokenCmd.AddCommand(ContentsCmd)
66 ContentsCmd.Flags().StringVarP(&argLabel, "label", "l", "", "Display objects with this label only")
67 ContentsCmd.Flags().StringVarP(&argId, "id", "i", "", "Display objects with this ID only")
68 ContentsCmd.Flags().BoolVarP(&argValues, "values", "v", false, "Show contents of objects")
69
70 shared.AddLateHook(addProviderTypeHelp) // deferred so token providers can init()
71 }
72
73 func addProviderTypeHelp() {
74 var listable []string
75 for ptype := range token.Listers {
76 listable = append(listable, ptype)
77 }
78 sort.Strings(listable)
79 TokenCmd.PersistentFlags().StringVar(&argType, "type", "", fmt.Sprintf("Provider type (%s)", strings.Join(listable, ", ")))
80 }
81
82 func tokensCmd(cmd *cobra.Command, args []string) error {
83 if argToken == "" && (argType == "" || argProvider == "") {
84 return errors.New("--token, or --type and --provider, are required")
85 }
86 if err := shared.InitConfig(); err != nil {
87 return err
88 }
89 if argToken != "" {
90 tokenConf, err := shared.CurrentConfig.GetToken(argToken)
91 if err != nil {
92 return err
93 }
94 if argType == "" {
95 argType = tokenConf.Type
96 }
97 if argProvider == "" {
98 argProvider = tokenConf.Provider
99 }
100 }
101 return shared.Fail(open.List(argType, argProvider, os.Stdout))
102 }
103
104 func contentsCmd(cmd *cobra.Command, args []string) error {
105 if argToken == "" && (argType == "" || argProvider == "") {
106 return errors.New("--token, or --type and --provider, are required")
107 }
108 if err := shared.InitConfig(); err != nil {
109 return err
110 }
111 var tokenConf *config.TokenConfig
112 if argToken != "" {
113 var err error
114 tokenConf, err = shared.CurrentConfig.GetToken(argToken)
115 if err != nil {
116 return err
117 }
118 } else {
119 argToken = ":new-token:"
120 tokenConf = shared.CurrentConfig.NewToken(argToken)
121 }
122 if argType != "" {
123 tokenConf.Type = argType
124 }
125 if argProvider != "" {
126 tokenConf.Provider = argProvider
127 }
128 tok, err := openToken(argToken)
129 if err != nil {
130 return err
131 }
132 return shared.Fail(tok.ListKeys(token.ListOptions{
133 Output: os.Stdout,
134 Label: argLabel,
135 ID: argId,
136 Values: argValues,
137 }))
138 }