From 7ac63613fb64c6cec3119afc51ba4bac91b81a94 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 8 Feb 2017 12:58:53 +0100 Subject: feat main: Add proper CLI support Adds a basic CLI structure with a single "run" command that takes a --file (-f) and --limit (-l) flag. --limit can be used to only output certain resource sets. Closes #4 --- main.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 19 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 158c9a566a..de734e8ac3 100644 --- a/main.go +++ b/main.go @@ -4,36 +4,70 @@ import ( "fmt" "os" + "github.com/polydawn/meep" "github.com/tazjin/kontemplate/context" "github.com/tazjin/kontemplate/templater" + "github.com/urfave/cli" ) func main() { - args := os.Args[1:] - if len(args) == 0 { - fmt.Fprintln(os.Stderr, "Usage: kontemplate ") - os.Exit(1) - } + app := cli.NewApp() - c, err := context.LoadContextFromFile(os.Args[1]) + app.Name = "kontemplate" + app.Usage = "simple Kubernetes resource templating" + app.Version = "0.0.1" - if err != nil { - fmt.Fprintf(os.Stderr, "%v\n", err) - os.Exit(1) + app.Commands = []cli.Command{ + ApplyCommand(), } - fmt.Fprintf(os.Stderr, "Applying cluster %s\n", c.Name) + app.Run(os.Args) +} + +func ApplyCommand() cli.Command { + return cli.Command{ + Name: "run", + Usage: "Interpolate and print templates", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "file, f", + Usage: "Cluster configuration file to use", + }, + cli.StringSliceFlag{ + Name: "limit, l", + Usage: "Limit templating to certain resource sets", + }, + }, + Action: func(c *cli.Context) error { + limit := c.StringSlice("limit") + f := c.String("file") + + if f == "" { + return meep.New( + &meep.ErrInvalidParam{ + Param: "file", + Reason: "Cluster config file must be specified", + }, + ) + } + + ctx, err := context.LoadContextFromFile(f) + + if err != nil { + return err + } + + resources, err := templater.LoadAndPrepareTemplates(&limit, ctx) - for _, rs := range c.ResourceSets { - fmt.Fprintf(os.Stderr, "Applying resource %s with values %v\n", rs.Name, rs.Values) - resources, err := templater.LoadAndPrepareTemplates(c) + if err != nil { + return err + } - if err != nil { - fmt.Println(err) - } + for _, r := range resources { + fmt.Println(r) + } - for _, r := range resources { - fmt.Print(r) - } + return nil + }, } } -- cgit 1.4.1