From 85f9fe6eac27265cd6d17fffb5926c582f051413 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Wed, 14 Aug 2024 11:35:12 +0200 Subject: [PATCH] WIP --- cmd/explore.go | 134 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 cmd/explore.go diff --git a/cmd/explore.go b/cmd/explore.go new file mode 100644 index 00000000..8acca83f --- /dev/null +++ b/cmd/explore.go @@ -0,0 +1,134 @@ +package cmd + +import ( + "fmt" + "os" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/overmindtech/aws-source/proc" + "github.com/overmindtech/cli/tfutils" + stdlibsource "github.com/overmindtech/stdlib-source/sources" + "github.com/pterm/pterm" + "github.com/spf13/cobra" +) + +// exploreCmd represents the explore command +var exploreCmd = &cobra.Command{ + Use: "explore", + Short: "Run local sources for using in the Explore page", + Long: `Run sources locally using terraform's configured authorization to provide data when using https://app.overmind.tech/explore.`, + PreRun: PreRunSetup, + RunE: Explore, + + // SilenceErrors: false, +} + +func Explore(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + multi := pterm.DefaultMultiPrinter + + loginSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Connecting to Overmind") + + _, _ = multi.Start() + defer func() { + // TODO: remove debugging delay + pterm.DefaultParagraph.WithWriter(multi.NewWriter()).Println("Sleeping for 5 seconds") + time.Sleep(time.Second * 5) + _, _ = multi.Stop() + }() + + ctx, oi, token, err := login(ctx, cmd, []string{"explore:read", "changes:read"}) + if err != nil { + return err + } + + loginSpinner.Success("Connected to Overmind") + + stdlibSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting stdlib source engine") + awsSpinner, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Starting AWS source engine") + + natsOptions := natsOptions(ctx, oi, token) + + stdlibEngine, err := stdlibsource.InitializeEngine(natsOptions, 2_000, true) + if err != nil { + stdlibSpinner.Fail("Failed to initialize stdlib source engine") + return fmt.Errorf("failed to initialize stdlib source engine: %w", err) + } + + // todo: pass in context with timeout to abort timely and allow Ctrl-C to work + err = stdlibEngine.Start() + if err != nil { + stdlibSpinner.Fail("Failed to start stdlib source engine") + return fmt.Errorf("failed to start stdlib source engine: %w", err) + } + stdlibSpinner.Success("Stdlib source engine started") + + tfEval, err := tfutils.LoadEvalContext(args, os.Environ()) + if err != nil { + awsSpinner.Fail("Failed to load variables from the environment") + return fmt.Errorf("failed to load variables from the environment: %w", err) + } + + providers, err := tfutils.ParseAWSProviders(".", tfEval) + if err != nil { + awsSpinner.Fail("Failed to parse providers") + return fmt.Errorf("failed to parse providers: %w", err) + } + configs := []aws.Config{} + + for _, p := range providers { + if p.Error != nil { + // skip providers that had errors. This allows us to use + // providers we _could_ detect, while still failing if there is + // a true syntax error and no providers are available at all. + continue + } + c, err := tfutils.ConfigFromProvider(ctx, *p.Provider) + if err != nil { + awsSpinner.Fail("Error when converting provider to config") + return fmt.Errorf("error when converting provider to config: %w", err) + } + configs = append(configs, c) + } + + awsEngine, err := proc.InitializeAwsSourceEngine(ctx, natsOptions, 2_000, configs...) + if err != nil { + awsSpinner.Fail("Failed to initialize AWS source engine") + return fmt.Errorf("failed to initialize AWS source engine: %w", err) + } + + // todo: pass in context with timeout to abort timely and allow Ctrl-C to work + err = awsEngine.Start() + if err != nil { + awsSpinner.Fail("Failed to start AWS source engine") + return fmt.Errorf("failed to start AWS source engine: %w", err) + } + + awsSpinner.Success("AWS source engine started") + + // for i := 0; i < 20; i++ { + // fmt.Println("terraform plan output") + // } + + // multi.Start() + // _, err = multi.Stop() + return nil +} + +func init() { + rootCmd.AddCommand(exploreCmd) + + addAPIFlags(exploreCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // exploreCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // exploreCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +}