Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidS-ovm committed Aug 14, 2024
1 parent b806ceb commit 85f9fe6
Showing 1 changed file with 134 additions and 0 deletions.
134 changes: 134 additions & 0 deletions cmd/explore.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 85f9fe6

Please sign in to comment.