Skip to content

Commit

Permalink
feat: add support for rendering folder & subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
kha7iq committed Jul 7, 2021
1 parent aa20b8e commit eba06e0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
12 changes: 6 additions & 6 deletions cmd/dir/dir.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package file
package dir

import (
"os"
Expand All @@ -20,14 +20,14 @@ func Render() *cli.Command {
return &cli.Command{
Name: "dir",
Aliases: []string{"d"},
Usage: "input from a file",
Usage: "Directory lets you render all files in a folder & sub folder.",
Flags: []cli.Flag{
&cli.StringFlag{
Destination: &subVarsOpts.Path,
Name: "name",
Aliases: []string{"n"},
Usage: "Name of directory containing template files ",
EnvVars: []string{"SUBVARS_DIR_NAME"},
Name: "path",
Aliases: []string{"p"},
Usage: "Path of folder containing template file.s",
EnvVars: []string{"SUBVARS_DIR_PATH"},
},
},
Action: func(ctx *cli.Context) error {
Expand Down
48 changes: 24 additions & 24 deletions cmd/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,18 @@ import (
"text/template"

"github.com/Masterminds/sprig"
"github.com/urfave/cli/v2"
)

// Flags struct holds inputs values for global flags
type Flags struct {
Prefix string
MissingKey string
}

var (
GlobalOpts Flags

GlobalFlags = []cli.Flag{
&cli.StringFlag{
Name: "prefix, pr",
Usage: "Match only variables with given prefix pattern",
Destination: &GlobalOpts.Prefix,
EnvVars: []string{"SUBVARS_PREFIX_PATTERN"},
},
&cli.StringFlag{
Destination: &GlobalOpts.MissingKey,
Name: "missingkey",
Usage: "Behavior for missing key when parsing variables",
EnvVars: []string{"SUBVARS_MISSINGKEY"},
Value: "default",
},
}
)
var GlobalOpts Flags

// GetVars will get all the environment variables
func GetVars() (enVars map[string]interface{}) {
enVars = make(map[string]interface{})
for _, value := range os.Environ() {
Expand All @@ -44,16 +28,13 @@ func GetVars() (enVars map[string]interface{}) {
return
}

// ParseString will parse any input provided as string
func ParseString(str string) (*template.Template, error) {
funcMap := sprig.TxtFuncMap()
return template.Must(template.New("").Funcs(funcMap).Parse(str)), nil
}

func ParseFiles(files ...string) (*template.Template, error) {
funcMap := sprig.TxtFuncMap()
return template.Must(template.New(filepath.Base(files[0])).Funcs(funcMap).ParseFiles(files...)), nil
}

// MatchPrefix will match a given prefix pattern of all env variables and render only those.
func MatchPrefix(prefix string) map[string]string {
enVars := make(map[string]string)
for _, value := range os.Environ() {
Expand All @@ -64,3 +45,22 @@ func MatchPrefix(prefix string) map[string]string {
}
return enVars
}

// GetPathInDir Recursively get all file paths in directory, including sub-directories.
func GetPathInDir(dirpath string) ([]string, error) {
var paths []string
err := filepath.Walk(dirpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
paths = append(paths, path)
}
return nil
})
if err != nil {
return nil, err
}

return paths, nil
}
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"os"

"github.com/kha7iq/subvars/cmd/file"
"github.com/kha7iq/subvars/cmd/dir"
"github.com/kha7iq/subvars/cmd/helpers"

"github.com/urfave/cli/v2"
Expand All @@ -17,12 +17,28 @@ var version string
func main() {
app := cli.NewApp()
app.Commands = []*cli.Command{
file.Render(),
dir.Render(),
}
app.Name = "subvars"
app.Version = version
app.Usage = "Substitute environment variables defined as go templates."
app.Flags = append([]cli.Flag{}, helpers.GlobalFlags...)
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "prefix, pr",
Usage: "Match only variables with given prefix pattern",
Destination: &helpers.GlobalOpts.Prefix,
EnvVars: []string{"SUBVARS_PREFIX"},
},
&cli.StringFlag{
Destination: &helpers.GlobalOpts.MissingKey,
Name: "missingkey",
Usage: "Behavior for missing key when parsing variables." +
"Available options 'invalid', 'error' or 'zero'",
EnvVars: []string{"SUBVARS_MISSINGKEY"},
Value: "invalid",
},
}

app.Action = func(c *cli.Context) error {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
Expand Down

0 comments on commit eba06e0

Please sign in to comment.