11package kompose
22
33import (
4+ "fmt"
45 "os"
56 "path/filepath"
67 "strconv"
@@ -42,52 +43,93 @@ func Convert(dockerCompose DockerComposeFile) (string, error) {
4243 tempFilePath := filepath .Join (mesheryDir , "temp.data" )
4344 resultFilePath := filepath .Join (mesheryDir , "result.yaml" )
4445
46+ // Create the temp file
4547 if err := utils .CreateFile (dockerCompose , "temp.data" , mesheryDir ); err != nil {
4648 return "" , ErrCvrtKompose (err )
4749 }
4850
49- defer func () {
50- os .Remove (tempFilePath )
51- os .Remove (resultFilePath )
52- }()
51+ // Format Docker Compose file for Kompose
52+ err = formatComposeFile (& dockerCompose )
53+ if err != nil {
54+ return "" , ErrCvrtKompose (err )
55+ }
5356
54- formatComposeFile ( & dockerCompose )
57+ // Check version compatibility
5558 err = versionCheck (dockerCompose )
5659 if err != nil {
5760 return "" , ErrCvrtKompose (err )
5861 }
5962
63+ // Initialize Kompose client
6064 komposeClient , err := client .NewClient ()
6165 if err != nil {
6266 return "" , ErrCvrtKompose (err )
6367 }
6468
69+ // Set up Convert options
6570 ConvertOpt := client.ConvertOptions {
6671 InputFiles : []string {tempFilePath },
6772 OutFile : resultFilePath ,
6873 GenerateNetworkPolicies : true ,
6974 }
7075
76+ // Convert using Kompose client
7177 _ , err = komposeClient .Convert (ConvertOpt )
7278 if err != nil {
7379 return "" , ErrCvrtKompose (err )
7480 }
7581
82+ // Read the result file
7683 result , err := os .ReadFile (resultFilePath )
7784 if err != nil {
7885 return "" , ErrCvrtKompose (err )
7986 }
8087
88+ // Clean up temporary files
89+ cleanupErr := cleanup (tempFilePath , resultFilePath )
90+ if cleanupErr != nil {
91+ return "" , cleanupErr
92+ }
93+
8194 return string (result ), nil
8295}
8396
84- type composeFile struct {
85- Version string `yaml:"version,omitempty"`
97+ // cleanup removes temporary files
98+ func cleanup (tempFilePath , resultFilePath string ) error {
99+ // Try to remove tempFilePath
100+ if err := os .Remove (tempFilePath ); err != nil {
101+ return fmt .Errorf ("failed to remove temp file %s: %w" , tempFilePath , err )
102+ }
103+
104+ // Try to remove resultFilePath
105+ if err := os .Remove (resultFilePath ); err != nil {
106+ return fmt .Errorf ("failed to remove result file %s: %w" , resultFilePath , err )
107+ }
108+
109+ return nil // No errors
110+ }
111+
112+ // formatComposeFile takes in a pointer to the compose file byte array and formats it
113+ // so that it is compatible with Kompose. It expects a validated Docker Compose file.
114+ func formatComposeFile (yamlManifest * DockerComposeFile ) error {
115+ data := composeFile {}
116+ err := yaml .Unmarshal (* yamlManifest , & data )
117+ if err != nil {
118+ return fmt .Errorf ("failed to unmarshal compose file: %w" , err )
119+ }
120+
121+ // Marshal it again to ensure it is in the correct format for Kompose
122+ out , err := yaml .Marshal (data )
123+ if err != nil {
124+ return fmt .Errorf ("failed to marshal compose file: %w" , err )
125+ }
126+
127+ * yamlManifest = out
128+ return nil
86129}
87130
88- // checks if the version is compatible with `kompose`
89- // expects a valid docker compose yaml
90- // error = nil means it is compatible
131+ // versionCheck checks if the version in the Docker Compose file is compatible with Kompose.
132+ // It expects a valid Docker Compose YAML and returns an error if the version is not supported.
91133func versionCheck (dc DockerComposeFile ) error {
92134 cf := composeFile {}
93135 err := yaml .Unmarshal (dc , & cf )
@@ -108,18 +150,7 @@ func versionCheck(dc DockerComposeFile) error {
108150 return nil
109151}
110152
111- // formatComposeFile takes in a pointer to the compose file byte array and formats it so that it is compatible with `Kompose`
112- // it expects a validated docker compose file and does not validate
113- func formatComposeFile (yamlManifest * DockerComposeFile ) {
114- data := composeFile {}
115- err := yaml .Unmarshal (* yamlManifest , & data )
116- if err != nil {
117- return
118- }
119- // so that "3.3" and 3.3 are treated differently by `Kompose`
120- out , err := yaml .Marshal (data )
121- if err != nil {
122- return
123- }
124- * yamlManifest = out
153+ // composeFile represents the structure of the Docker Compose file version.
154+ type composeFile struct {
155+ Version string `yaml:"version,omitempty"`
125156}
0 commit comments