Skip to content

Commit e574a2e

Browse files
committed
chore(errors): Better error management
We must remove all "Fatal" calls and use errors instead, to be returned and managed globally. This is the first step, but it is, at this time, a real problem. Tests are complicated without this.
1 parent eb760d4 commit e574a2e

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

cmd/katenary/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ func generateConvertCommand() *cobra.Command {
141141
convertCmd := &cobra.Command{
142142
Use: "convert",
143143
Short: "Converts a docker-compose file to a Helm Chart",
144-
Run: func(cmd *cobra.Command, args []string) {
144+
RunE: func(cmd *cobra.Command, args []string) error {
145145
if givenAppVersion != "" {
146146
appVersion = &givenAppVersion
147147
}
148-
generator.Convert(generator.ConvertOptions{
148+
return generator.Convert(generator.ConvertOptions{
149149
Force: force,
150150
OutputDir: outputDir,
151151
Profiles: profiles,

generator/configMap.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package generator
22

33
import (
4+
"fmt"
45
"katenary/generator/labels"
56
"katenary/generator/labels/labelStructs"
67
"katenary/utils"
@@ -141,7 +142,7 @@ func NewConfigMapFromDirectory(service types.ServiceConfig, appName, path string
141142
// cumulate the path to the WorkingDir
142143
path = filepath.Join(service.WorkingDir, path)
143144
path = filepath.Clean(path)
144-
cm.AppenddDir(path)
145+
cm.AppendDir(path)
145146
return cm
146147
}
147148

@@ -160,17 +161,17 @@ func (c *ConfigMap) AddBinaryData(key string, value []byte) {
160161

161162
// AddFile adds files from given path to the configmap. It is not recursive, to add all files in a directory,
162163
// you need to call this function for each subdirectory.
163-
func (c *ConfigMap) AppenddDir(path string) {
164+
func (c *ConfigMap) AppendDir(path string) error {
164165
// read all files in the path and add them to the configmap
165166
stat, err := os.Stat(path)
166167
if err != nil {
167-
log.Fatalf("Path %s does not exist\n", path)
168+
return fmt.Errorf("Path %s does not exist, %w\n", path, err)
168169
}
169170
// recursively read all files in the path and add them to the configmap
170171
if stat.IsDir() {
171172
files, err := os.ReadDir(path)
172173
if err != nil {
173-
log.Fatal(err)
174+
return err
174175
}
175176
for _, file := range files {
176177
if file.IsDir() {
@@ -180,7 +181,7 @@ func (c *ConfigMap) AppenddDir(path string) {
180181
path := filepath.Join(path, file.Name())
181182
content, err := os.ReadFile(path)
182183
if err != nil {
183-
log.Fatal(err)
184+
return err
184185
}
185186
// remove the path from the file
186187
filename := filepath.Base(path)
@@ -195,7 +196,7 @@ func (c *ConfigMap) AppenddDir(path string) {
195196
// add the file to the configmap
196197
content, err := os.ReadFile(path)
197198
if err != nil {
198-
log.Fatal(err)
199+
return err
199200
}
200201
filename := filepath.Base(path)
201202
if utf8.Valid(content) {
@@ -204,20 +205,21 @@ func (c *ConfigMap) AppenddDir(path string) {
204205
c.AddBinaryData(filename, content)
205206
}
206207
}
208+
return nil
207209
}
208210

209-
func (c *ConfigMap) AppendFile(path string) {
211+
func (c *ConfigMap) AppendFile(path string) error {
210212
// read all files in the path and add them to the configmap
211213
stat, err := os.Stat(path)
212214
if err != nil {
213-
log.Fatalf("Path %s does not exist\n", path)
215+
return fmt.Errorf("Path %s doesn not exists, %w", path, err)
214216
}
215217
// recursively read all files in the path and add them to the configmap
216218
if !stat.IsDir() {
217219
// add the file to the configmap
218220
content, err := os.ReadFile(path)
219221
if err != nil {
220-
log.Fatal(err)
222+
return err
221223
}
222224
if utf8.Valid(content) {
223225
c.AddData(filepath.Base(path), string(content))
@@ -226,6 +228,7 @@ func (c *ConfigMap) AppendFile(path string) {
226228
}
227229

228230
}
231+
return nil
229232
}
230233

231234
// Filename returns the filename of the configmap. If the configmap is used for files, the filename contains the path.

generator/configMap_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"testing"
88

9+
"github.com/compose-spec/compose-go/types"
910
v1 "k8s.io/api/core/v1"
1011
"sigs.k8s.io/yaml"
1112
)
@@ -73,3 +74,19 @@ services:
7374
t.Errorf("Expected FOO to be baz, got %s", v)
7475
}
7576
}
77+
78+
func TestAppendBadFile(t *testing.T) {
79+
cm := NewConfigMap(types.ServiceConfig{}, "app", true)
80+
err := cm.AppendFile("foo")
81+
if err == nil {
82+
t.Errorf("Expected error, got nil")
83+
}
84+
}
85+
86+
func TestAppendBadDir(t *testing.T) {
87+
cm := NewConfigMap(types.ServiceConfig{}, "app", true)
88+
err := cm.AppendDir("foo")
89+
if err == nil {
90+
t.Errorf("Expected error, got nil")
91+
}
92+
}

generator/converter.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var keyRegExp = regexp.MustCompile(`^\s*[^#]+:.*`)
9090

9191
// Convert a compose (docker, podman...) project to a helm chart.
9292
// It calls Generate() to generate the chart and then write it to the disk.
93-
func Convert(config ConvertOptions, dockerComposeFile ...string) {
93+
func Convert(config ConvertOptions, dockerComposeFile ...string) error {
9494
var (
9595
templateDir = filepath.Join(config.OutputDir, "templates")
9696
helpersPath = filepath.Join(config.OutputDir, "templates", "_helpers.tpl")
@@ -105,7 +105,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
105105
// go to the root of the project
106106
if err := os.Chdir(filepath.Dir(dockerComposeFile[0])); err != nil {
107107
fmt.Println(utils.IconFailure, err)
108-
os.Exit(1)
108+
return err
109109
}
110110
defer os.Chdir(currentDir) // after the generation, go back to the original directory
111111

@@ -118,13 +118,13 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
118118
project, err := parser.Parse(config.Profiles, config.EnvFiles, dockerComposeFile...)
119119
if err != nil {
120120
fmt.Println(err)
121-
os.Exit(1)
121+
return err
122122
}
123123

124124
// check older version of labels
125125
if err := checkOldLabels(project); err != nil {
126126
fmt.Println(utils.IconFailure, err)
127-
os.Exit(1)
127+
return err
128128
}
129129

130130
// TODO: use katenary.yaml file here to set the labels
@@ -140,7 +140,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
140140
)
141141
if !overwrite {
142142
fmt.Println("Aborting")
143-
os.Exit(126) // 126 is the exit code for "Command invoked cannot execute"
143+
return nil
144144
}
145145
}
146146
fmt.Println() // clean line
@@ -150,7 +150,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
150150
chart, err := Generate(project)
151151
if err != nil {
152152
fmt.Println(err)
153-
os.Exit(1)
153+
return err
154154
}
155155

156156
// if the app version is set from the command line, use it
@@ -194,6 +194,7 @@ func Convert(config ConvertOptions, dockerComposeFile ...string) {
194194

195195
// call helm update if needed
196196
callHelmUpdate(config)
197+
return nil
197198
}
198199

199200
func addChartDoc(values []byte, project *types.Project) []byte {

generator/tools_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ func internalCompileTest(t *testing.T, options ...string) string {
4848
AppVersion: appVersion,
4949
ChartVersion: chartVersion,
5050
}
51-
Convert(convertOptions, "compose.yml")
51+
if err := Convert(convertOptions, "compose.yml"); err != nil {
52+
return err.Error()
53+
}
5254

5355
// launch helm lint to check the generated chart
5456
if helmLint(convertOptions) != nil {

0 commit comments

Comments
 (0)