Add output flag for writing chart files

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2021-02-05 17:16:20 +01:00
parent a9247e5412
commit f21326a319
5 changed files with 83 additions and 16 deletions

View File

@ -81,6 +81,8 @@ type DownOptions struct {
type ConvertOptions struct { type ConvertOptions struct {
// Format define the output format used to dump converted application model (json|yaml) // Format define the output format used to dump converted application model (json|yaml)
Format string Format string
// Output defines the path to save the application model
Output string
} }
// KillOptions group options of the Kill API // KillOptions group options of the Kill API

View File

@ -30,8 +30,11 @@ import (
type convertOptions struct { type convertOptions struct {
*projectOptions *projectOptions
Format string Format string
Output string
} }
var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
func convertCommand(p *projectOptions) *cobra.Command { func convertCommand(p *projectOptions) *cobra.Command {
opts := convertOptions{ opts := convertOptions{
projectOptions: p, projectOptions: p,
@ -47,6 +50,10 @@ func convertCommand(p *projectOptions) *cobra.Command {
flags := convertCmd.Flags() flags := convertCmd.Flags()
flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]") flags.StringVar(&opts.Format, "format", "yaml", "Format the output. Values: [yaml | json]")
// add flags for hidden backends
for _, f := range addFlagsFuncs {
f(convertCmd, &opts)
}
return convertCmd return convertCmd
} }
@ -64,11 +71,14 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err
json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{ json, err = c.ComposeService().Convert(ctx, project, compose.ConvertOptions{
Format: opts.Format, Format: opts.Format,
Output: opts.Output,
}) })
if err != nil { if err != nil {
return err return err
} }
if opts.Output != "" {
fmt.Print("model saved to ")
}
fmt.Println(string(json)) fmt.Println(string(json))
return nil return nil
} }

View File

@ -0,0 +1,31 @@
// +build kube
/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package compose
import (
"github.com/spf13/cobra"
)
func init() {
addFlagsFuncs = append(addFlagsFuncs, func(cmd *cobra.Command, opts *convertOptions) {
flags := cmd.Flags()
flags.StringVar(&opts.Output, "output", "", "Save to directory")
})
}

View File

@ -164,10 +164,17 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options com
// Convert translate compose model into backend's native format // Convert translate compose model into backend's native format
func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) { func (s *composeService) Convert(ctx context.Context, project *types.Project, options compose.ConvertOptions) ([]byte, error) {
chart, err := helm.GetChartInMemory(project) chart, err := helm.GetChartInMemory(project)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if options.Output != "" {
fullpath, err := helm.SaveChart(chart, options.Output)
return []byte(fullpath), err
}
buff := []byte{} buff := []byte{}
for _, f := range chart.Raw { for _, f := range chart.Raw {
header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n" header := "\n" + f.Name + "\n" + strings.Repeat("-", len(f.Name)) + "\n"

View File

@ -22,16 +22,17 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"html/template" "html/template"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/docker/compose-cli/kube/resources" "github.com/docker/compose-cli/kube/resources"
"github.com/pkg/errors"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
chart "helm.sh/helm/v3/pkg/chart" chart "helm.sh/helm/v3/pkg/chart"
loader "helm.sh/helm/v3/pkg/chart/loader" loader "helm.sh/helm/v3/pkg/chart/loader"
util "helm.sh/helm/v3/pkg/chartutil"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
) )
@ -130,22 +131,38 @@ func GetChartInMemory(project *types.Project) (*chart.Chart, error) {
return ConvertToChart(project.Name, objects) return ConvertToChart(project.Name, objects)
} }
// SaveChart converts compose project to helm and saves the chart // SaveChart saves the chart to directory
func SaveChart(project *types.Project, dest string) error { func SaveChart(c *chart.Chart, dest string) (string, error) {
chart, err := GetChartInMemory(project) dir, err := filepath.Abs(dest)
if err != nil { if err != nil {
return err return "", err
} }
return util.SaveDir(chart, dest) for _, file := range c.Raw {
} filename := filepath.Join(dir, file.Name)
filedir := filepath.Dir(filename)
// GenerateChart generates helm chart from Compose project stat, err := os.Stat(filedir)
func GenerateChart(project *types.Project, dirname string) error {
if strings.Contains(dirname, ".") { if err != nil {
splits := strings.SplitN(dirname, ".", 2) if os.IsNotExist(err) {
dirname = splits[0] if err2 := os.MkdirAll(filedir, 0755); err2 != nil {
return "", err2
}
} else {
return "", err
}
} else if !stat.IsDir() {
return "", errors.Errorf("%s: not a directory", dest)
}
f, err := os.Create(filename)
if err != nil {
return "", err
}
_, err = f.Write(file.Data)
if err != nil {
return "", err
}
} }
return dir, nil
dirname = filepath.Dir(dirname)
return SaveChart(project, dirname)
} }