@@ -22,16 +22,17 @@ import (
2222 "bytes"
2323 "encoding/json"
2424 "html/template"
25+ "os"
2526 "path/filepath"
2627 "strings"
2728
2829 "github.com/compose-spec/compose-go/types"
2930 "github.com/docker/compose-cli/kube/resources"
31+ "github.com/pkg/errors"
3032 "gopkg.in/yaml.v3"
3133
3234 chart "helm.sh/helm/v3/pkg/chart"
3335 loader "helm.sh/helm/v3/pkg/chart/loader"
34- util "helm.sh/helm/v3/pkg/chartutil"
3536 "k8s.io/apimachinery/pkg/runtime"
3637)
3738
@@ -130,22 +131,38 @@ func GetChartInMemory(project *types.Project) (*chart.Chart, error) {
130131 return ConvertToChart (project .Name , objects )
131132}
132133
133- // SaveChart converts compose project to helm and saves the chart
134- func SaveChart (project * types. Project , dest string ) error {
135- chart , err := GetChartInMemory ( project )
134+ // SaveChart saves the chart to directory
135+ func SaveChart (c * chart. Chart , dest string ) ( string , error ) {
136+ dir , err := filepath . Abs ( dest )
136137 if err != nil {
137- return err
138+ return "" , err
138139 }
139- return util .SaveDir (chart , dest )
140- }
140+ for _ , file := range c .Raw {
141+ filename := filepath .Join (dir , file .Name )
142+ filedir := filepath .Dir (filename )
141143
142- // GenerateChart generates helm chart from Compose project
143- func GenerateChart (project * types.Project , dirname string ) error {
144- if strings .Contains (dirname , "." ) {
145- splits := strings .SplitN (dirname , "." , 2 )
146- dirname = splits [0 ]
147- }
144+ stat , err := os .Stat (filedir )
145+
146+ if err != nil {
147+ if os .IsNotExist (err ) {
148+ if err2 := os .MkdirAll (filedir , 0755 ); err2 != nil {
149+ return "" , err2
150+ }
151+ } else {
152+ return "" , err
153+ }
154+ } else if ! stat .IsDir () {
155+ return "" , errors .Errorf ("%s: not a directory" , dest )
156+ }
148157
149- dirname = filepath .Dir (dirname )
150- return SaveChart (project , dirname )
158+ f , err := os .Create (filename )
159+ if err != nil {
160+ return "" , err
161+ }
162+ _ , err = f .Write (file .Data )
163+ if err != nil {
164+ return "" , err
165+ }
166+ }
167+ return dir , nil
151168}
0 commit comments