Skip to content

Commit

Permalink
fix template-context issue when non-existant - release 0.9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gondor committed Mar 27, 2017
1 parent 755855b commit e7cb75b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
27 changes: 8 additions & 19 deletions commands/marathon/deploy_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/ContainX/depcon/pkg/cli"
"github.com/ContainX/depcon/pkg/encoding"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -177,25 +176,15 @@ func outputDeployment(result interface{}, e error) {
}

func parseDescriptor(tempctx, filename string) string {
if TemplateExists(tempctx) {
b := &bytes.Buffer{}
b := &bytes.Buffer{}

r, err := LoadTemplateContext(tempctx)
if err != nil {
exitWithError(err)
}

if err := r.Transform(b, filename); err != nil {
exitWithError(err)
}
return b.String()
} else {
if b, err := ioutil.ReadFile(filename); err != nil {
exitWithError(err)
} else {
return string(b)
r, err := LoadTemplateContext(tempctx)
if err != nil {
exitWithError(err)
}

}
if err := r.Transform(b, filename); err != nil {
exitWithError(err)
}
return ""
return b.String()
}
14 changes: 11 additions & 3 deletions commands/marathon/templatectx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"os"
"text/template"

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

const (
DefaultEnv = "-"
DefaultEnv = "-"
ContextErrFmt = "Error parsing template context: %s - %s"
)

// Templated based Functions
Expand Down Expand Up @@ -108,20 +110,26 @@ func TemplateExists(filename string) bool {
}

func LoadTemplateContext(filename string) (*TemplateContext, error) {
// Return empty context if non-exists
if !TemplateExists(filename) {
fmt.Println("Ignoring context file (not found) - template-context.json")
return &TemplateContext{Environments: make(map[string]*TemplateEnvironment)}, nil
}

ctx, err := os.Open(filename)
if err != nil {
return nil, err
}

encoder, err := encoding.NewEncoder(encoding.JSON)
if err != nil {
return nil, err
return nil, fmt.Errorf(ContextErrFmt, filename, err.Error())
}

result := &TemplateContext{Environments: make(map[string]*TemplateEnvironment)}

if err := encoder.UnMarshal(ctx, result); err != nil {
return nil, err
return nil, fmt.Errorf(ContextErrFmt, filename, err.Error())
}
return result, nil
}

0 comments on commit e7cb75b

Please sign in to comment.