forked from LaconicNetwork/kompose
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:
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
+20
-13
@@ -22,36 +22,43 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
dockerlib "github.com/fsouza/go-dockerclient"
|
||||
"github.com/kubernetes/kompose/pkg/kobject"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"path/filepath"
|
||||
|
||||
"github.com/kubernetes/kompose/pkg/utils/docker"
|
||||
|
||||
"github.com/kubernetes/kompose/pkg/version"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
api "k8s.io/api/core/v1"
|
||||
)
|
||||
|
||||
// Selector used as labels and selector
|
||||
const Selector = "io.kompose.service"
|
||||
|
||||
// Exists returns true if a file path exists.
|
||||
// Otherwise, returns false.
|
||||
func Exists(p string) bool {
|
||||
_, err := os.Stat(p)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// CreateOutFile creates the file to write to if --out is specified
|
||||
func CreateOutFile(out string) (*os.File, error) {
|
||||
var f *os.File
|
||||
var err error
|
||||
if len(out) != 0 {
|
||||
f, err = os.Create(out)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error creating file, os.Create failed")
|
||||
if len(out) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
// Creates directories if "out" contains nonexistent directories.
|
||||
if dir := filepath.Dir(out); !Exists(dir) {
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create directories")
|
||||
}
|
||||
}
|
||||
f, err := os.Create(out)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create file, os.Create failed")
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user