better error display and cleanup

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2020-04-10 17:08:18 +02:00
parent 881818223c
commit 3bd15bf0d4
3 changed files with 38 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package compose
import (
"errors"
"path/filepath"
"strings"
@ -19,13 +20,19 @@ type ComposeProject struct {
}
func Load(name string, configpaths []string) (*ComposeProject, error) {
if name == "" {
name = "docker-compose"
}
model, workingDir, err := internal.GetConfig(name, configpaths)
if err != nil {
return nil, err
}
if name == "" {
if model != nil {
name = filepath.Base(filepath.Dir(model.Filename))
} else if workingDir != "" {
name = filepath.Base(filepath.Dir(workingDir))
}
}
return &ComposeProject{
config: model,
helm: helm.NewHelmActions(nil),
@ -35,6 +42,10 @@ func Load(name string, configpaths []string) (*ComposeProject, error) {
}
func (cp *ComposeProject) GenerateChart(dirname string) error {
if cp.config == nil {
return errors.New(`Can't find a suitable configuration file in this directory or any
parent. Are you in the right directory?`)
}
if dirname == "" {
dirname = cp.config.Filename
if strings.Contains(dirname, ".") {
@ -51,7 +62,13 @@ func (cp *ComposeProject) Install(name, path string) error {
if path != "" {
return cp.helm.InstallChartFromDir(name, path)
}
if cp.config == nil {
return errors.New(`Can't find a suitable configuration file in this directory or any
parent. Are you in the right directory?`)
}
if name == "" {
name = cp.Name
}
chart, err := internal.GetChartInMemory(cp.config, name)
if err != nil {
return err
@ -60,6 +77,16 @@ func (cp *ComposeProject) Install(name, path string) error {
}
func (cp *ComposeProject) Uninstall(name string) error {
if name == "" {
if cp.config == nil {
return errors.New(`Can't find a suitable configuration file in this directory or any
parent. Are you in the right directory?
Alternative: uninstall [INSTALLATION NAME]
`)
}
name = cp.Name
}
return cp.helm.Uninstall(name)
}

View File

@ -29,9 +29,6 @@ func Environment() map[string]string {
}
func GetConfig(name string, configPaths []string) (*types.Config, string, error) {
if name == "" {
name = "docker-compose"
}
workingDir, configs, err := utils.GetConfigs(
name,
configPaths,
@ -39,6 +36,9 @@ func GetConfig(name string, configPaths []string) (*types.Config, string, error)
if err != nil {
return nil, "", err
}
if configs == nil {
return nil, "", nil
}
config, err := loader.Load(types.ConfigDetails{
WorkingDir: workingDir,
ConfigFiles: configs,

View File

@ -1,7 +1,6 @@
package utils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -20,6 +19,9 @@ func GetConfigs(name string, configPaths []string) (string, []types.ConfigFile,
if err != nil {
return "", nil, err
}
if configPath == nil {
return "", nil, nil
}
workingDir := filepath.Dir(configPath[0])
if name == "" {
@ -88,7 +90,7 @@ func getConfigPaths(configPaths []string) ([]string, error) {
}
parent := filepath.Dir(pwd)
if parent == pwd {
return nil, fmt.Errorf("Can't find a suitable configuration file in this directory or any parent. Are you in the right directory?")
return nil, nil
}
pwd = parent
}