blob: 9741e514fdf33a1da4e7216e9034764c051c8394 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package commands
18
19import (
20 "encoding/json"
21 "fmt"
22 "github.com/opencord/cordctl/format"
23 "google.golang.org/grpc"
24 "gopkg.in/yaml.v2"
25 "io/ioutil"
26 "log"
27 "os"
28 "strings"
29 "time"
30)
31
32type OutputType uint8
33
34const (
35 OUTPUT_TABLE OutputType = iota
36 OUTPUT_JSON
37 OUTPUT_YAML
38)
39
40var CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n")
41
42type GrpcConfigSpec struct {
43 Timeout time.Duration `yaml:"timeout"`
44}
45
46type TlsConfigSpec struct {
47 UseTls bool `yaml:"useTls"`
48 CACert string `yaml:"caCert"`
49 Cert string `yaml:"cert"`
50 Key string `yaml:"key"`
51 Verify string `yaml:"verify"`
52}
53
54type GlobalConfigSpec struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070055 Server string `yaml:"server"`
56 Username string `yaml:"username"`
57 Password string `yaml:"password"`
58 Tls TlsConfigSpec `yaml:"tls"`
59 Grpc GrpcConfigSpec
Scott Baker2c0ebda2019-05-06 16:55:47 -070060}
61
62var GlobalConfig = GlobalConfigSpec{
Scott Baker6cf525a2019-05-09 12:25:08 -070063 Server: "localhost",
Scott Baker2c0ebda2019-05-06 16:55:47 -070064 Tls: TlsConfigSpec{
65 UseTls: false,
66 },
67 Grpc: GrpcConfigSpec{
68 Timeout: time.Second * 10,
69 },
70}
71
72var GlobalOptions struct {
Scott Baker6cf525a2019-05-09 12:25:08 -070073 Config string `short:"c" long:"config" env:"CORDCONFIG" value-name:"FILE" default:"" description:"Location of client config file"`
74 Server string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"`
75 Username string `short:"u" long:"username" value-name:"USERNAME" default:"" description:"Username to authenticate with XOS"`
76 Password string `short:"p" long:"password" value-name:"PASSWORD" default:"" description:"Password to authenticate with XOS"`
77 Debug bool `short:"d" long:"debug" description:"Enable debug mode"`
78 UseTLS bool `long:"tls" description:"Use TLS"`
79 CACert string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"`
80 Cert string `long:"tlscert" value-name:"CERT_FILE" description:"Path to TLS vertificate file"`
81 Key string `long:"tlskey" value-name:"KEY_FILE" description:"Path to TLS key file"`
82 Verify bool `long:"tlsverify" description:"Use TLS and verify the remote"`
Scott Baker2c0ebda2019-05-06 16:55:47 -070083}
84
85type OutputOptions struct {
86 Format string `long:"format" value-name:"FORMAT" default:"" description:"Format to use to output structured data"`
87 Quiet bool `short:"q" long:"quiet" description:"Output only the IDs of the objects"`
88 OutputAs string `short:"o" long:"outputas" default:"table" choice:"table" choice:"json" choice:"yaml" description:"Type of output to generate"`
89}
90
91func toOutputType(in string) OutputType {
92 switch in {
93 case "table":
94 fallthrough
95 default:
96 return OUTPUT_TABLE
97 case "json":
98 return OUTPUT_JSON
99 case "yaml":
100 return OUTPUT_YAML
101 }
102}
103
104type CommandResult struct {
105 Format format.Format
106 OutputAs OutputType
107 Data interface{}
108}
109
110type config struct {
Scott Baker6cf525a2019-05-09 12:25:08 -0700111 Server string `yaml:"server"`
Scott Baker2c0ebda2019-05-06 16:55:47 -0700112}
113
114func NewConnection() (*grpc.ClientConn, error) {
115 if len(GlobalOptions.Config) == 0 {
116 home := os.Getenv("HOME")
117 // TODO: Replace after Jenkins updated to go 1.12
118 /*
119 home, err := os.UserHomeDir()
120 if err != nil {
121 log.Printf("Unable to discover they users home directory: %s\n", err)
122 }
123 */
124 GlobalOptions.Config = fmt.Sprintf("%s/.cord/config", home)
125 }
126
127 info, err := os.Stat(GlobalOptions.Config)
128 if err == nil && !info.IsDir() {
129 configFile, err := ioutil.ReadFile(GlobalOptions.Config)
130 if err != nil {
131 log.Printf("configFile.Get err #%v ", err)
132 }
133 err = yaml.Unmarshal(configFile, &GlobalConfig)
134 if err != nil {
135 log.Fatalf("Unmarshal: %v", err)
136 }
137 }
138
139 // Override from command line
140 if GlobalOptions.Server != "" {
141 GlobalConfig.Server = GlobalOptions.Server
142 }
Scott Baker2c0ebda2019-05-06 16:55:47 -0700143 if GlobalOptions.Username != "" {
144 GlobalConfig.Username = GlobalOptions.Username
145 }
146 if GlobalOptions.Password != "" {
147 GlobalConfig.Password = GlobalOptions.Password
148 }
149
150 return grpc.Dial(GlobalConfig.Server, grpc.WithInsecure())
151}
152
153func GenerateOutput(result *CommandResult) {
154 if result != nil && result.Data != nil {
155 if result.OutputAs == OUTPUT_TABLE {
156 tableFormat := format.Format(result.Format)
157 tableFormat.Execute(os.Stdout, true, result.Data)
158 } else if result.OutputAs == OUTPUT_JSON {
159 asJson, err := json.Marshal(&result.Data)
160 if err != nil {
161 panic(err)
162 }
163 fmt.Printf("%s", asJson)
164 } else if result.OutputAs == OUTPUT_YAML {
165 asYaml, err := yaml.Marshal(&result.Data)
166 if err != nil {
167 panic(err)
168 }
169 fmt.Printf("%s", asYaml)
170 }
171 }
172}