Skip to content

Commit b5330eb

Browse files
author
Enda Phelan
committed
feat: check if directory exists
1 parent 6c4f063 commit b5330eb

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

kafka.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/cmd/serviceaccount/create/create.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"time"
9+
"path"
910

1011
"github.com/MakeNowJust/heredoc"
1112
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/managedservices"
@@ -128,7 +129,12 @@ func runCreate(opts *options) error {
128129
}
129130

130131
if opts.filename != "" {
131-
fileName = opts.filename
132+
fileName = path.Clean(opts.filename)
133+
}
134+
135+
fileDir := path.Dir(fileName)
136+
if !pathExists(fileDir) {
137+
return fmt.Errorf("Directory '%v' does not exist", fileDir)
132138
}
133139

134140
t := time.Now()
@@ -144,23 +150,22 @@ func runCreate(opts *options) error {
144150

145151
dataToWrite := []byte(fileContent)
146152

147-
if fileExists(fileName) && !opts.force {
153+
if pathExists(fileName) && !opts.force {
148154
fmt.Fprintf(os.Stderr, "File '%v' already exist. Use --force flag to overwrite the file, or use the --output-file flag to choose a custom location\n", fileName)
149155
return nil
150156
}
151157

152158
err = ioutil.WriteFile(fileName, dataToWrite, 0600)
153159
if err != nil {
154-
fmt.Fprintf(os.Stderr, "Error saving file %v \n", err)
155-
return nil
160+
return fmt.Errorf("Could not save file: %w", err)
156161
}
157162

158163
fmt.Fprintf(os.Stderr, "Successfully saved credentials to %v \n", fileName)
159164

160165
return nil
161166
}
162167

163-
func fileExists(path string) bool {
168+
func pathExists(path string) bool {
164169
_, err := os.Stat(path)
165170
return !os.IsNotExist(err)
166171
}

0 commit comments

Comments
 (0)