github.com/goliatone/go-envset@v0.7.0/cmd/envset/template/template.go (about)

     1  package template
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/goliatone/go-envset/pkg/config"
     8  	"github.com/goliatone/go-envset/pkg/envset"
     9  	"github.com/urfave/cli/v2"
    10  )
    11  
    12  //GetCommand exports template command
    13  func GetCommand(cnf *config.Config) *cli.Command {
    14  	return &cli.Command{
    15  		//TODO: This actually should load a template file and resolve it using the context.
    16  		//Default template should generate envset.example
    17  		Name:        "template",
    18  		Usage:       "make a template file from an environment",
    19  		Description: "create a new template or update file to document the variables in your environment",
    20  		Flags: []cli.Flag{
    21  			&cli.BoolFlag{Name: "print", Usage: "only print the contents to stdout, don't write file"},
    22  			&cli.StringFlag{Name: "filename", Usage: "template file `name`", Value: cnf.Template.File},
    23  			&cli.StringFlag{Name: "filepath", Usage: "template file `path`", Value: cnf.Template.Dir},
    24  			&cli.StringFlag{Name: "env-file", Value: cnf.Filename, Usage: "load environment from `FILE`"},
    25  			&cli.BoolFlag{Name: "overwrite", Usage: "overwrite file, this will delete any changes"},
    26  		},
    27  		Action: func(c *cli.Context) error {
    28  			print := c.Bool("print")
    29  			filename := c.String("env-file")
    30  			template := c.String("filename")
    31  			dir := c.String("filepath")
    32  			overwrite := c.Bool("overwrite")
    33  
    34  			dir, err := filepath.Abs(dir)
    35  			if err != nil {
    36  				return err
    37  			}
    38  
    39  			if _, err = os.Stat(dir); os.IsNotExist(err) {
    40  				if err = os.MkdirAll(dir, os.ModePerm); err != nil {
    41  					return err
    42  				}
    43  			}
    44  			//TODO: This should take a a template file which we use to run against our thing
    45  			template = filepath.Join(dir, template)
    46  
    47  			return envset.DocumentTemplate(filename, template, overwrite, print)
    48  		},
    49  	}
    50  }