Create directories if --out ends with "/" or includes nonexistent directories (#1350)

* Create directory if --out ends with "/"

* Create directories if "out" contains nonexistent directories
This commit is contained in:
namusyaka
2020-11-08 23:18:39 +08:00
committed by GitHub
parent 8dc5572ec7
commit 25036e1b22
4 changed files with 43 additions and 30 deletions
+8 -1
View File
@@ -164,11 +164,17 @@ func objectToRaw(object runtime.Object) runtime.RawExtension {
// PrintList will take the data converted and decide on the commandline attributes given
func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
var f *os.File
dirName := getDirName(opt)
log.Debugf("Target Dir: %s", dirName)
// Create a directory if "out" ends with "/" and does not exist.
if !transformer.Exists(opt.OutFile) && strings.HasSuffix(opt.OutFile, "/") {
if err := os.MkdirAll(opt.OutFile, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create a directory")
}
}
// Check if output file is a directory
isDirVal, err := isDir(opt.OutFile)
if err != nil {
@@ -182,6 +188,7 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
if err != nil {
return errors.Wrap(err, "transformer.CreateOutFile failed")
}
log.Printf("Kubernetes file %q created", opt.OutFile)
defer f.Close()
}