From 9f51697cccf42b827c7037e30e157320bd72a647 Mon Sep 17 00:00:00 2001 From: Jordan Brockopp Date: Fri, 21 Feb 2020 12:19:49 -0600 Subject: [PATCH] fix(config): create dir path before file write (#30) --- cmd/vela-kubernetes/config.go | 6 ++++++ cmd/vela-kubernetes/config_test.go | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/cmd/vela-kubernetes/config.go b/cmd/vela-kubernetes/config.go index b773ae2..c7fb324 100644 --- a/cmd/vela-kubernetes/config.go +++ b/cmd/vela-kubernetes/config.go @@ -74,5 +74,11 @@ func (c *Config) Write() error { // create full path for .kube/config file path := filepath.Join(home, ".kube/config") + // send Filesystem call to create directory path for .kube/config file + err = a.Fs.MkdirAll(filepath.Dir(path), 0777) + if err != nil { + return err + } + return a.WriteFile(path, []byte(c.File), 0600) } diff --git a/cmd/vela-kubernetes/config_test.go b/cmd/vela-kubernetes/config_test.go index 27dd573..26724c0 100644 --- a/cmd/vela-kubernetes/config_test.go +++ b/cmd/vela-kubernetes/config_test.go @@ -76,6 +76,25 @@ func TestKubernetes_Config_Write(t *testing.T) { } } +func TestKubernetes_Config_Write_Error(t *testing.T) { + // setup filesystem + appFS = afero.NewReadOnlyFs(afero.NewMemMapFs()) + + // setup types + c := &Config{ + Action: "apply", + File: "file", + Cluster: "cluster", + Context: "context", + Namespace: "namespace", + } + + err := c.Write() + if err == nil { + t.Errorf("Write should have returned err") + } +} + func TestKubernetes_Config_Write_NoFile(t *testing.T) { // setup filesystem appFS = afero.NewMemMapFs()