-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
126 lines (103 loc) · 3.45 KB
/
root.go
File metadata and controls
126 lines (103 loc) · 3.45 KB
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
package cmd
import (
"os"
"strings"
"github.com/spf13/cobra"
viperpkg "github.com/spf13/viper"
)
const (
EUCIURL = "https://cloudinfra-gw.portal.checkpoint.com"
USCIURL = "https://cloudinfra-gw-us.portal.checkpoint.com"
DevCIURL = "https://dev-cloudinfra-gw.kube1.iaas.checkpoint.com"
CIAuthPath = "/auth/external"
ProdCIAPIV1 = "/app/i2/graphql/V1"
DevCIAPIV1 = "/app/infinity2gem/graphql/V1"
)
var (
clientID string
accessKey string
region string
token string
cfgFile string
)
type graphqlRequest struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}
type graphqlResponse[T any] struct {
Data T `json:"data"`
}
type externalAuthResponseData struct {
Token string `json:"token"`
CSRF string `json:"csrf"`
Expires string `json:"expires"`
ExpiresIn int `json:"expiresIn"`
}
type externalAuthResponse struct {
Success bool `json:"success"`
Data externalAuthResponseData `json:"data"`
}
type lowerCaseStringEnvKeyReplacer struct{}
func (r *lowerCaseStringEnvKeyReplacer) Replace(key string) string {
return strings.ReplaceAll(key, "-", "_")
}
type getTaskResponseGetTask struct {
ID string `json:"id"`
Status string `json:"status"`
}
type getTaskResponseData struct {
GetTask getTaskResponseGetTask `json:"getTask"`
}
type getTaskResponse struct {
Data getTaskResponseData `json:"data"`
}
// rootCmd represents the base command when called without any subcommands
var (
rootCmd = &cobra.Command{
Use: "inext",
Short: "Infinity Next API Command Line Interface",
Long: `Infinity Next API Command Line Interface
For example:
inext publish && inext enforce
`,
}
viper = viperpkg.NewWithOptions(viperpkg.EnvKeyReplacer(&lowerCaseStringEnvKeyReplacer{}))
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(version string) {
rootCmd.Version = version
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVarP(&clientID, "client-id", "c", "", "Client ID of the API key")
rootCmd.PersistentFlags().StringVarP(&accessKey, "access-key", "k", "", "Access key of the API key")
rootCmd.PersistentFlags().StringVarP(®ion, "region", "r", "eu", "Region of Infinity Next API")
rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "Authorization token of the API key")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.inext.yaml)")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)
// Search config in home directory with name ".inext" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".inext")
}
viper.SetEnvPrefix("inext")
viper.AutomaticEnv() // read in environment variables that match INEXT_*
// If a config file is found, read it in.
viper.ReadInConfig() //nolint:errcheck // the error can be ignored, the user doesn't have to provide config file
}