fix issue #1: output in a single file

This commit is contained in:
ngtuna 2016-07-04 14:39:08 +07:00
parent fd82f2cba4
commit bea0e4b7c3
3 changed files with 38 additions and 16 deletions

View File

@ -215,6 +215,13 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
toStdout := false toStdout := false
composeFile := c.String("file") composeFile := c.String("file")
var outFile string
outFile = c.String("out")
if (outFile != "") && c.BoolT("stdout") {
logrus.Fatalf("Failed: -o and --stdout can't be put at the same time")
}
p = project.NewProject(&project.Context{ p = project.NewProject(&project.Context{
ProjectName: "kube", ProjectName: "kube",
ComposeFile: composeFile, ComposeFile: composeFile,
@ -632,16 +639,16 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
if c.BoolT("deployment") { if c.BoolT("deployment") {
// Create the deployment // Create the deployment
print(name, "deployment", datadc, toStdout, generateYaml) print(name, "deployment", datadc, toStdout, generateYaml, outFile)
} else if c.BoolT("daemonset") { } else if c.BoolT("daemonset") {
// Create the daemonset // Create the daemonset
print(name, "daemonset", datads, toStdout, generateYaml) print(name, "daemonset", datads, toStdout, generateYaml, outFile)
} else if c.BoolT("replicaset") { } else if c.BoolT("replicaset") {
// Create the replicaset container // Create the replicaset container
print(name, "replicaset", datars, toStdout, generateYaml) print(name, "replicaset", datars, toStdout, generateYaml, outFile)
} else { } else {
// Create the replication controller // Create the replication controller
print(name, "rc", datarc, toStdout, generateYaml) print(name, "rc", datarc, toStdout, generateYaml, outFile)
} }
// Create the services // Create the services
@ -658,10 +665,9 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
logrus.Debugf("%s\n", datasvc) logrus.Debugf("%s\n", datasvc)
print(k, "svc", datasvc, toStdout, generateYaml) print(k, "svc", datasvc, toStdout, generateYaml, outFile)
} }
} }
} }
/* Need to iterate through one more time to ensure we capture all service/rc */ /* Need to iterate through one more time to ensure we capture all service/rc */
@ -675,23 +681,34 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
} }
} }
func print(name, trailing string, data []byte, toStdout, generateYaml bool) { func print(name, trailing string, data []byte, toStdout, generateYaml bool, outFile string) {
file := fmt.Sprintf("%s-%s.json", name, trailing) file := fmt.Sprintf("%s-%s.json", name, trailing)
if generateYaml { if generateYaml {
file = fmt.Sprintf("%s-%s.yaml", name, trailing) file = fmt.Sprintf("%s-%s.yaml", name, trailing)
} }
if outFile != "" {
file = outFile
}
separator := ""
if generateYaml {
separator = "---"
}
if toStdout { if toStdout {
separator := ""
if generateYaml {
separator = "---"
}
fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator) fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator)
} else { } else {
if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil { //if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
logrus.Fatalf("Failed to write %s: %v", trailing, err) // logrus.Fatalf("Failed to write %s: %v", trailing, err)
//}
f, err := os.OpenFile(file, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0644)
if err != nil {
logrus.Fatalf("error opening file: %v", err)
}
defer f.Close()
if _, err = f.WriteString(string(data) + "\n" + separator); err != nil {
logrus.Fatalf("Failed to write %s to file: %v", trailing, err)
} }
}
}
} }
// ProjectKuberUp brings up rc, svc. // ProjectKuberUp brings up rc, svc.

View File

@ -35,6 +35,11 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
Value: "docker-compose.yml", Value: "docker-compose.yml",
EnvVar: "COMPOSE_FILE", EnvVar: "COMPOSE_FILE",
}, },
cli.StringFlag{
Name: "out,o",
Usage: "Specify file name in order to save objects into",
EnvVar: "OUTPUT_FILE",
},
// TODO: validate the flags and make sure only one type is specified // TODO: validate the flags and make sure only one type is specified
cli.BoolFlag{ cli.BoolFlag{
Name: "deployment,d", Name: "deployment,d",
@ -57,7 +62,7 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
Usage: "Generate resource file in yaml format", Usage: "Generate resource file in yaml format",
}, },
cli.BoolFlag{ cli.BoolFlag{
Name: "stdout,out", Name: "stdout",
Usage: "Print Kubernetes objects to stdout", Usage: "Print Kubernetes objects to stdout",
}, },
}, },

View File

@ -18,7 +18,7 @@ package version
var ( var (
// VERSION should be updated by hand at each release // VERSION should be updated by hand at each release
VERSION = "0.0.5-dev" VERSION = "0.0.1-alpha"
// GITCOMMIT will be overwritten automatically by the build system // GITCOMMIT will be overwritten automatically by the build system
GITCOMMIT = "HEAD" GITCOMMIT = "HEAD"