about summary refs log tree commit diff
path: root/main.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-02-08T11·58+0100
committerVincent Ambo <tazjin@gmail.com>2017-02-08T11·58+0100
commit7ac63613fb64c6cec3119afc51ba4bac91b81a94 (patch)
tree58927d534621fe94ea882628df372de7901007a5 /main.go
parent8fac7c1a415d6ea5e41c28f4e7ab5c1ef0883997 (diff)
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
Diffstat (limited to 'main.go')
-rw-r--r--main.go72
1 files changed, 53 insertions, 19 deletions
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 <cluster-config>")
-		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
+		},
 	}
 }