blob: 98103009827cd764d172eb116242f67953d11f46 [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
50 descriptor := grpcurl.DescriptorSourceFromServer(context.Background(), refClient)
51
52 return conn, descriptor, nil
53}
54
55// A makeshift substitute for C's Ternary operator
56func Ternary_uint32(condition bool, value_true uint32, value_false uint32) uint32 {
57 if condition {
58 return value_true
59 } else {
60 return value_false
61 }
62}
63
64// call printf only if visible is True
65func conditional_printf(visible bool, format string, args ...interface{}) {
66 if visible {
67 fmt.Printf(format, args...)
68 }
69}
Scott Baker5281d002019-05-16 10:45:26 -070070
71// Print a confirmation prompt and get a response from the user
72func Confirmf(format string, args ...interface{}) bool {
73 if GlobalOptions.Yes {
74 return true
75 }
76
77 reader := bufio.NewReader(os.Stdin)
78
79 for {
80 msg := fmt.Sprintf(format, args...)
81 fmt.Print(msg)
82
83 response, err := reader.ReadString('\n')
84 if err != nil {
85 log.Fatal(err)
86 }
87
88 response = strings.ToLower(strings.TrimSpace(response))
89
90 if response == "y" || response == "yes" {
91 return true
92 } else if response == "n" || response == "no" {
93 return false
94 }
95 }
96}
Scott Baker175cb402019-05-17 16:13:06 -070097
98func HumanReadableError(err error) string {
99 st, ok := status.FromError(err)
100 if ok {
101 grpc_message := st.Message()
102 if strings.HasPrefix(grpc_message, "Exception calling application: ") {
103 return st.Message()[31:]
104 } else {
105 return st.Message()
106 }
107 }
108 return err.Error()
109}