Skip to content

Commit 31dd524

Browse files
committed
Move target folder creation at startup time
Where it belongs, so we can fail early and cleanly. Also, doing that while on a separate goroutine was pretty bad (since we'd have to either ignore the error, or panic in a package).
1 parent 74881fb commit 31dd524

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

cmd/execute.go

+8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55
"os"
66
"os/signal"
7+
"path/filepath"
78
"syscall"
89

10+
"github.com/spf13/afero"
911
"github.com/spf13/cobra"
1012

1113
"github.com/bpineau/katafygio/pkg/client"
@@ -22,6 +24,7 @@ const appName = "katafygio"
2224

2325
var (
2426
restcfg client.Interface
27+
appFs = afero.NewOsFs()
2528

2629
// RootCmd is our main entry point, launching runE()
2730
RootCmd = &cobra.Command{
@@ -47,6 +50,11 @@ func runE(cmd *cobra.Command, args []string) (err error) {
4750
}
4851
}
4952

53+
err = appFs.MkdirAll(filepath.Clean(localDir), 0700)
54+
if err != nil {
55+
return fmt.Errorf("Can't create directory %s: %v", localDir, err)
56+
}
57+
5058
repo, err := git.New(logger, dryRun, localDir, gitURL).Start()
5159
if err != nil {
5260
return fmt.Errorf("failed to start git repo handler: %v", err)

cmd/execute_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"testing"
66

7+
"github.com/spf13/afero"
78
"k8s.io/client-go/rest"
89
)
910

@@ -15,6 +16,7 @@ func (m *mockClient) GetRestConfig() *rest.Config {
1516

1617
func TestRootCmd(t *testing.T) {
1718
restcfg = new(mockClient)
19+
appFs = afero.NewMemMapFs()
1820
RootCmd.SetOutput(new(bytes.Buffer))
1921
RootCmd.SetArgs([]string{
2022
"--config",

pkg/recorder/recorder.go

-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ func New(log logger, events event.Notifier, localDir string, gcInterval int, dry
6262
// Start continuously receive events and saves them to disk as files
6363
func (w *Listener) Start() *Listener {
6464
w.logger.Infof("Starting event recorder")
65-
err := appFs.MkdirAll(filepath.Clean(w.localDir), 0700)
66-
if err != nil {
67-
panic(fmt.Sprintf("Can't create directory %s: %v", w.localDir, err))
68-
}
6965

7066
go func() {
7167
evCh := w.events.ReadChan()

0 commit comments

Comments
 (0)