-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
97 lines (87 loc) · 3.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2015 Alex Browne.
// All rights reserved. Use of this source code is
// governed by the MIT license, which can be found
// in the LICENSE file.
// Temple is a command-line tool for managing go templates which
// supports sharing templates between a client and server. It generates
// code which is compatible with gopherjs and can be compiled to
// javascript to run in the browser.
//
// Version 0.1.3
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/albrow/prtty"
"github.com/go-humble/temple/temple"
"github.com/spf13/cobra"
)
const (
version = "temple version 0.1.1"
)
var (
verbose = false
)
// setQuiet effectively causes all loggers to print
// to /dev/null. However, error will still be printed
// out to stderr.
func setQuiet() {
prtty.AllLoggers.SetOutput(ioutil.Discard)
prtty.Error.Output = os.Stderr
}
// setVerbose sets all loggers to print to stdout,
// except for the Error logger, which will print to
// stderr.
func setVerbose() {
prtty.AllLoggers.SetOutput(os.Stdout)
prtty.Error.Output = os.Stderr
}
func main() {
// Define build command
cmdBuild := &cobra.Command{
Use: "build <src> <dest>",
Short: "Compile the templates in the src directory and write generated go code to the dest file.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
prtty.Error.Fatal("temple build requires exactly 2 arguments: the src directory and the dest file.")
}
if verbose {
setVerbose()
} else {
setQuiet()
}
partials := cmd.Flag("partials").Value.String()
layouts := cmd.Flag("layouts").Value.String()
packageName := cmd.Flag("package").Value.String()
if err := temple.Build(args[0], args[1], partials, layouts, packageName); err != nil {
prtty.Error.Fatal(err)
}
},
}
cmdBuild.Flags().String("partials", "", "(optional) The directory to look for partials. Partials are .tmpl files that are associated with layouts and all other templates.")
cmdBuild.Flags().String("layouts", "", "(optional) The directory to look for layouts. Layouts are .tmpl files which have access to partials and are associated with all other templates.")
cmdBuild.Flags().String("package", "", "(optional) The package name for the generated go file. If not provided, the default will be the directory where the go file is created.")
cmdBuild.Flags().BoolVarP(&verbose, "verbose", "v", false, "If set to true, temple will print out information while building.")
// Define version command
cmdVersion := &cobra.Command{
Use: "version",
Short: "Print the current version number.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version)
},
}
// Define the root command
rootCmd := &cobra.Command{
Use: "temple",
Short: "A command line tool for sharing go templates between a client and server.",
Long: `
A command line tool for sharing go templates between a client and server.
Visit https://github.com/albrow/temple for source code, example usage, documentation, and more.`,
}
rootCmd.AddCommand(cmdBuild, cmdVersion)
if err := rootCmd.Execute(); err != nil {
prtty.Error.Fatal(err)
}
}