Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #34 from gondor/master
Browse files Browse the repository at this point in the history
cleanup and add isEnv option in templates
  • Loading branch information
gondor committed Jun 28, 2016
2 parents 46f18a5 + b6a0778 commit ca72504
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion commands/marathon/deploy_cmds.go
Expand Up @@ -78,6 +78,7 @@ the other for delegation to the origin (app or group)

func init() {

deployCreateCmd.Flags().BoolP(WAIT_FLAG, "w", false, "Wait for group to become healthy")
deployCreateCmd.Flags().String(TEMPLATE_CTX_FLAG, "", "Provides data per environment in JSON form to do a first pass parse of descriptor as template")
deployCreateCmd.Flags().BoolP(FORCE_FLAG, "f", false, "Force deployment (updates application if it already exists)")
deployCreateCmd.Flags().Bool(STOP_DEPLOYS_FLAG, false, "Stop an existing deployment for this app (if exists) and use this revision")
Expand Down Expand Up @@ -110,7 +111,6 @@ func deployAppOrGroup(cmd *cobra.Command, args []string) {
options := &marathon.CreateOptions{Wait: wait, Force: force, ErrorOnMissingParams: !ignore, StopDeploy: stop_deploy}

descriptor := parseDescriptor(tempctx, filename)

et, err := encoding.NewEncoderFromFileExt(filename)
if err != nil {
exitWithError(err)
Expand Down
21 changes: 19 additions & 2 deletions commands/marathon/templatectx.go
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/ContainX/depcon/pkg/encoding"
"github.com/spf13/viper"
"strings"
"path/filepath"
)

const (
Expand Down Expand Up @@ -43,6 +45,13 @@ var Funcs = template.FuncMap{

return value
},
"isEnv": func(value string) bool {
if len(value) > 0 {
current := strings.ToLower(viper.GetString(ENV_NAME))
return current == strings.ToLower(value)
}
return false
},
}

type TemplateContext struct {
Expand All @@ -60,13 +69,21 @@ func (ctx *TemplateContext) Transform(writer io.Writer, descriptor string) error
return err
} else {
var e error
t = template.New("output").Funcs(Funcs)
t = template.New(descriptor).Funcs(Funcs)
t, e = t.Parse(string(b))
if e != nil {
return e
}
if matches, err := filepath.Glob("./**/*.tmpl"); err == nil && len(matches) > 0 {
if t, e = t.ParseFiles(matches...); err != nil {
return err
}
}
}
if err := t.Execute(writer, ctx.mergeAppWithDefault(string(viper.GetString(ENV_NAME)))); err != nil {
environment := viper.GetString(ENV_NAME)
m := ctx.mergeAppWithDefault(environment)

if err := t.Execute(writer, m); err != nil {
return err
}
return nil
Expand Down

0 comments on commit ca72504

Please sign in to comment.