代码拉取完成,页面将自动刷新
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/pkg/errors"
)
const defaultGlob = "module/*/_meta/config*.yml.tpl"
var (
goos = flag.String("os", runtime.GOOS, "generate config specific to the specified operating system")
reference = flag.Bool("ref", false, "generate a reference config")
concat = flag.Bool("concat", false, "concatenate all configs instead writing individual files")
)
func findConfigFiles(globs []string) ([]string, error) {
var configFiles []string
for _, glob := range globs {
files, err := filepath.Glob(glob)
if err != nil {
return nil, errors.Wrapf(err, "failed on glob %v", glob)
}
configFiles = append(configFiles, files...)
}
return configFiles, nil
}
func getConfig(file string) ([]byte, error) {
tpl, err := template.ParseFiles(file)
if err != nil {
return nil, errors.Wrapf(err, "failed reading %v", file)
}
data := map[string]interface{}{
"goos": *goos,
"reference": *reference,
}
buf := new(bytes.Buffer)
if err = tpl.Execute(buf, data); err != nil {
return nil, errors.Wrapf(err, "failed executing template %v", file)
}
return buf.Bytes(), nil
}
func output(content []byte, file string) error {
if file == "-" {
fmt.Println(string(content))
return nil
}
if err := ioutil.WriteFile(file, content, 0640); err != nil {
return errors.Wrapf(err, "failed writing output to %v", file)
}
return nil
}
func logAndExit(err error) {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
func main() {
flag.Parse()
globs := os.Args
if len(os.Args) > 0 {
path, err := filepath.Abs(defaultGlob)
if err != nil {
logAndExit(err)
}
globs = []string{path}
}
files, err := findConfigFiles(globs)
if err != nil {
logAndExit(err)
}
var segments [][]byte
for _, file := range files {
segment, err := getConfig(file)
if err != nil {
logAndExit(err)
}
if *concat {
segments = append(segments, segment)
} else {
output(segment, strings.TrimSuffix(file, ".tpl"))
}
}
if *concat {
if err := output(bytes.Join(segments, []byte{'\n'}), "-"); err != nil {
logAndExit(err)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。