From 66a62ab2542540226b7268ab76e31efaf5e830a0 Mon Sep 17 00:00:00 2001 From: Romaric P Date: Thu, 12 Sep 2024 11:00:09 -0400 Subject: [PATCH] change migrate command by prepare command and add progress bar --- cmd/{migrate.go => prepare.go} | 69 ++++++++++++++++++++++++---------- go.mod | 5 +++ go.sum | 16 ++++++++ pkg/migration/migration.go | 32 +++++++++++++--- 4 files changed, 98 insertions(+), 24 deletions(-) rename cmd/{migrate.go => prepare.go} (63%) diff --git a/cmd/migrate.go b/cmd/prepare.go similarity index 63% rename from cmd/migrate.go rename to cmd/prepare.go index efb7fb2..30c7c4b 100644 --- a/cmd/migrate.go +++ b/cmd/prepare.go @@ -2,11 +2,11 @@ package cmd import ( "fmt" + "github.com/schollz/progressbar/v3" + "github.com/spf13/cobra" "os" "path/filepath" "qovery-ai-migration/pkg/migration" - - "github.com/spf13/cobra" ) var ( @@ -16,24 +16,24 @@ var ( stdoutFlag bool ) -// migrateCmd represents the migrate command -var migrateCmd = &cobra.Command{ - Use: "migrate", - Short: "Migrate applications to Qovery", - Long: `This command migrates your applications to Qovery, generating necessary Terraform configurations and Dockerfiles.`, - Run: runMigrate, +// prepareCmd represents the prepare command +var prepareCmd = &cobra.Command{ + Use: "prepare", + Short: "Prepare migration assets for Qovery", + Long: `This command prepares migration assets for Qovery, generating necessary Terraform configurations and Dockerfiles.`, + Run: runPrepare, } func init() { - rootCmd.AddCommand(migrateCmd) - migrateCmd.Flags().StringVarP(&source, "from", "f", "", "Source platform (e.g., 'heroku') (required)") - migrateCmd.Flags().StringVarP(&destination, "to", "t", "", "Destination cloud provider (aws, gcp, or scaleway) (required)") - migrateCmd.Flags().StringVarP(&outputDir, "output", "o", "", "Output directory for generated files") - _ = migrateCmd.MarkFlagRequired("from") - _ = migrateCmd.MarkFlagRequired("to") + rootCmd.AddCommand(prepareCmd) + prepareCmd.Flags().StringVarP(&source, "from", "f", "", "Source platform (e.g., 'heroku') (required)") + prepareCmd.Flags().StringVarP(&destination, "to", "t", "", "Destination cloud provider (aws, gcp, or scaleway) (required)") + prepareCmd.Flags().StringVarP(&outputDir, "output", "o", "", "Output directory for generated files") + _ = prepareCmd.MarkFlagRequired("from") + _ = prepareCmd.MarkFlagRequired("to") } -func runMigrate(cmd *cobra.Command, args []string) { +func runPrepare(cmd *cobra.Command, args []string) { // Validate source if source != "heroku" { fmt.Println("Error: Currently only 'heroku' is supported as a source") @@ -61,9 +61,40 @@ func runMigrate(cmd *cobra.Command, args []string) { os.Exit(1) } - assets, err := migration.GenerateMigrationAssets(source, herokuAPIKey, claudeAPIKey, qoveryAPIKey, destination) + // Create a progress channel + progressChan := make(chan migration.ProgressUpdate) + + // Create a new progress bar + bar := progressbar.NewOptions(100, + progressbar.OptionSetWidth(15), + progressbar.OptionSetDescription("Preparing..."), + progressbar.OptionSetTheme(progressbar.Theme{ + Saucer: "๐Ÿš€", + SaucerHead: "๐Ÿš€", + SaucerPadding: "ยท", + BarStart: "[", + BarEnd: "]", + }), + ) + + // Start a goroutine to update the progress bar + go func() { + for update := range progressChan { + _ = bar.Set(int(update.Progress * 100)) + bar.Describe(update.Stage) + } + }() + + assets, err := migration.GenerateMigrationAssets(source, herokuAPIKey, claudeAPIKey, qoveryAPIKey, destination, progressChan) + + // Close the progress channel + close(progressChan) + + // Ensure the progress bar reaches 100% + _ = bar.Finish() + if err != nil { - fmt.Printf("Error generating migration assets: %v\n", err) + fmt.Printf("\nError generating migration assets: %v\n", err) os.Exit(1) } @@ -95,12 +126,12 @@ func runMigrate(cmd *cobra.Command, args []string) { } } - fmt.Printf("Migration assets generated successfully in %s\n", outputDir) + fmt.Printf("\nMigration assets prepared successfully in %s\n", outputDir) return } // Output the generated assets to stdout - fmt.Println("Terraform Main Configuration:") + fmt.Println("\nTerraform Main Configuration:") fmt.Println(assets.TerraformMain) fmt.Println("\nTerraform Variables:") fmt.Println(assets.TerraformVariables) diff --git a/go.mod b/go.mod index 0083654..d10db9e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.5 require ( github.com/google/go-github/v39 v39.2.0 github.com/joho/godotenv v1.5.1 + github.com/schollz/progressbar/v3 v3.14.6 github.com/spf13/cobra v1.8.1 github.com/stretchr/testify v1.9.0 ) @@ -13,9 +14,13 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/objx v0.5.2 // indirect golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/term v0.22.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5ab6daf..5e5c0e0 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -14,15 +15,25 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= +github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/schollz/progressbar/v3 v3.14.6 h1:GyjwcWBAf+GFDMLziwerKvpuS7ZF+mNTAXIB2aspiZs= +github.com/schollz/progressbar/v3 v3.14.6/go.mod h1:Nrzpuw3Nl0srLY0VlTvC4V6RL50pcEymjy6qyJAaLa0= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -34,7 +45,12 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= +golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/pkg/migration/migration.go b/pkg/migration/migration.go index 520fd2b..9fe1741 100644 --- a/pkg/migration/migration.go +++ b/pkg/migration/migration.go @@ -25,14 +25,22 @@ type Dockerfile struct { DockerfileContent string } -// GenerateMigrationAssets generates all necessary assets for migration -func GenerateMigrationAssets(source, herokuAPIKey, claudeAPIKey, qoveryAPIKey, destination string) (*Assets, error) { +// ProgressUpdate represents a progress update +type ProgressUpdate struct { + Stage string + Progress float64 +} + +// GenerateMigrationAssets generates all necessary assets for migration and reports progress +func GenerateMigrationAssets(source, herokuAPIKey, claudeAPIKey, qoveryAPIKey, destination string, progressChan chan<- ProgressUpdate) (*Assets, error) { claudeClient := claude.NewClaudeClient(claudeAPIKey) qoveryProvider := qovery.NewQoveryProvider(qoveryAPIKey) var configs []map[string]interface{} var err error + // Fetch configs + progressChan <- ProgressUpdate{Stage: "Fetching configs", Progress: 0.1} switch source { case "heroku": herokuProvider := heroku.NewHerokuProvider(herokuAPIKey) @@ -45,10 +53,13 @@ func GenerateMigrationAssets(source, herokuAPIKey, claudeAPIKey, qoveryAPIKey, d return nil, fmt.Errorf("unsupported source: %s", source) } + progressChan <- ProgressUpdate{Stage: "Processing configs", Progress: 0.3} + var qoveryConfigs = make(map[string]interface{}) var dockerfiles []Dockerfile - for _, app := range configs { + totalApps := len(configs) + for i, app := range configs { appName := app["name"].(string) qoveryConfig := qoveryProvider.TranslateConfig(app, destination) qoveryConfigs[appName] = qoveryConfig @@ -62,18 +73,29 @@ func GenerateMigrationAssets(source, herokuAPIKey, claudeAPIKey, qoveryAPIKey, d AppName: appName, DockerfileContent: dockerfile, }) + + progress := 0.3 + (float64(i+1) / float64(totalApps) * 0.4) + progressChan <- ProgressUpdate{Stage: fmt.Sprintf("Processing app %d/%d", i+1, totalApps), Progress: progress} } + progressChan <- ProgressUpdate{Stage: "Generating Terraform configs", Progress: 0.7} + terraformMain, terraformVariables, err := generateTerraform(qoveryConfigs, destination, claudeClient) if err != nil { return nil, fmt.Errorf("error generating Terraform configs: %w", err) } - return &Assets{ + progressChan <- ProgressUpdate{Stage: "Finalizing", Progress: 0.9} + + assets := &Assets{ TerraformMain: terraformMain, TerraformVariables: terraformVariables, Dockerfiles: dockerfiles, - }, nil + } + + progressChan <- ProgressUpdate{Stage: "Completed", Progress: 1.0} + + return assets, nil } // generateDockerfile generates a Dockerfile for a given app configuration