Skip to content

Commit

Permalink
export write assets function to shared lib
Browse files Browse the repository at this point in the history
  • Loading branch information
evoxmusic committed Sep 13, 2024
1 parent 7f5f181 commit 79a73a9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 36 deletions.
38 changes: 3 additions & 35 deletions cli/cmd/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ package cmd

import (
"fmt"
"github.com/qovery/qovery-migration-ai-agent"
"github.com/Qovery/qovery-migration-ai-agent/pkg/migration"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

var (
source string
destination string
outputDir string
stdoutFlag bool
)

// prepareCmd represents the prepare command
Expand Down Expand Up @@ -62,7 +60,7 @@ func runPrepare(cmd *cobra.Command, args []string) {
}

// Create a progress channel
progressChan := make(chan qovery_migration_ai_agent.migration.ProgressUpdate)
progressChan := make(chan migration.ProgressUpdate)

// Create a new progress bar
bar := progressbar.NewOptions(100,
Expand Down Expand Up @@ -99,33 +97,7 @@ func runPrepare(cmd *cobra.Command, args []string) {
}

if outputDir != "" {
// Create the output directory if it doesn't exist
if err := os.MkdirAll(outputDir, 0755); err != nil {
fmt.Printf("Error creating output directory: %v\n", err)
os.Exit(1)
}

// Write Terraform main configuration
if err := writeToFile(filepath.Join(outputDir, "main.tf"), assets.TerraformMain); err != nil {
fmt.Printf("Error writing main.tf: %v\n", err)
os.Exit(1)
}

// Write Terraform variables
if err := writeToFile(filepath.Join(outputDir, "variables.tf"), assets.TerraformVariables); err != nil {
fmt.Printf("Error writing variables.tf: %v\n", err)
os.Exit(1)
}

// Write Dockerfiles
for _, dockerfile := range assets.Dockerfiles {
filename := fmt.Sprintf("Dockerfile-%s", dockerfile.AppName)
if err := writeToFile(filepath.Join(outputDir, filename), dockerfile.DockerfileContent); err != nil {
fmt.Printf("Error writing %s: %v\n", filename, err)
os.Exit(1)
}
}

migration.WriteAssets(outputDir, assets)
fmt.Printf("\nMigration assets prepared successfully in %s\n", outputDir)
return
}
Expand All @@ -142,7 +114,3 @@ func runPrepare(cmd *cobra.Command, args []string) {
fmt.Println("---")
}
}

func writeToFile(filename, content string) error {
return os.WriteFile(filename, []byte(content), 0644)
}
5 changes: 4 additions & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ module qovery-ai-migration-cli
go 1.22.5

require (
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240913105729-7f5f181916cc
github.com/joho/godotenv v1.5.1
github.com/schollz/progressbar/v3 v3.14.6
github.com/spf13/cobra v1.8.1
)

require (
github.com/google/go-github/v39 v39.2.0 // 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/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // 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
)
29 changes: 29 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240913105729-7f5f181916cc h1:yBpFxN5KC91++De7BcheKFXmRSFGi0UlW5S87doHWIM=
github.com/Qovery/qovery-migration-ai-agent v0.0.0-20240913105729-7f5f181916cc/go.mod h1:BK4dodrSYDAJySssVnQDgzCjaR1yvonA3hNYsDigYLQ=
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=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github/v39 v39.2.0 h1:rNNM311XtPOz5rDdsJXAp2o8F67X9FnROXTvto3aSnQ=
github.com/google/go-github/v39 v39.2.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
Expand All @@ -22,14 +33,32 @@ github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3k
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=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
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=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 32 additions & 0 deletions pkg/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,35 @@ Please fix the Terraform configuration to resolve these errors. Provide only the

return "", fmt.Errorf("exceeded maximum iterations (%d) without achieving a valid Terraform configuration", maxIterations)
}

// WriteAssets writes the generated assets to the output directory
func WriteAssets(outputDir string, assets *Assets) error {
// Create the output directory if it doesn't exist
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("error creating output directory: %w", err)
}

// Write Terraform main configuration
if err := writeToFile(filepath.Join(outputDir, "main.tf"), assets.TerraformMain); err != nil {
return fmt.Errorf("error writing main.tf: %w", err)
}

// Write Terraform variables
if err := writeToFile(filepath.Join(outputDir, "variables.tf"), assets.TerraformVariables); err != nil {
return fmt.Errorf("error writing variables.tf: %w", err)
}

// Write Dockerfiles
for _, dockerfile := range assets.Dockerfiles {
filename := fmt.Sprintf("Dockerfile-%s", dockerfile.AppName)
if err := writeToFile(filepath.Join(outputDir, filename), dockerfile.DockerfileContent); err != nil {
return fmt.Errorf("error writing %s: %w", filename, err)
}
}

return nil
}

func writeToFile(filename, content string) error {
return os.WriteFile(filename, []byte(content), 0644)
}

0 comments on commit 79a73a9

Please sign in to comment.