summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-02-18 23:25:43 -0500
committerDave Henderson <dhenderson@gmail.com>2018-04-18 15:23:49 -0400
commit0ff06aa75c6a3062852f08c0ae73751530ede6c3 (patch)
tree01002e4c015ee950f3b63c6584ebb8462eaf9b87 /cmd
parentcd60ef2d97a45135f3b4127ea0038db5a4ffe847 (diff)
Putting main pkg in cmd subdirectory
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/gomplate/main.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/cmd/gomplate/main.go b/cmd/gomplate/main.go
new file mode 100644
index 00000000..256bed1f
--- /dev/null
+++ b/cmd/gomplate/main.go
@@ -0,0 +1,91 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "os"
+
+ "github.com/hairyhenderson/gomplate"
+ "github.com/hairyhenderson/gomplate/env"
+ "github.com/hairyhenderson/gomplate/version"
+ "github.com/spf13/cobra"
+)
+
+var (
+ printVer bool
+ opts gomplate.Config
+)
+
+func validateOpts(cmd *cobra.Command, args []string) error {
+ if cmd.Flag("in").Changed && cmd.Flag("file").Changed {
+ return errors.New("--in and --file may not be used together")
+ }
+
+ if len(opts.InputFiles) != len(opts.OutputFiles) {
+ return fmt.Errorf("Must provide same number of --out (%d) as --file (%d) options", len(opts.OutputFiles), len(opts.InputFiles))
+ }
+
+ if cmd.Flag("input-dir").Changed && (cmd.Flag("in").Changed || cmd.Flag("file").Changed) {
+ return errors.New("--input-dir can not be used together with --in or --file")
+ }
+
+ if cmd.Flag("output-dir").Changed {
+ if cmd.Flag("out").Changed {
+ return errors.New("--output-dir can not be used together with --out")
+ }
+ if !cmd.Flag("input-dir").Changed {
+ return errors.New("--input-dir must be set when --output-dir is set")
+ }
+ }
+ return nil
+}
+
+func printVersion(name string) {
+ // fmt.Printf("%s version %s, build %s\n", name, version.Version, version.GitCommit)
+ fmt.Printf("%s version %s\n", name, version.Version)
+}
+
+func newGomplateCmd() *cobra.Command {
+ rootCmd := &cobra.Command{
+ Use: "gomplate",
+ Short: "Process text files with Go templates",
+ PreRunE: validateOpts,
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if printVer {
+ printVersion(cmd.Name())
+ return nil
+ }
+ return gomplate.RunTemplates(&opts)
+ },
+ Args: cobra.NoArgs,
+ }
+ return rootCmd
+}
+
+func initFlags(command *cobra.Command) {
+ command.Flags().BoolVarP(&printVer, "version", "v", false, "print the version")
+
+ command.Flags().StringArrayVarP(&opts.InputFiles, "file", "f", []string{"-"}, "Template `file` to process. Omit to use standard input, or use --in or --input-dir")
+ command.Flags().StringVarP(&opts.Input, "in", "i", "", "Template `string` to process (alternative to --file and --input-dir)")
+ command.Flags().StringVar(&opts.InputDir, "input-dir", "", "`directory` which is examined recursively for templates (alternative to --file and --in)")
+ command.Flags().StringArrayVar(&opts.ExcludeGlob, "exclude", []string{}, "glob of files to not parse")
+ command.Flags().StringArrayVarP(&opts.OutputFiles, "out", "o", []string{"-"}, "output `file` name. Omit to use standard output.")
+ command.Flags().StringVar(&opts.OutputDir, "output-dir", ".", "`directory` to store the processed templates. Only used for --input-dir")
+
+ command.Flags().StringArrayVarP(&opts.DataSources, "datasource", "d", nil, "`datasource` in alias=URL form. Specify multiple times to add multiple sources.")
+ command.Flags().StringArrayVarP(&opts.DataSourceHeaders, "datasource-header", "H", nil, "HTTP `header` field in 'alias=Name: value' form to be provided on HTTP-based data sources. Multiples can be set.")
+
+ ldDefault := env.Getenv("GOMPLATE_LEFT_DELIM", "{{")
+ rdDefault := env.Getenv("GOMPLATE_RIGHT_DELIM", "}}")
+ command.Flags().StringVar(&opts.LDelim, "left-delim", ldDefault, "override the default left-`delimiter` [$GOMPLATE_LEFT_DELIM]")
+ command.Flags().StringVar(&opts.RDelim, "right-delim", rdDefault, "override the default right-`delimiter` [$GOMPLATE_RIGHT_DELIM]")
+}
+
+func main() {
+ command := newGomplateCmd()
+ initFlags(command)
+ if err := command.Execute(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+}