blob: f07e705508de8554fc60445989ce6b4854d23738 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package commands
17
18import (
Scott Baker5281d002019-05-16 10:45:26 -070019 "bufio"
Scott Baker2c0ebda2019-05-06 16:55:47 -070020 b64 "encoding/base64"
Scott Baker6cf525a2019-05-09 12:25:08 -070021 "fmt"
22 "github.com/fullstorydev/grpcurl"
23 "github.com/jhump/protoreflect/grpcreflect"
24 "golang.org/x/net/context"
25 "google.golang.org/grpc"
26 reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
Scott Baker175cb402019-05-17 16:13:06 -070027 "google.golang.org/grpc/status"
Scott Baker5281d002019-05-16 10:45:26 -070028 "log"
29 "os"
30 "strings"
Scott Baker2c0ebda2019-05-06 16:55:47 -070031)
32
33func GenerateHeaders() []string {
34 username := GlobalConfig.Username
35 password := GlobalConfig.Password
36 sEnc := b64.StdEncoding.EncodeToString([]byte(username + ":" + password))
37 headers := []string{"authorization: basic " + sEnc}
38 return headers
39}
Scott Baker6cf525a2019-05-09 12:25:08 -070040
41func InitReflectionClient() (*grpc.ClientConn, grpcurl.DescriptorSource, error) {
42 conn, err := NewConnection()
43 if err != nil {
44 return nil, nil, err
45 }
46
47 refClient := grpcreflect.NewClient(context.Background(), reflectpb.NewServerReflectionClient(conn))
48 defer refClient.Reset()
49
Scott Baker14c8f182019-05-22 18:05:29 -070050 // Intended method of use is to download the protos via reflection API. Loading the
51 // protos from a file is supported for unit testing, as the mock server does not
52 // support the reflection API.
53
54 var descriptor grpcurl.DescriptorSource
55 if GlobalConfig.Protoset != "" {
56 descriptor, err = grpcurl.DescriptorSourceFromProtoSets(GlobalConfig.Protoset)
57 if err != nil {
58 return nil, nil, err
59 }
60 } else {
61 descriptor = grpcurl.DescriptorSourceFromServer(context.Background(), refClient)
62 }
Scott Baker6cf525a2019-05-09 12:25:08 -070063
64 return conn, descriptor, nil
65}
66
67// A makeshift substitute for C's Ternary operator
68func Ternary_uint32(condition bool, value_true uint32, value_false uint32) uint32 {
69 if condition {
70 return value_true
71 } else {
72 return value_false
73 }
74}
75
76// call printf only if visible is True
77func conditional_printf(visible bool, format string, args ...interface{}) {
78 if visible {
79 fmt.Printf(format, args...)
80 }
81}
Scott Baker5281d002019-05-16 10:45:26 -070082
83// Print a confirmation prompt and get a response from the user
84func Confirmf(format string, args ...interface{}) bool {
85 if GlobalOptions.Yes {
86 return true
87 }
88
89 reader := bufio.NewReader(os.Stdin)
90
91 for {
92 msg := fmt.Sprintf(format, args...)
93 fmt.Print(msg)
94
95 response, err := reader.ReadString('\n')
96 if err != nil {
97 log.Fatal(err)
98 }
99
100 response = strings.ToLower(strings.TrimSpace(response))
101
102 if response == "y" || response == "yes" {
103 return true
104 } else if response == "n" || response == "no" {
105 return false
106 }
107 }
108}
Scott Baker175cb402019-05-17 16:13:06 -0700109
110func HumanReadableError(err error) string {
111 st, ok := status.FromError(err)
112 if ok {
113 grpc_message := st.Message()
114 if strings.HasPrefix(grpc_message, "Exception calling application: ") {
115 return st.Message()[31:]
116 } else {
117 return st.Message()
118 }
119 }
120 return err.Error()
121}