Fusozay Var Var: A CLI tool for quick text template rendering
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.8 KiB

package cmd
import (
homedir "github.com/mitchellh/go-homedir"
const templateGlob = "*.tmpl"
var cfgFile string
var rootCmd = &cobra.Command{
Use: "fvv",
Args: cobra.ExactArgs(1),
Short: "Quickly render go templates to stdout",
Long: `Quick Usage: fvv <template definition name>
Fusozay Var Var is a CLI application for quickly rendering out text templates.
I often write outfit and character descriptions that reuses a lot of elements.
This allows me to DRY up my descriptions and still quickly get results.
Template requirements:
- All of the templates must be valid golang templates.
- They must not require any variables to be passed in.
- All templates must have the '.tmpl' extension. All other files ignored.
- The template you want to render must be "named"
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) { renderTemplate(args[0]) },
func renderTemplate(definition string) {
tmpl, err := template.ParseGlob(templateGlob)
fatalIfErr("ParsingGlob", err)
err = tmpl.ExecuteTemplate(os.Stdout, definition, "no data needed")
fatalIfErr("ExecutingTemplate", err)
func fatalIfErr(helpMessage string, err error) {
if err != nil {
l := log.New(os.Stderr, "", 0)
l.Fatal(helpMessage, ": ", err)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.fvv.toml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
// Search config in home directory with name ".fvv" (without extension).
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())