This repository has been archived by the owner on May 23, 2023. It is now read-only.
/
root.go
141 lines (120 loc) · 4.68 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/apigee/istio-mixer-adapter/apigee-istio/cmd/bindings"
"github.com/apigee/istio-mixer-adapter/apigee-istio/cmd/provision"
"github.com/apigee/istio-mixer-adapter/apigee-istio/cmd/token"
"github.com/apigee/istio-mixer-adapter/apigee-istio/shared"
"github.com/spf13/cobra"
)
func init() {
// Apigee does not support http2 at present
if !strings.Contains(os.Getenv("GODEBUG"), "http2client=0") {
os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",http2client=0")
}
}
// GetRootCmd returns the root of the cobra command-tree.
func GetRootCmd(args []string, printf, fatalf shared.FormatFn) *cobra.Command {
rootArgs := &shared.RootArgs{}
c := &cobra.Command{
Use: "apigee-istio",
Short: "Utility to work with Apigee and Istio.",
Long: "This command lets you interact with Apigee and Istio.",
}
c.SetArgs(args)
c.PersistentFlags().AddGoFlagSet(flag.CommandLine)
var addCommand = func(cmds ...*cobra.Command) {
for _, subC := range cmds {
// add general flags
subC.PersistentFlags().StringVarP(&rootArgs.RouterBase, "routerBase", "r",
shared.DefaultRouterBase, "Apigee router base")
subC.PersistentFlags().StringVarP(&rootArgs.ManagementBase, "managementBase", "m",
shared.DefaultManagementBase, "Apigee management base")
subC.PersistentFlags().BoolVarP(&rootArgs.Verbose, "verbose", "v",
false, "verbose output")
subC.PersistentFlags().BoolVarP(&rootArgs.IsHybrid, "hybrid", "y",
false, "Apigee hybrid (automatically sets management base)")
subC.PersistentFlags().StringVarP(&rootArgs.NetrcPath, "netrc", "n",
"", "Path to a .netrc file to use (default is $HOME/.netrc")
subC.PersistentFlags().StringVarP(&rootArgs.Org, "org", "o",
"", "Apigee organization name")
subC.PersistentFlags().StringVarP(&rootArgs.Env, "env", "e",
"", "Apigee environment name")
subC.PersistentFlags().StringVarP(&rootArgs.Username, "username", "u",
"", "Apigee username")
subC.PersistentFlags().StringVarP(&rootArgs.Password, "password", "p",
"", "Apigee password")
subC.PersistentFlags().StringVarP(&rootArgs.Token, "token", "t",
"", "Apigee OAuth or SAML token")
subC.MarkPersistentFlagRequired("org")
subC.MarkPersistentFlagRequired("env")
c.AddCommand(subC)
}
}
addCommand(provision.Cmd(rootArgs, printf, fatalf))
addCommand(bindings.Cmd(rootArgs, printf, fatalf))
addCommand(token.Cmd(rootArgs, printf, fatalf))
c.AddCommand(version(rootArgs, printf, fatalf))
return c
}
const versionAPIFormat = "%s/version" // internalProxyURL
func version(rootArgs *shared.RootArgs, printf, fatalf shared.FormatFn) *cobra.Command {
subC := &cobra.Command{
Use: "version",
Short: "Prints build version - specify org and env to include proxy version",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return rootArgs.Resolve(true)
},
Run: func(cmd *cobra.Command, args []string) {
printf("apigee-istio version %s %s [%s]",
shared.BuildInfo.Version, shared.BuildInfo.Date, shared.BuildInfo.Commit)
if rootArgs.RouterBase == "https://-.apigee.net" {
return
}
// check proxy version
versionURL := fmt.Sprintf(versionAPIFormat, rootArgs.CustomerProxyURL)
req, err := http.NewRequest(http.MethodGet, versionURL, nil)
if err != nil {
fatalf("error creating request: %v", err)
}
var version versionResponse
resp, err := rootArgs.Client.Do(req, &version)
if err != nil {
fatalf("error getting proxy version: %v", err)
}
if err != nil {
body, _ := ioutil.ReadAll(resp.Body)
fatalf("error getting proxy version. response code: %d, body: %s", resp.StatusCode, string(body))
}
printf("istio-auth proxy version: %v", version.Version)
},
}
subC.PersistentFlags().StringVarP(&rootArgs.RouterBase, "routerBase", "r",
shared.DefaultRouterBase, "Apigee router base")
subC.PersistentFlags().StringVarP(&rootArgs.Org, "org", "o",
"", "Apigee organization name")
subC.PersistentFlags().StringVarP(&rootArgs.Env, "env", "e",
"", "Apigee environment name")
return subC
}
type versionResponse struct {
Version string `json:"version"`
}