Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit f21326a

Browse files
author
aiordache
committed
Add output flag for writing chart files
Signed-off-by: aiordache <[email protected]>
1 parent a9247e5 commit f21326a

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

api/compose/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type DownOptions struct {
8181
type ConvertOptions struct {
8282
// Format define the output format used to dump converted application model (json|yaml)
8383
Format string
84+
// Output defines the path to save the application model
85+
Output string
8486
}
8587

8688
// KillOptions group options of the Kill API

cli/cmd/compose/convert.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ import (
3030
type convertOptions struct {
3131
*projectOptions
3232
Format string
33+
Output string
3334
}
3435

36+
var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
37+
3538
func convertCommand(p *projectOptions) *cobra.Command {
3639
opts := convertOptions{
3740
projectOptions: p,
@@ -47,6 +50,10 @@ func convertCommand(p *projectOptions) *cobra.Command {
4750
flags := convertCmd.Flags()
4851
flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
4952

53+
// add flags for hidden backends
54+
for _, f := range addFlagsFuncs {
55+
f(convertCmd, &opts)
56+
}
5057
return convertCmd
5158
}
5259

@@ -64,11 +71,14 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err
6471

6572
json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
6673
Format: opts.Format,
74+
Output: opts.Output,
6775
})
6876
if err != nil {
6977
return err
7078
}
71-
79+
if opts.Output != "" {
80+
fmt.Print("model saved to ")
81+
}
7282
fmt.Println(string(json))
7383
return nil
7484
}

cli/cmd/compose/convert_kube.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// +build kube
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package compose
20+
21+
import (
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func init() {
26+
addFlagsFuncs = append(addFlagsFuncs, func(cmd *cobra.Command, opts *convertOptions) {
27+
flags := cmd.Flags()
28+
flags.StringVar(&opts.Output, "output", "", "Save to directory")
29+
})
30+
31+
}

kube/compose.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,17 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options com
164164

165165
// Convert translate compose model into backend's native format
166166
func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
167+
167168
chart, err := helm.GetChartInMemory(project)
168169
if err != nil {
169170
return nil, err
170171
}
172+
173+
if options.Output != "" {
174+
fullpath, err := helm.SaveChart(chart, options.Output)
175+
return []byte(fullpath), err
176+
}
177+
171178
buff := []byte{}
172179
for _, f := range chart.Raw {
173180
header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n"

kube/helm/chart.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)