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

Commit 3bd15bf

Browse files
author
aiordache
committed
better error display and cleanup
Signed-off-by: aiordache <[email protected]>
1 parent 8818182 commit 3bd15bf

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

compose/compose.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compose
22

33
import (
4+
"errors"
45
"path/filepath"
56
"strings"
67

@@ -19,13 +20,19 @@ type ComposeProject struct {
1920
}
2021

2122
func Load(name string, configpaths []string) (*ComposeProject, error) {
22-
if name == "" {
23-
name = "docker-compose"
24-
}
2523
model, workingDir, err := internal.GetConfig(name, configpaths)
2624
if err != nil {
2725
return nil, err
2826
}
27+
28+
if name == "" {
29+
if model != nil {
30+
name = filepath.Base(filepath.Dir(model.Filename))
31+
} else if workingDir != "" {
32+
name = filepath.Base(filepath.Dir(workingDir))
33+
}
34+
}
35+
2936
return &ComposeProject{
3037
config: model,
3138
helm: helm.NewHelmActions(nil),
@@ -35,6 +42,10 @@ func Load(name string, configpaths []string) (*ComposeProject, error) {
3542
}
3643

3744
func (cp *ComposeProject) GenerateChart(dirname string) error {
45+
if cp.config == nil {
46+
return errors.New(`Can't find a suitable configuration file in this directory or any
47+
parent. Are you in the right directory?`)
48+
}
3849
if dirname == "" {
3950
dirname = cp.config.Filename
4051
if strings.Contains(dirname, ".") {
@@ -51,7 +62,13 @@ func (cp *ComposeProject) Install(name, path string) error {
5162
if path != "" {
5263
return cp.helm.InstallChartFromDir(name, path)
5364
}
54-
65+
if cp.config == nil {
66+
return errors.New(`Can't find a suitable configuration file in this directory or any
67+
parent. Are you in the right directory?`)
68+
}
69+
if name == "" {
70+
name = cp.Name
71+
}
5572
chart, err := internal.GetChartInMemory(cp.config, name)
5673
if err != nil {
5774
return err
@@ -60,6 +77,16 @@ func (cp *ComposeProject) Install(name, path string) error {
6077
}
6178

6279
func (cp *ComposeProject) Uninstall(name string) error {
80+
if name == "" {
81+
if cp.config == nil {
82+
return errors.New(`Can't find a suitable configuration file in this directory or any
83+
parent. Are you in the right directory?
84+
85+
Alternative: uninstall [INSTALLATION NAME]
86+
`)
87+
}
88+
name = cp.Name
89+
}
6390
return cp.helm.Uninstall(name)
6491
}
6592

compose/internal/env.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ func Environment() map[string]string {
2929
}
3030

3131
func GetConfig(name string, configPaths []string) (*types.Config, string, error) {
32-
if name == "" {
33-
name = "docker-compose"
34-
}
3532
workingDir, configs, err := utils.GetConfigs(
3633
name,
3734
configPaths,
3835
)
3936
if err != nil {
4037
return nil, "", err
4138
}
39+
if configs == nil {
40+
return nil, "", nil
41+
}
4242
config, err := loader.Load(types.ConfigDetails{
4343
WorkingDir: workingDir,
4444
ConfigFiles: configs,

compose/internal/utils/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package utils
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
76
"path/filepath"
@@ -20,6 +19,9 @@ func GetConfigs(name string, configPaths []string) (string, []types.ConfigFile,
2019
if err != nil {
2120
return "", nil, err
2221
}
22+
if configPath == nil {
23+
return "", nil, nil
24+
}
2325
workingDir := filepath.Dir(configPath[0])
2426

2527
if name == "" {
@@ -88,7 +90,7 @@ func getConfigPaths(configPaths []string) ([]string, error) {
8890
}
8991
parent := filepath.Dir(pwd)
9092
if parent == pwd {
91-
return nil, fmt.Errorf("Can't find a suitable configuration file in this directory or any parent. Are you in the right directory?")
93+
return nil, nil
9294
}
9395
pwd = parent
9496
}

0 commit comments

Comments
 (0)