-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroot.go
105 lines (87 loc) · 3.97 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
package root
import (
"cf-argo-plugin/cmd/processResult"
"cf-argo-plugin/cmd/rollback"
"cf-argo-plugin/cmd/rollout"
"cf-argo-plugin/cmd/sync"
"cf-argo-plugin/pkg/codefresh"
"cf-argo-plugin/pkg/context"
"fmt"
"github.com/spf13/cobra"
"os"
)
type authContext struct {
CodefreshToken string
CodefreshHost string
CodefreshIntegration string
ArgoUsername string
ArgoPassword string
ArgoHost string
ArgoToken string
BasicAuth bool
}
var pluginAuthContext = &authContext{}
var rootCmd = &cobra.Command{
Use: "cf-argo-plugin",
Short: "Codefresh plugin for easy interact with argocd",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return nil
},
PersistentPreRunE: fetchArgoCredentials,
}
func init() {
pf := rootCmd.PersistentFlags()
pf.StringVar(&pluginAuthContext.CodefreshToken, "cf-token", "", "Api token from Codefresh")
pf.StringVar(&pluginAuthContext.CodefreshHost, "cf-host", "https://g.codefresh.io", "Host of Codefresh")
pf.StringVar(&pluginAuthContext.CodefreshIntegration, "cf-integration", "", "Name of Argo integration on Codefresh")
pf.StringVar(&pluginAuthContext.ArgoUsername, "argo-username", "", "Username for argo cd, use only if you not provide integration")
pf.StringVar(&pluginAuthContext.ArgoPassword, "argo-password", "", "Password for argo cd, use only if you not provide integration")
pf.StringVar(&pluginAuthContext.ArgoToken, "argo-token", "", "Token for argo cd, use only if you not provide integration")
pf.StringVar(&pluginAuthContext.ArgoHost, "argo-host", "", "Host for argo cd, use only if you not provide integration")
pf.BoolVar(&pluginAuthContext.BasicAuth, "basic-auth", false, "Use ArgoCD username/password as primary credentials")
pf.StringVar(&context.PluginOutConfig.CommandsFile, "out-commands-file", "", "Write main commands to file")
pf.StringVar(&context.PluginOutConfig.ExportOutUrlCommand, "out-export-file", "", "Write export commands to file")
pf.StringVar(&context.PluginOutConfig.CustomOutputUrl, "custom-external-link", "", "Custom link that Codefresh is showing inside build view")
rootCmd.AddCommand(sync.Cmd)
rootCmd.AddCommand(rollout.Cmd)
rootCmd.AddCommand(processResult.WaitRolloutCmd)
rootCmd.AddCommand(rollback.Cmd)
}
func fetchArgoCredentials(cmd *cobra.Command, args []string) error {
context.PluginCodefreshCredentials.Host = pluginAuthContext.CodefreshHost
context.PluginCodefreshCredentials.Token = pluginAuthContext.CodefreshToken
context.PluginCodefreshCredentials.Integration = pluginAuthContext.CodefreshIntegration
if pluginAuthContext.ArgoUsername != "" && pluginAuthContext.ArgoPassword != "" && pluginAuthContext.ArgoHost != "" {
context.PluginArgoCredentials.Host = pluginAuthContext.ArgoHost
context.PluginArgoCredentials.Username = pluginAuthContext.ArgoUsername
context.PluginArgoCredentials.Password = pluginAuthContext.ArgoPassword
} else if pluginAuthContext.CodefreshToken != "" && pluginAuthContext.CodefreshIntegration != "" {
codefreshApi := codefresh.New(&codefresh.ClientOptions{
Token: pluginAuthContext.CodefreshToken,
Host: pluginAuthContext.CodefreshHost,
})
integration, err := codefreshApi.GetIntegration(pluginAuthContext.CodefreshIntegration)
if err != nil {
return fmt.Errorf("failed to retrive argo integration, %s", err.Error())
}
context.PluginArgoCredentials.Host = integration.Data.Url
context.PluginArgoCredentials.Username = integration.Data.Username
context.PluginArgoCredentials.Password = integration.Data.Password
context.PluginArgoCredentials.Token = integration.Data.Token
} else if pluginAuthContext.ArgoToken != "" && pluginAuthContext.ArgoHost != "" {
context.PluginArgoCredentials.Token = pluginAuthContext.ArgoToken
context.PluginArgoCredentials.Host = pluginAuthContext.ArgoHost
}
context.PluginArgoCredentials.BasicAuth = pluginAuthContext.BasicAuth
return nil
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}