Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (74 loc) · 2.14 KB
/
Copy pathmain.go
File metadata and controls
89 lines (74 loc) · 2.14 KB
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
/*
Copyright AppsCode Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/google/uuid"
"github.com/hashicorp/go-getter"
flag "github.com/spf13/pflag"
"sigs.k8s.io/yaml"
)
var (
sessionID = uuid.New().String()
tplFile string
tplPattern string
dataFile string
)
func init() {
flag.StringVar(&tplFile, "template", "", "Path to Go template file (local file or url is accepted)")
flag.StringVar(&tplPattern, "pattern", "", "Pattern to select Go template files")
flag.StringVar(&dataFile, "data", "", "Path to data file in JSON or YAML format (local file or url is accepted)")
}
func main() {
flag.Parse()
opts := func(c *getter.Client) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
c.Pwd = pwd
return nil
}
localDataFile := filepath.Join(os.TempDir(), sessionID, "template-data.txt")
err := getter.GetFile(localDataFile, dataFile, opts)
if err != nil {
panic(err)
}
d, err := os.ReadFile(localDataFile)
if err != nil {
panic(err)
}
var data interface{}
err = yaml.Unmarshal(d, &data)
if err != nil {
panic(err)
}
var tpl *template.Template
if tplPattern != "" {
tpl = template.Must(template.New(filepath.Base(tplFile)).Funcs(sprig.TxtFuncMap()).ParseGlob(tplPattern))
} else {
localTplFile := filepath.Join(os.TempDir(), sessionID, "template.txt")
err := getter.GetFile(localTplFile, tplFile, opts)
if err != nil {
panic(err)
}
tpl = template.Must(template.New(filepath.Base(localTplFile)).Funcs(sprig.TxtFuncMap()).ParseFiles(localTplFile))
}
err = tpl.Execute(os.Stdout, &data)
if err != nil {
panic(err)
}
}