Skip to content

Commit

Permalink
Merge pull request #28 from gondor/master
Browse files Browse the repository at this point in the history
switch all CLI output to Go Template
  • Loading branch information
gondor committed May 6, 2016
2 parents d3206e1 + 7e2ece1 commit d8af997
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 331 deletions.
69 changes: 62 additions & 7 deletions commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@ import (
"github.com/ContainX/depcon/utils"
"github.com/spf13/cobra"
"io"
"text/template"
)

const (
T_CONFIG_ENV = `
{{ "NAME" }} {{ "TYPE" }} {{ "ENDPOINT" }} {{ "AUTH" }} {{ "DEFAULT" }}
{{ range . }}{{ .Name }} {{ .EnvType }} {{ .HostURL }} {{ .Auth | boolToYesNo }} {{ .Default | defaultEnvToStr }}
{{end}}`
)


type ConfigEnvironments struct {
DefaultEnv string
Envs map[string]*cliconfig.ConfigEnvironment
}

type EnvironmentSummary struct {
Name string
EnvType string
HostURL string
Auth bool
Default bool
}

var ValidOutputs []string = []string{"json", "yaml", "column"}
var ErrInvalidOutputFormat = errors.New("Invalid Output specified. Must be 'json','yaml' or 'column'")
var ErrInvalidRootOption = errors.New("Invalid chroot option specified. Must be 'true' or 'false'")
Expand Down Expand Up @@ -66,7 +83,9 @@ var configListCmd = &cobra.Command{
Use: "list",
Short: "List current environments",
Run: func(cmd *cobra.Command, args []string) {
cli.Output(&ConfigEnvironments{DefaultEnv: configFile.DefaultEnv, Envs: configFile.Environments}, nil)
ce := &ConfigEnvironments{DefaultEnv: configFile.DefaultEnv, Envs: configFile.Environments}
template := templateFor(T_CONFIG_ENV, ce.toEnvironmentMap())
cli.Output(template, nil)
},
}

Expand Down Expand Up @@ -149,17 +168,53 @@ func init() {
configCmd.AddCommand(configEnvCmd, configOutputCmd, configRootServiceCmd)
}

func (e ConfigEnvironments) ToColumns(output io.Writer) error {
w := cli.NewTabWriter(output)
fmt.Fprintln(w, "\nNAME\tTYPE\tURI\tAUTH\tDEFAULT")
type ConfigTemplate struct {
cli.FormatData
}

func templateFor(template string, data interface{}) ConfigTemplate {
return ConfigTemplate{cli.FormatData{ Template: template, Data: data, Funcs: buildFuncMap() }}
}

func (d ConfigTemplate) ToColumns(output io.Writer) error {
return d.FormatData.ToColumns(output)
}

func (d ConfigTemplate) Data() cli.FormatData {
return d.FormatData
}

func (e ConfigEnvironments) toEnvironmentMap() []*EnvironmentSummary {

arr := []*EnvironmentSummary{}

for k, v := range e.Envs {
var sc cliconfig.ServiceConfig
switch v.EnvironmentType() {
case cliconfig.TypeMarathon:
sc = *v.Marathon
}
fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%v\n", k, v.EnvironmentType(), sc.HostUrl, sc.Username != "", k == e.DefaultEnv)
arr = append(arr, &EnvironmentSummary{
Name: k,
EnvType: v.EnvironmentType(),
HostURL: sc.HostUrl,
Auth: sc.Username != "",
Default: k == e.DefaultEnv,
})
}
cli.FlushWriter(w)
return nil
return arr
}

func buildFuncMap() template.FuncMap {
funcMap := template.FuncMap{
"defaultEnvToStr": defaultEnvToStr,
}
return funcMap
}

func defaultEnvToStr(b bool) string {
if b {
return "true"
}
return "-"
}
3 changes: 2 additions & 1 deletion commands/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func PrintFormat(formatter cli.Formatter) {

func printEncodedType(formatter cli.Formatter, encoder encoding.EncoderType) {
e, _ := encoding.NewEncoder(encoder)
str, _ := e.MarshalIndent(formatter)
str, _ := e.MarshalIndent(formatter.Data().Data)
fmt.Println(str)
}

Expand All @@ -63,3 +63,4 @@ func printColumn(formatter cli.Formatter) {
log.Error("Error: %s", err.Error())
}
}

3 changes: 1 addition & 2 deletions commands/marathon/app_bg_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func deployBlueGreenCmd(cmd *cobra.Command, args []string) {
cli.Output(nil, err)
os.Exit(1)
}
cli.Output(Application{a}, err)

cli.Output(templateFor(T_APPLICATIONS, a), err)
}

func bgc(c *cobra.Command) bluegreen.BlueGreen {
Expand Down
20 changes: 10 additions & 10 deletions commands/marathon/app_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var appListCmd = &cobra.Command{
Short: "List all applications",
Run: func(cmd *cobra.Command, args []string) {
v, e := client(cmd).ListApplications()
cli.Output(Applications{v}, e)
cli.Output(templateFor(T_APPLICATIONS, v), e)
},
}

Expand All @@ -68,7 +68,7 @@ var appGetCmd = &cobra.Command{
return
}
v, e := client(cmd).GetApplication(args[0])
cli.Output(Application{v}, e)
cli.Output(templateFor(T_APPLICATION, v), e)
},
}

Expand All @@ -81,7 +81,7 @@ var appVersionsCmd = &cobra.Command{
return
}
v, e := client(cmd).ListVersions(args[0])
cli.Output(Versions{v}, e)
cli.Output(templateFor(T_VERSIONS, v), e)
},
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func createApp(cmd *cobra.Command, args []string) {
}
os.Exit(1)
}
cli.Output(Application{result}, e)
cli.Output(templateFor(T_APPLICATIONS, result), e)
}

func parseParamsFile(filename string) (map[string]string, error) {
Expand Down Expand Up @@ -208,7 +208,7 @@ func restartApp(cmd *cobra.Command, args []string) {
force, _ := cmd.Flags().GetBool(FORCE_FLAG)

v, e := client(cmd).RestartApplication(args[0], force)
cli.Output(DeploymentId{v}, e)
cli.Output(templateFor(T_DEPLOYMENT_ID, v), e)
waitForDeploymentIfFlagged(cmd, v.DeploymentID)
}

Expand All @@ -218,7 +218,7 @@ func destroyApp(cmd *cobra.Command, args []string) {
}

v, e := client(cmd).DestroyApplication(args[0])
cli.Output(DeploymentId{v}, e)
cli.Output(templateFor(T_DEPLOYMENT_ID, v), e)
waitForDeploymentIfFlagged(cmd, v.DeploymentID)
}

Expand All @@ -233,7 +233,7 @@ func scaleApp(cmd *cobra.Command, args []string) {
os.Exit(1)
}
v, e := client(cmd).ScaleApplication(args[0], instances)
cli.Output(DeploymentId{v}, e)
cli.Output(templateFor(T_DEPLOYMENT_ID, v), e)
waitForDeploymentIfFlagged(cmd, v.DeploymentID)
}

Expand All @@ -251,7 +251,7 @@ func updateAppCPU(cmd *cobra.Command, args []string) {
}
update := marathon.NewApplication(args[0]).CPU(cpu)
v, e := client(cmd).UpdateApplication(update, wait)
cli.Output(Application{v}, e)
cli.Output(templateFor(T_APPLICATIONS, v), e)
}

func updateAppMemory(cmd *cobra.Command, args []string) {
Expand All @@ -268,7 +268,7 @@ func updateAppMemory(cmd *cobra.Command, args []string) {
}
update := marathon.NewApplication(args[0]).Memory(mem)
v, e := client(cmd).UpdateApplication(update, wait)
cli.Output(Application{v}, e)
cli.Output(templateFor(T_APPLICATIONS, v), e)
}

func rollbackAppVersion(cmd *cobra.Command, args []string) {
Expand All @@ -289,7 +289,7 @@ func rollbackAppVersion(cmd *cobra.Command, args []string) {
}
update := marathon.NewApplication(args[0]).RollbackVersion(version)
v, e := client(cmd).UpdateApplication(update, wait)
cli.Output(Application{v}, e)
cli.Output(templateFor(T_APPLICATIONS, v), e)
}

func convertFile(cmd *cobra.Command, args []string) {
Expand Down
3 changes: 1 addition & 2 deletions commands/marathon/app_log_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
const (
STDERR_FLAG = "stderr"
FOLLOW_FLAG = "follow"
POLL_FLAG = "poll"

POLL_FLAG = "poll"
)

var logCmd = &cobra.Command{
Expand Down
Loading

0 comments on commit d8af997

Please sign in to comment.