Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package commands |
| 17 | |
| 18 | import ( |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 19 | "bufio" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 20 | b64 "encoding/base64" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 21 | "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 Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 27 | "google.golang.org/grpc/status" |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 28 | "log" |
| 29 | "os" |
| 30 | "strings" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | func 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 40 | |
| 41 | func 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 Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 50 | // 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 Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 63 | |
| 64 | return conn, descriptor, nil |
| 65 | } |
| 66 | |
| 67 | // A makeshift substitute for C's Ternary operator |
| 68 | func 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 |
| 77 | func conditional_printf(visible bool, format string, args ...interface{}) { |
| 78 | if visible { |
| 79 | fmt.Printf(format, args...) |
| 80 | } |
| 81 | } |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 82 | |
| 83 | // Print a confirmation prompt and get a response from the user |
| 84 | func 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 Baker | 175cb40 | 2019-05-17 16:13:06 -0700 | [diff] [blame] | 109 | |
| 110 | func 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 | } |