-
Notifications
You must be signed in to change notification settings - Fork 2
/
dir.go
119 lines (106 loc) · 2.94 KB
/
dir.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package dir
import (
"os"
"path"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/kha7iq/subvars/cmd/assist"
"github.com/urfave/cli/v2"
)
type flagsDir struct {
InputDir string
OutDir string
}
// Render function will render all the template files in any given input folder and saves
// them in target folder.
func Render() *cli.Command {
var subVarsOpts flagsDir
return &cli.Command{
Name: "dir",
Aliases: []string{"d"},
Usage: "Directory lets you render all files in a folder & subfolder.",
Flags: []cli.Flag{
&cli.StringFlag{
Destination: &subVarsOpts.InputDir,
Name: "input",
Aliases: []string{"i"},
Usage: "Path of folder containing template files.",
EnvVars: []string{"SUBVARS_INPUTDIR"},
},
&cli.StringFlag{
Destination: &subVarsOpts.OutDir,
Name: "out",
Aliases: []string{"o"},
Usage: "Output folder path. If folder does not exist it will be created automatically.",
EnvVars: []string{"SUBVARS_OUTDIR"},
},
},
Action: func(ctx *cli.Context) error {
paths, err := getPathInDir(subVarsOpts.InputDir)
if err != nil {
return err
}
for _, v := range paths {
t := parseFile(v)
if assist.IsFlagSet(assist.GlobalFlags.Prefix) {
assist.EnvVariables = assist.MatchPrefix(assist.GlobalFlags.Prefix)
} else {
assist.EnvVariables = assist.GetVars()
}
t = t.Option("missingkey=" + assist.GlobalFlags.MissingKey)
if assist.IsFlagSet(subVarsOpts.OutDir) {
subDir, outfile := path.Split(v)
if err := createDirIfNotExist(subVarsOpts.OutDir + "/" + subDir); err != nil {
return err
}
file, err := os.Create(subVarsOpts.OutDir + "/" + subDir + outfile)
if err != nil {
return err
}
if err := t.Execute(file, assist.EnvVariables); err != nil {
return err
}
file.Close()
} else {
if err := t.Execute(os.Stdout, assist.EnvVariables); err != nil {
return err
}
}
}
return nil
},
}
}
// getPathInDir recursively get all file paths in directory, including sub-directories.
func getPathInDir(pid string) ([]string, error) {
var paths []string
err := filepath.Walk(pid, 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
}
// createDirIfNotExist will check if folder does not exist it will create it.
func createDirIfNotExist(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err = os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
return err
}
return nil
}
// parseFile will parse any input provided as string
func parseFile(file string) *template.Template {
funcMap := sprig.TxtFuncMap()
return template.Must(template.New(filepath.Base(file)).Funcs(funcMap).Funcs(assist.MatchFunc()).ParseFiles(file))
}