forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
190 lines (155 loc) · 5.46 KB
/
cli.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Code generated by go-swagger; DO NOT EDIT.
package cli
// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"github.com/circl-dev/go-swagger/examples/cli/client"
"github.com/go-openapi/strfmt"
homedir "github.com/mitchellh/go-homedir"
"github.com/circl-dev/runtime"
httptransport "github.com/circl-dev/runtime/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// debug flag indicating that cli should output debug logs
var debug bool
// config file location
var configFile string
// dry run flag
var dryRun bool
// name of the executable
var exeName string = filepath.Base(os.Args[0])
// logDebugf writes debug log to stdout
func logDebugf(format string, v ...interface{}) {
if !debug {
return
}
log.Printf(format, v...)
}
// depth of recursion to construct model flags
var maxDepth int = 5
// makeClient constructs a client object
func makeClient(cmd *cobra.Command, args []string) (*client.AToDoListApplication, error) {
hostname := viper.GetString("hostname")
scheme := viper.GetString("scheme")
r := httptransport.New(hostname, client.DefaultBasePath, []string{scheme})
r.SetDebug(debug)
// set custom producer and consumer to use the default ones
r.Consumers["application/io.goswagger.examples.todo-list.v1+json"] = runtime.JSONConsumer()
r.Producers["application/io.goswagger.examples.todo-list.v1+json"] = runtime.JSONProducer()
auth, err := makeAuthInfoWriter(cmd)
if err != nil {
return nil, err
}
r.DefaultAuthentication = auth
appCli := client.New(r, strfmt.Default)
logDebugf("Server url: %v://%v", scheme, hostname)
return appCli, nil
}
// MakeRootCmd returns the root cmd
func MakeRootCmd() (*cobra.Command, error) {
cobra.OnInitialize(initViperConfigs)
// Use executable name as the command name
rootCmd := &cobra.Command{
Use: exeName,
}
// register basic flags
rootCmd.PersistentFlags().String("hostname", client.DefaultHost, "hostname of the service")
viper.BindPFlag("hostname", rootCmd.PersistentFlags().Lookup("hostname"))
rootCmd.PersistentFlags().String("scheme", client.DefaultSchemes[0], fmt.Sprintf("Choose from: %v", client.DefaultSchemes))
viper.BindPFlag("scheme", rootCmd.PersistentFlags().Lookup("scheme"))
// configure debug flag
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "output debug logs")
// configure config location
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file path")
// configure dry run flag
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "do not send the request to server")
// register security flags
if err := registerAuthInoWriterFlags(rootCmd); err != nil {
return nil, err
}
// add all operation groups
operationGroupTodosCmd, err := makeOperationGroupTodosCmd()
if err != nil {
return nil, err
}
rootCmd.AddCommand(operationGroupTodosCmd)
// add cobra completion
rootCmd.AddCommand(makeGenCompletionCmd())
return rootCmd, nil
}
// initViperConfigs initialize viper config using config file in '$HOME/.config/<cli name>/config.<json|yaml...>'
// currently hostname, scheme and auth tokens can be specified in this config file.
func initViperConfigs() {
if configFile != "" {
// use user specified config file location
viper.SetConfigFile(configFile)
} else {
// look for default config
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(path.Join(home, ".config", exeName))
viper.SetConfigName("config")
}
if err := viper.ReadInConfig(); err != nil {
logDebugf("Error: loading config file: %v", err)
return
}
logDebugf("Using config file: %v", viper.ConfigFileUsed())
}
// registerAuthInoWriterFlags registers all flags needed to perform authentication
func registerAuthInoWriterFlags(cmd *cobra.Command) error {
/*x-todolist-token */
cmd.PersistentFlags().String("x-todolist-token", "", ``)
viper.BindPFlag("x-todolist-token", cmd.PersistentFlags().Lookup("x-todolist-token"))
return nil
}
// makeAuthInfoWriter retrieves cmd flags and construct an auth info writer
func makeAuthInfoWriter(cmd *cobra.Command) (runtime.ClientAuthInfoWriter, error) {
auths := []runtime.ClientAuthInfoWriter{}
/*x-todolist-token */
if viper.IsSet("x-todolist-token") {
XTodolistTokenKey := viper.GetString("x-todolist-token")
auths = append(auths, httptransport.APIKeyAuth("x-todolist-token", "header", XTodolistTokenKey))
}
if len(auths) == 0 {
logDebugf("Warning: No auth params detected.")
return nil, nil
}
// compose all auths together
return httptransport.Compose(auths...), nil
}
func makeOperationGroupTodosCmd() (*cobra.Command, error) {
operationGroupTodosCmd := &cobra.Command{
Use: "todos",
Long: ``,
}
operationAddOneCmd, err := makeOperationTodosAddOneCmd()
if err != nil {
return nil, err
}
operationGroupTodosCmd.AddCommand(operationAddOneCmd)
operationDestroyOneCmd, err := makeOperationTodosDestroyOneCmd()
if err != nil {
return nil, err
}
operationGroupTodosCmd.AddCommand(operationDestroyOneCmd)
operationFindTodosCmd, err := makeOperationTodosFindTodosCmd()
if err != nil {
return nil, err
}
operationGroupTodosCmd.AddCommand(operationFindTodosCmd)
operationUpdateOneCmd, err := makeOperationTodosUpdateOneCmd()
if err != nil {
return nil, err
}
operationGroupTodosCmd.AddCommand(operationUpdateOneCmd)
return operationGroupTodosCmd, nil
}