Skip to content

Commit

Permalink
Merge pull request #19 from janetkuo
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Jul 7, 2016
2 parents e9d6a52 + 134ba7a commit 30fa304
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,26 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
logrus.Fatalf("Failed to parse the compose project from %s: %v", composeFile, err)
}

//// check flags
//if c.BoolT("yaml") {
// generateYaml = true
//}
//
//if c.BoolT("stdout") {
// toStdout = true
//}

// Create the file f to write to if --out is specified
var f *os.File
var err error
if len(outFile) != 0 {
f, err = os.Create(outFile)
if err != nil {
logrus.Fatalf("error opening file: %v", err)
}
defer f.Close()
}

var mServices map[string]api.Service = make(map[string]api.Service)
var serviceLinks []string

Expand Down Expand Up @@ -689,20 +709,19 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
// If --out or --stdout is set, the validation should already prevent multiple controllers being generated
if createD {
// Create the deployment
print(name, "deployment", datadc, toStdout, generateYaml, outFile)
print(name, "deployment", datadc, toStdout, generateYaml, f)
}
if createDS {
// Create the daemonset
print(name, "daemonset", datads, toStdout, generateYaml, outFile)
print(name, "daemonset", datads, toStdout, generateYaml, f)
}
if createRS {
// Create the replicaset container
print(name, "replicaset", datars, toStdout, generateYaml, outFile)
print(name, "replicaset", datars, toStdout, generateYaml, f)
}
// We can create RC when we either don't print to --out or --stdout, or we don't create any other controllers
if !singleOutput || (!createD && !createDS && !createRS) {
// Create the replication controller
print(name, "rc", datarc, toStdout, generateYaml, outFile)
print(name, "rc", datarc, toStdout, generateYaml, f)
}

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

logrus.Debugf("%s\n", datasvc)

print(k, "svc", datasvc, toStdout, generateYaml, outFile)
print(k, "svc", datasvc, toStdout, generateYaml, f)
}
}
}
if f != nil {
fmt.Fprintf(os.Stdout, "file %q created\n", outFile)
}

/* Need to iterate through one more time to ensure we capture all service/rc */
for name := range p.Configs {
Expand All @@ -746,30 +768,29 @@ func checkUnsupportedKey(service project.ServiceConfig) {
}
}

func print(name, trailing string, data []byte, toStdout, generateYaml bool, outFile string) {
func print(name, trailing string, data []byte, toStdout, generateYaml bool, f *os.File) {
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 {
fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator)
} else {
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 {
} else if f != nil {
// Write all content to a single file f
if _, err := f.WriteString(fmt.Sprintf("%s%s\n", string(data), separator)); err != nil {
logrus.Fatalf("Failed to write %s to file: %v", trailing, err)
}
fmt.Println("file " + file + " has been created")
f.Sync()
} else {
// Write content separately to each file
if err := ioutil.WriteFile(file, []byte(data), 0644); err != nil {
logrus.Fatalf("Failed to write %s: %v", trailing, err)
}
fmt.Fprintf(os.Stdout, "file %q created\n", file)
}
}

Expand Down

0 comments on commit 30fa304

Please sign in to comment.