Skip to content

Commit

Permalink
fix issue #1: output in a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Jul 4, 2016
1 parent fd82f2c commit bea0e4b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
45 changes: 31 additions & 14 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
toStdout := false
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{
ProjectName: "kube",
ComposeFile: composeFile,
Expand Down Expand Up @@ -632,16 +639,16 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {

if c.BoolT("deployment") {
// Create the deployment
print(name, "deployment", datadc, toStdout, generateYaml)
print(name, "deployment", datadc, toStdout, generateYaml, outFile)
} else if c.BoolT("daemonset") {
// Create the daemonset
print(name, "daemonset", datads, toStdout, generateYaml)
print(name, "daemonset", datads, toStdout, generateYaml, outFile)
} else if c.BoolT("replicaset") {
// Create the replicaset container
print(name, "replicaset", datars, toStdout, generateYaml)
print(name, "replicaset", datars, toStdout, generateYaml, outFile)
} else {
// Create the replication controller
print(name, "rc", datarc, toStdout, generateYaml)
print(name, "rc", datarc, toStdout, generateYaml, outFile)
}

// Create the services
Expand All @@ -658,10 +665,9 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {

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 */
Expand All @@ -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)
if generateYaml {
file = fmt.Sprintf("%s-%s.yaml", name, trailing)
}
if outFile != "" {
file = outFile
}
separator := ""
if generateYaml {
separator = "---"
}
if toStdout {
separator := ""
if generateYaml {
separator = "---"
}
fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator)
} else {
if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
logrus.Fatalf("Failed to write %s: %v", trailing, err)
//if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
// 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.
Expand Down
7 changes: 6 additions & 1 deletion cli/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
Value: "docker-compose.yml",
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
cli.BoolFlag{
Name: "deployment,d",
Expand All @@ -57,7 +62,7 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
Usage: "Generate resource file in yaml format",
},
cli.BoolFlag{
Name: "stdout,out",
Name: "stdout",
Usage: "Print Kubernetes objects to stdout",
},
},
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package version

var (
// 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 = "HEAD"
Expand Down

0 comments on commit bea0e4b

Please sign in to comment.