blob: 93c1b5d27bfde026b123b2321e3402fee304f472 [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 Baker5281d002019-05-16 10:45:26 -070027 "log"
28 "os"
29 "strings"
Scott Baker2c0ebda2019-05-06 16:55:47 -070030)
31
32func GenerateHeaders() []string {
33 username := GlobalConfig.Username
34 password := GlobalConfig.Password
35 sEnc := b64.StdEncoding.EncodeToString([]byte(username + ":" + password))
36 headers := []string{"authorization: basic " + sEnc}
37 return headers
38}
Scott Baker6cf525a2019-05-09 12:25:08 -070039
40func InitReflectionClient() (*grpc.ClientConn, grpcurl.DescriptorSource, error) {
41 conn, err := NewConnection()
42 if err != nil {
43 return nil, nil, err
44 }
45
46 refClient := grpcreflect.NewClient(context.Background(), reflectpb.NewServerReflectionClient(conn))
47 defer refClient.Reset()
48
49 descriptor := grpcurl.DescriptorSourceFromServer(context.Background(), refClient)
50
51 return conn, descriptor, nil
52}
53
54// A makeshift substitute for C's Ternary operator
55func Ternary_uint32(condition bool, value_true uint32, value_false uint32) uint32 {
56 if condition {
57 return value_true
58 } else {
59 return value_false
60 }
61}
62
63// call printf only if visible is True
64func conditional_printf(visible bool, format string, args ...interface{}) {
65 if visible {
66 fmt.Printf(format, args...)
67 }
68}
Scott Baker5281d002019-05-16 10:45:26 -070069
70// Print a confirmation prompt and get a response from the user
71func Confirmf(format string, args ...interface{}) bool {
72 if GlobalOptions.Yes {
73 return true
74 }
75
76 reader := bufio.NewReader(os.Stdin)
77
78 for {
79 msg := fmt.Sprintf(format, args...)
80 fmt.Print(msg)
81
82 response, err := reader.ReadString('\n')
83 if err != nil {
84 log.Fatal(err)
85 }
86
87 response = strings.ToLower(strings.TrimSpace(response))
88
89 if response == "y" || response == "yes" {
90 return true
91 } else if response == "n" || response == "no" {
92 return false
93 }
94 }
95}