11package commands
22
3- import "github.com/spf13/cobra"
3+ import (
4+ "fmt"
5+ "kool-dev/kool/core/environment"
6+ "kool-dev/kool/services/cloud/api"
7+
8+ "github.com/spf13/cobra"
9+ )
10+
11+ // KoolCloudDeployFlags holds the flags for the kool cloud deploy command
12+ type KoolCloudFlags struct {
13+ Token string // env: KOOL_API_TOKEN
14+ DeployDomain string // env: KOOL_DEPLOY_DOMAIN
15+ }
16+
17+ type Cloud struct {
18+ DefaultKoolService
19+
20+ flags * KoolCloudFlags
21+ env environment.EnvStorage
22+ }
23+
24+ func NewCloud () * Cloud {
25+ return & Cloud {
26+ * newDefaultKoolService (),
27+ & KoolCloudFlags {},
28+ environment .NewEnvStorage (),
29+ }
30+ }
431
532func AddKoolCloud (root * cobra.Command ) {
633 var (
7- cloudCmd = NewCloudCommand ()
34+ cloud = NewCloud ()
35+ cloudCmd = NewCloudCommand (cloud )
836 )
937
10- cloudCmd .AddCommand (NewDeployCommand (NewKoolDeploy ()))
38+ cloudCmd .AddCommand (NewDeployCommand (NewKoolDeploy (cloud )))
1139 cloudCmd .AddCommand (NewDeployExecCommand (NewKoolDeployExec ()))
1240 cloudCmd .AddCommand (NewDeployDestroyCommand (NewKoolDeployDestroy ()))
1341 cloudCmd .AddCommand (NewDeployLogsCommand (NewKoolDeployLogs ()))
@@ -17,15 +45,59 @@ func AddKoolCloud(root *cobra.Command) {
1745}
1846
1947// NewCloudCommand initializes new kool cloud command
20- func NewCloudCommand () (cloudCmd * cobra.Command ) {
48+ func NewCloudCommand (cloud * Cloud ) (cloudCmd * cobra.Command ) {
2149 cloudCmd = & cobra.Command {
2250 Use : "cloud COMMAND [flags]" ,
2351 Short : "Interact with Kool Cloud and manage your deployments." ,
2452 Long : "The cloud subcommand encapsulates a set of APIs to interact with Kool Cloud and deploy, access and tail logs from your deployments." ,
2553 Example : `kool cloud deploy` ,
2654 // add cobra usage help content
2755 DisableFlagsInUseLine : true ,
56+ PersistentPreRunE : func (cmd * cobra.Command , args []string ) (err error ) {
57+ // calls root PersistentPreRunE
58+ var (
59+ requiredFlags bool = cmd .Use != "setup"
60+ root * cobra.Command = cmd
61+ )
62+
63+ for root .HasParent () {
64+ root = root .Parent ()
65+ }
66+ if err = root .PersistentPreRunE (cmd , args ); err != nil {
67+ return
68+ }
69+
70+ if url := cloud .env .Get ("KOOL_API_URL" ); url != "" {
71+ api .SetBaseURL (url )
72+ }
73+
74+ // if no domain is set, we try to get it from the environment
75+ if cloud .flags .DeployDomain == "" && cloud .env .Get ("KOOL_DEPLOY_DOMAIN" ) == "" {
76+ if requiredFlags {
77+ err = fmt .Errorf ("missing deploy domain - please set it via --domain or KOOL_DEPLOY_DOMAIN environment variable" )
78+ return
79+ }
80+ } else if cloud .flags .DeployDomain != "" {
81+ // shares the flag via environment variable
82+ cloud .env .Set ("KOOL_DEPLOY_DOMAIN" , cloud .flags .DeployDomain )
83+ }
84+
85+ // if no token is set, we try to get it from the environment
86+ if cloud .flags .Token == "" && cloud .env .Get ("KOOL_API_TOKEN" ) == "" {
87+ if requiredFlags {
88+ err = fmt .Errorf ("missing Kool Cloud API token - please set it via --token or KOOL_API_TOKEN environment variable" )
89+ return
90+ }
91+ } else if cloud .flags .Token != "" {
92+ cloud .env .Set ("KOOL_API_TOKEN" , cloud .flags .Token )
93+ }
94+
95+ return
96+ },
2897 }
2998
99+ cloudCmd .PersistentFlags ().StringVarP (& cloud .flags .Token , "token" , "" , "" , "Token to authenticate with Kool Cloud API" )
100+ cloudCmd .PersistentFlags ().StringVarP (& cloud .flags .DeployDomain , "domain" , "" , "" , "Environment domain name to deploy to" )
101+
30102 return
31103}
0 commit comments