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:
parent
8dc5572ec7
commit
25036e1b22
@ -164,11 +164,17 @@ func objectToRaw(object runtime.Object) runtime.RawExtension {
|
|||||||
|
|
||||||
// PrintList will take the data converted and decide on the commandline attributes given
|
// PrintList will take the data converted and decide on the commandline attributes given
|
||||||
func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
|
func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
|
||||||
|
|
||||||
var f *os.File
|
var f *os.File
|
||||||
dirName := getDirName(opt)
|
dirName := getDirName(opt)
|
||||||
log.Debugf("Target Dir: %s", dirName)
|
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
|
// Check if output file is a directory
|
||||||
isDirVal, err := isDir(opt.OutFile)
|
isDirVal, err := isDir(opt.OutFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -182,6 +188,7 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "transformer.CreateOutFile failed")
|
return errors.Wrap(err, "transformer.CreateOutFile failed")
|
||||||
}
|
}
|
||||||
|
log.Printf("Kubernetes file %q created", opt.OutFile)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,36 +22,43 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
dockerlib "github.com/fsouza/go-dockerclient"
|
dockerlib "github.com/fsouza/go-dockerclient"
|
||||||
"github.com/kubernetes/kompose/pkg/kobject"
|
"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/utils/docker"
|
||||||
|
|
||||||
"github.com/kubernetes/kompose/pkg/version"
|
"github.com/kubernetes/kompose/pkg/version"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
api "k8s.io/api/core/v1"
|
api "k8s.io/api/core/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Selector used as labels and selector
|
// Selector used as labels and selector
|
||||||
const Selector = "io.kompose.service"
|
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
|
// CreateOutFile creates the file to write to if --out is specified
|
||||||
func CreateOutFile(out string) (*os.File, error) {
|
func CreateOutFile(out string) (*os.File, error) {
|
||||||
var f *os.File
|
if len(out) == 0 {
|
||||||
var err error
|
return nil, nil
|
||||||
if len(out) != 0 {
|
}
|
||||||
f, err = os.Create(out)
|
// Creates directories if "out" contains nonexistent directories.
|
||||||
if err != nil {
|
if dir := filepath.Dir(out); !Exists(dir) {
|
||||||
return nil, errors.Wrap(err, "error creating file, os.Create failed")
|
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
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -530,20 +530,6 @@ cmd="kompose --provider openshift -f $KOMPOSE_ROOT/script/test/fixtures/service-
|
|||||||
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/service-label/output-oc.json > /tmp/output-oc.json
|
sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/service-label/output-oc.json > /tmp/output-oc.json
|
||||||
convert::expect_success "$cmd" "/tmp/output-oc.json"
|
convert::expect_success "$cmd" "/tmp/output-oc.json"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
######
|
|
||||||
# Test the output file behavior of kompose convert
|
|
||||||
# Default behavior without -o
|
|
||||||
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -j" "redis-deployment.json" "redis-service.json" "web-deployment.json" "web-service.json"
|
|
||||||
# Behavior with -o <filename>
|
|
||||||
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o output_file -j" "output_file"
|
|
||||||
# Behavior with -o <dirname>
|
|
||||||
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR -j" "$TEMP_DIR/redis-deployment.json" "$TEMP_DIR/redis-service.json" "$TEMP_DIR/web-deployment.json" "$TEMP_DIR/web-service.json"
|
|
||||||
# Behavior with -o <dirname>/<filename>
|
|
||||||
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR/output_file -j" "$TEMP_DIR/output_file"
|
|
||||||
|
|
||||||
######
|
######
|
||||||
# Test charts generate with custom dir
|
# Test charts generate with custom dir
|
||||||
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR -j -c" "$TEMP_DIR/Chart.yaml" "$TEMP_DIR/README.md" "$TEMP_DIR/templates/redis-deployment.json" "$TEMP_DIR/templates/redis-service.json" "$TEMP_DIR/templates/web-deployment.json" "$TEMP_DIR/templates/web-service.json"
|
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR -j -c" "$TEMP_DIR/Chart.yaml" "$TEMP_DIR/README.md" "$TEMP_DIR/templates/redis-deployment.json" "$TEMP_DIR/templates/redis-service.json" "$TEMP_DIR/templates/web-deployment.json" "$TEMP_DIR/templates/web-service.json"
|
||||||
|
|||||||
@ -63,5 +63,18 @@ os_output="$KOMPOSE_ROOT/script/test/fixtures/$DIR/output-os.json"
|
|||||||
convert::expect_success_and_warning "$k8s_cmd" "$k8s_output"
|
convert::expect_success_and_warning "$k8s_cmd" "$k8s_output"
|
||||||
convert::expect_success_and_warning "$os_cmd" "$os_output"
|
convert::expect_success_and_warning "$os_cmd" "$os_output"
|
||||||
|
|
||||||
|
######
|
||||||
|
# Test the output file behavior of kompose convert
|
||||||
|
# Default behavior without -o
|
||||||
|
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -j" "redis-deployment.json" "redis-service.json" "web-deployment.json" "web-service.json"
|
||||||
|
# Behavior with -o <filename>
|
||||||
|
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o output_file -j" "output_file"
|
||||||
|
# Behavior with -o <dirname>
|
||||||
|
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR -j" "$TEMP_DIR/redis-deployment.json" "$TEMP_DIR/redis-service.json" "$TEMP_DIR/web-deployment.json" "$TEMP_DIR/web-service.json"
|
||||||
|
# Behavior with -o <dirname>/<filename>
|
||||||
|
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR/output_file -j" "$TEMP_DIR/output_file"
|
||||||
|
# Behavior with -o <non-existent-dirname>/
|
||||||
|
dst=$TEMP_DIR/output_dir/
|
||||||
|
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $dst -j" "${dst}redis-deployment.json" "${dst}redis-service.json" "${dst}web-deployment.json" "${dst}web-service.json"
|
||||||
|
# Behavior with -o <non-existent-dirname>/<filename>
|
||||||
|
convert::check_artifacts_generated "kompose -f $KOMPOSE_ROOT/script/test/fixtures/redis-example/docker-compose.yml convert -o $TEMP_DIR/output_dir2/output_file -j" "$TEMP_DIR/output_dir2/output_file"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user