Skip to content

Commit

Permalink
Merge pull request #5 from janetkuo/print-stdout
Browse files Browse the repository at this point in the history
Support printing to stdout
  • Loading branch information
ngtuna committed Jul 3, 2016
2 parents 5380233 + 6ae79d0 commit fd82f2c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
89 changes: 41 additions & 48 deletions cli/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package app
import (
"fmt"
"math/rand"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -211,6 +212,7 @@ func ProjectKuberScale(p *project.Project, c *cli.Context) {
// ProjectKuberConvert tranforms docker compose to k8s objects
func ProjectKuberConvert(p *project.Project, c *cli.Context) {
generateYaml := false
toStdout := false
composeFile := c.String("file")

p = project.NewProject(&project.Context{
Expand All @@ -226,6 +228,10 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
generateYaml = true
}

if c.BoolT("stdout") {
toStdout = true
}

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

Expand Down Expand Up @@ -624,47 +630,21 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
}
}

fileRC := fmt.Sprintf("%s-rc.json", name)
if generateYaml == true {
fileRC = fmt.Sprintf("%s-rc.yaml", name)
}
if err := ioutil.WriteFile(fileRC, []byte(datarc), 0644); err != nil {
logrus.Fatalf("Failed to write replication controller: %v", err)
}

/* Create the deployment container */
if c.BoolT("deployment") {
fileDC := fmt.Sprintf("%s-deployment.json", name)
if generateYaml == true {
fileDC = fmt.Sprintf("%s-deployment.yaml", name)
}
if err := ioutil.WriteFile(fileDC, []byte(datadc), 0644); err != nil {
logrus.Fatalf("Failed to write deployment container: %v", err)
}
}

/* Create the daemonset container */
if c.BoolT("daemonset") {
fileDS := fmt.Sprintf("%s-daemonset.json", name)
if generateYaml == true {
fileDS = fmt.Sprintf("%s-daemonset.yaml", name)
}
if err := ioutil.WriteFile(fileDS, []byte(datads), 0644); err != nil {
logrus.Fatalf("Failed to write daemonset: %v", err)
}
}

/* Create the replicaset container */
if c.BoolT("replicaset") {
fileRS := fmt.Sprintf("%s-replicaset.json", name)
if generateYaml == true {
fileRS = fmt.Sprintf("%s-replicaset.yaml", name)
}
if err := ioutil.WriteFile(fileRS, []byte(datars), 0644); err != nil {
logrus.Fatalf("Failed to write replicaset: %v", err)
}
}

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

// Create the services
for k, v := range mServices {
for i := 0; i < len(serviceLinks); i++ {
// convert datasvc to json / yaml
Expand All @@ -678,16 +658,10 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {

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

fileSVC := fmt.Sprintf("%s-svc.json", k)
if generateYaml == true {
fileSVC = fmt.Sprintf("%s-svc.yaml", k)
}

if err := ioutil.WriteFile(fileSVC, []byte(datasvc), 0644); err != nil {
logrus.Fatalf("Failed to write service controller: %v", err)
}
print(k, "svc", datasvc, toStdout, generateYaml)
}
}

}

/* Need to iterate through one more time to ensure we capture all service/rc */
Expand All @@ -701,6 +675,25 @@ func ProjectKuberConvert(p *project.Project, c *cli.Context) {
}
}

func print(name, trailing string, data []byte, toStdout, generateYaml bool) {
file := fmt.Sprintf("%s-%s.json", name, trailing)
if generateYaml {
file = fmt.Sprintf("%s-%s.yaml", name, trailing)
}
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)
}
}

}

// ProjectKuberUp brings up rc, svc.
func ProjectKuberUp(p *project.Project, c *cli.Context) {
factory := cmdutil.NewFactory(nil)
Expand Down
7 changes: 6 additions & 1 deletion cli/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
package command

import (
"github.com/urfave/cli"
"github.com/docker/libcompose/project"
"github.com/skippbox/kompose2/cli/app"
"github.com/urfave/cli"
)

// ConvertCommand defines the kompose convert subcommand.
Expand All @@ -35,6 +35,7 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
Value: "docker-compose.yml",
EnvVar: "COMPOSE_FILE",
},
// TODO: validate the flags and make sure only one type is specified
cli.BoolFlag{
Name: "deployment,d",
Usage: "Generate a deployment resource file",
Expand All @@ -55,6 +56,10 @@ func ConvertCommand(factory app.ProjectFactory) cli.Command {
Name: "yaml, y",
Usage: "Generate resource file in yaml format",
},
cli.BoolFlag{
Name: "stdout,out",
Usage: "Print Kubernetes objects to stdout",
},
},
}
}
Expand Down

0 comments on commit fd82f2c

Please sign in to comment.