Skip to content

Commit

Permalink
Move target folder creation at startup time
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bpineau committed Apr 29, 2018
1 parent 74881fb commit 31dd524
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 8 additions & 0 deletions cmd/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/spf13/afero"
"github.com/spf13/cobra"

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

var (
restcfg client.Interface
appFs = afero.NewOsFs()

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

err = appFs.MkdirAll(filepath.Clean(localDir), 0700)
if err != nil {
return fmt.Errorf("Can't create directory %s: %v", localDir, err)
}

repo, err := git.New(logger, dryRun, localDir, gitURL).Start()
if err != nil {
return fmt.Errorf("failed to start git repo handler: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"testing"

"github.com/spf13/afero"
"k8s.io/client-go/rest"
)

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

func TestRootCmd(t *testing.T) {
restcfg = new(mockClient)
appFs = afero.NewMemMapFs()
RootCmd.SetOutput(new(bytes.Buffer))
RootCmd.SetArgs([]string{
"--config",
Expand Down
4 changes: 0 additions & 4 deletions pkg/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ func New(log logger, events event.Notifier, localDir string, gcInterval int, dry
// Start continuously receive events and saves them to disk as files
func (w *Listener) Start() *Listener {
w.logger.Infof("Starting event recorder")
err := appFs.MkdirAll(filepath.Clean(w.localDir), 0700)
if err != nil {
panic(fmt.Sprintf("Can't create directory %s: %v", w.localDir, err))
}

go func() {
evCh := w.events.ReadChan()
Expand Down

0 comments on commit 31dd524

Please sign in to comment.