Remove redundant file creation message, and always overwirte files when converting

This commit is contained in:
Janet Kuo 2016-07-06 16:06:29 -07:00
parent 22898a97c0
commit 134ba7af04

View File

@ -38,13 +38,14 @@ import (
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/intstr" "k8s.io/kubernetes/pkg/util/intstr"
"github.com/ghodss/yaml"
"github.com/fatih/structs" "github.com/fatih/structs"
"github.com/ghodss/yaml"
) )
type ProjectAction func(project *project.Project, c *cli.Context) type ProjectAction func(project *project.Project, c *cli.Context)
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789" const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
var unsupportedKey = map[string]string{ var unsupportedKey = map[string]string{
"Build": "", "Build": "",
"CapAdd": "", "CapAdd": "",
@ -276,6 +277,17 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
toStdout = true 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 mServices map[string]api.Service = make(map[string]api.Service)
var serviceLinks []string var serviceLinks []string
@ -679,16 +691,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, outFile) print(name, "deployment", datadc, toStdout, generateYaml, f)
} else if c.BoolT("daemonset") { } else if c.BoolT("daemonset") {
// Create the daemonset // Create the daemonset
print(name, "daemonset", datads, toStdout, generateYaml, outFile) print(name, "daemonset", datads, toStdout, generateYaml, f)
} else if c.BoolT("replicaset") { } else if c.BoolT("replicaset") {
// Create the replicaset container // Create the replicaset container
print(name, "replicaset", datars, toStdout, generateYaml, outFile) print(name, "replicaset", datars, toStdout, generateYaml, f)
} else { } else {
// Create the replication controller // Create the replication controller
print(name, "rc", datarc, toStdout, generateYaml, outFile) print(name, "rc", datarc, toStdout, generateYaml, f)
} }
// Create the services // Create the services
@ -705,10 +717,13 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
logrus.Debugf("%s\n", datasvc) 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 */ /* Need to iterate through one more time to ensure we capture all service/rc */
for name := range p.Configs { for name := range p.Configs {
@ -732,30 +747,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) 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 := "" separator := ""
if generateYaml { if generateYaml {
separator = "---" separator = "---"
} }
if toStdout { if toStdout {
fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator) fmt.Fprintf(os.Stdout, "%s%s\n", string(data), separator)
} else { } else if f != nil {
f, err := os.OpenFile(file, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0644) // Write all content to a single file f
if err != nil { if _, err := f.WriteString(fmt.Sprintf("%s%s\n", string(data), separator)); 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) 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)
} }
} }