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

Add -yaml flag #31

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
38 changes: 30 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@ import (

"github.com/docker/libcompose/config"
"github.com/docker/libcompose/project"
"gopkg.in/yaml.v2"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)

var (
composeFile string
outputDir string
asYml bool
)

func init() {
flag.StringVar(&composeFile, "compose-file", "docker-compose.yml", "Specify an alternate compose `file`")
flag.StringVar(&outputDir, "output-dir", "output", "Kubernetes configs output `directory`")
flag.BoolVar(&asYml, "yaml", false, "output yaml instead of json")
}

func main() {
Expand Down Expand Up @@ -176,14 +179,33 @@ func main() {
if err != nil {
log.Fatalf("Failed to marshal the replication controller: %v", err)
}

// Save the replication controller for the Docker compose service to the
// configs directory.
outputFileName := fmt.Sprintf("%s-rc.json", name)
outputFilePath := filepath.Join(outputDir, outputFileName)
if err := ioutil.WriteFile(outputFilePath, data, 0644); err != nil {
log.Fatalf("Failed to write replication controller %s: %v", outputFileName, err)
if !asYml {
// Save the replication controller for the Docker compose service to the
// configs directory.
outputFileName := fmt.Sprintf("%s-rc.json", name)
outputFilePath := filepath.Join(outputDir, outputFileName)
if err := ioutil.WriteFile(outputFilePath, data, 0644); err != nil {
log.Fatalf("Failed to write replication controller %s: %v", outputFileName, err)
}
fmt.Println(outputFilePath)
} else {
// Save the replication controller to Yaml file
var exp interface{}
// because yaml is not directly usable grom api, we can unmarshal json to interface{}
// and then write yaml file
json.Unmarshal(data, &exp)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason we need to unmarshal the generated json to an interface{} rather than just yaml.Marshal(rc)?

data, err = yaml.Marshal(exp)
if err != nil {
log.Fatalf("Failed to marshal the replication controller to yaml: %v", err)
}
// Save the replication controller for the Docker compose service to the
// configs directory.
outputFileName := fmt.Sprintf("%s-rc.yml", name)
outputFilePath := filepath.Join(outputDir, outputFileName)
if err := ioutil.WriteFile(outputFilePath, data, 0644); err != nil {
log.Fatalf("Failed to write replication controller %s: %v", outputFileName, err)
}
fmt.Println(outputFilePath)
}
fmt.Println(outputFilePath)
}
}