blob: 9461f9945c28ee267715d8e494b0e3c571e07c51 [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 (
19 b64 "encoding/base64"
Scott Baker6cf525a2019-05-09 12:25:08 -070020 "fmt"
21 "github.com/fullstorydev/grpcurl"
22 "github.com/jhump/protoreflect/grpcreflect"
23 "golang.org/x/net/context"
24 "google.golang.org/grpc"
25 reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
Scott Baker2c0ebda2019-05-06 16:55:47 -070026)
27
28func GenerateHeaders() []string {
29 username := GlobalConfig.Username
30 password := GlobalConfig.Password
31 sEnc := b64.StdEncoding.EncodeToString([]byte(username + ":" + password))
32 headers := []string{"authorization: basic " + sEnc}
33 return headers
34}
Scott Baker6cf525a2019-05-09 12:25:08 -070035
36func InitReflectionClient() (*grpc.ClientConn, grpcurl.DescriptorSource, error) {
37 conn, err := NewConnection()
38 if err != nil {
39 return nil, nil, err
40 }
41
42 refClient := grpcreflect.NewClient(context.Background(), reflectpb.NewServerReflectionClient(conn))
43 defer refClient.Reset()
44
45 descriptor := grpcurl.DescriptorSourceFromServer(context.Background(), refClient)
46
47 return conn, descriptor, nil
48}
49
50// A makeshift substitute for C's Ternary operator
51func Ternary_uint32(condition bool, value_true uint32, value_false uint32) uint32 {
52 if condition {
53 return value_true
54 } else {
55 return value_false
56 }
57}
58
59// call printf only if visible is True
60func conditional_printf(visible bool, format string, args ...interface{}) {
61 if visible {
62 fmt.Printf(format, args...)
63 }
64}