diff --git a/cmd/create.go b/cmd/create.go index fdcb27b..eb35e7f 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -100,6 +100,10 @@ func GetCorgiServicesMap(corgi *utils.CorgiCompose) map[string]interface{} { corgiServicesMap[utils.UseDockerInConfig] = corgi.UseDocker } + if corgi.UseAwsVpn { + corgiServicesMap[utils.UseAwsVpnInConfig] = corgi.UseAwsVpn + } + return corgiServicesMap } @@ -377,6 +381,7 @@ func UpdateCorgiComposeFileWithMap(corgiMap map[string]interface{}) { utils.BeforeStartInConfig, utils.AfterStartInConfig, utils.UseDockerInConfig, + utils.UseAwsVpnInConfig, } { if section, exists := corgiMap[sectionKey]; exists { if sectionArr, ok := section.([]string); ok { diff --git a/cmd/root.go b/cmd/root.go index 1d872ab..0eb8c1c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" ) -var APP_VERSION = "1.3.2" +var APP_VERSION = "1.3.3" // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ diff --git a/cmd/run.go b/cmd/run.go index 12792f9..9a02297 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -118,6 +118,13 @@ func runRun(cmd *cobra.Command, _ []string) { utils.CleanFromScratch(cmd, *corgi) + if corgi.UseAwsVpn { + err = utils.AwsVpnInit() + if err != nil { + fmt.Println("AWS VPN init failed", err) + } + } + if corgi.UseDocker { err = utils.DockerInit(cmd) if err != nil { diff --git a/utils/config.go b/utils/config.go index f3dce52..0f56745 100644 --- a/utils/config.go +++ b/utils/config.go @@ -22,6 +22,7 @@ var StartInConfig = "start" var BeforeStartInConfig = "beforeStart" var AfterStartInConfig = "afterStart" var UseDockerInConfig = "useDocker" +var UseAwsVpnInConfig = "useAwsVpn" var RootDbServicesFolder = "corgi_services/db_services" var ServicesItemsFromFlag []string @@ -109,6 +110,7 @@ type CorgiCompose struct { AfterStart []string `yaml:"afterStart,omitempty"` UseDocker bool `yaml:"useDocker,omitempty"` + UseAwsVpn bool `yaml:"useAwsVpn,omitempty"` } type CorgiComposeYaml struct { @@ -122,6 +124,7 @@ type CorgiComposeYaml struct { AfterStart []string `yaml:"afterStart,omitempty"` UseDocker bool `yaml:"useDocker,omitempty"` + UseAwsVpn bool `yaml:"useAwsVpn,omitempty"` } var CorgiComposePath string @@ -156,6 +159,7 @@ func GetCorgiServices(cobra *cobra.Command) (*CorgiCompose, error) { corgi.Start = corgiYaml.Start corgi.AfterStart = corgiYaml.AfterStart corgi.UseDocker = corgiYaml.UseDocker + corgi.UseAwsVpn = corgiYaml.UseAwsVpn dbServicesData := corgiYaml.DatabaseServices diff --git a/utils/startCommands.go b/utils/startCommands.go new file mode 100644 index 0000000..4a02eec --- /dev/null +++ b/utils/startCommands.go @@ -0,0 +1,59 @@ +package utils + +import ( + "bytes" + "fmt" + "os/exec" + "runtime" + "strings" + "time" + + "github.com/briandowns/spinner" +) + +func AwsVpnInit() error { + if runtime.GOOS == "linux" { + return fmt.Errorf("this function is not intended to run on Linux") + } + + s := spinner.New(spinner.CharSets[39], 100*time.Millisecond) + s.Suffix = " doing woof magic to start aws vpn" + + for { + s.Start() + cmd := exec.Command("ps", "ax") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + s.Stop() + return fmt.Errorf("failed to execute ps command: %v", err) + } + + vpnAlive := false + for _, line := range strings.Split(out.String(), "\n") { + if strings.Contains(line, "AWS") && strings.Contains(line, "isAlive") { + vpnAlive = true + break + } + } + + if !vpnAlive { + startCmd := exec.Command("open", "-a", "AWS VPN Client") + if err := startCmd.Run(); err != nil { + s.Stop() + return fmt.Errorf("failed to start AWS VPN Client: %v", err) + } + + s.Suffix = " Waiting for AWS VPN to start..." + time.Sleep(5 * time.Second) + } else { + s.Stop() + fmt.Println("\nAWS vpn is opened, so waiting an additional time till you login in your account") + time.Sleep(10 * time.Second) + break + } + } + + return nil +}