Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 7, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 63 additions & 49 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,47 @@ import (
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/intstr"

"github.com/ghodss/yaml"
"github.com/fatih/structs"
"github.com/ghodss/yaml"
)

type ProjectAction func(project *project.Project, c *cli.Context)

const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
var unsupportedKey = map[string]string {
"Build": "",
"CapAdd": "",
"CapDrop": "",
"CPUSet": "",
"CPUShares": "",

var unsupportedKey = map[string]string{
"Build": "",
"CapAdd": "",
"CapDrop": "",
"CPUSet": "",
"CPUShares": "",
"ContainerName": "",
"Devices": "",
"DNS": "",
"DNSSearch": "",
"Dockerfile": "",
"DomainName": "",
"Entrypoint": "",
"EnvFile": "",
"Hostname": "",
"LogDriver": "",
"MemLimit": "",
"MemSwapLimit": "",
"Net": "",
"Pid": "",
"Uts": "",
"Ipc": "",
"ReadOnly": "",
"StdinOpen": "",
"SecurityOpt": "",
"Tty": "",
"User": "",
"VolumeDriver": "",
"VolumesFrom": "",
"Expose": "",
"Devices": "",
"DNS": "",
"DNSSearch": "",
"Dockerfile": "",
"DomainName": "",
"Entrypoint": "",
"EnvFile": "",
"Hostname": "",
"LogDriver": "",
"MemLimit": "",
"MemSwapLimit": "",
"Net": "",
"Pid": "",
"Uts": "",
"Ipc": "",
"ReadOnly": "",
"StdinOpen": "",
"SecurityOpt": "",
"Tty": "",
"User": "",
"VolumeDriver": "",
"VolumesFrom": "",
"Expose": "",
"ExternalLinks": "",
"LogOpt": "",
"ExtraHosts": "",
"LogOpt": "",
"ExtraHosts": "",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes above this line are just gofmt

}

// RandStringBytes generates randomly n-character string
Expand Down Expand Up @@ -276,6 +277,17 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
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 @@ -679,16 +691,16 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {

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

// Create the services
Expand All @@ -705,10 +717,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 @@ -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)
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