3 Star 0 Fork 0

mirrors_xalanq/cf-tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gen.go 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
Gaurang Tandon 提交于 2019-06-21 17:09 . Update gen.go
package cmd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
"github.com/xalanq/cf-tool/config"
"github.com/xalanq/cf-tool/util"
)
func parseTemplate(source string, cfg *config.Config) string {
now := time.Now()
source = strings.ReplaceAll(source, "$%U%$", cfg.Username)
source = strings.ReplaceAll(source, "$%Y%$", fmt.Sprintf("%v", now.Year()))
source = strings.ReplaceAll(source, "$%M%$", fmt.Sprintf("%02v", int(now.Month())))
source = strings.ReplaceAll(source, "$%D%$", fmt.Sprintf("%02v", now.Day()))
source = strings.ReplaceAll(source, "$%h%$", fmt.Sprintf("%02v", now.Hour()))
source = strings.ReplaceAll(source, "$%m%$", fmt.Sprintf("%02v", now.Minute()))
source = strings.ReplaceAll(source, "$%s%$", fmt.Sprintf("%02v", now.Second()))
return source
}
// Gen command
func Gen(args map[string]interface{}) error {
cfg := config.New(config.ConfigPath)
if len(cfg.Template) == 0 {
return errors.New("You have to add at least one code template by `cf config add`")
}
var path string
if alias, ok := args["<alias>"].(string); ok {
templates := cfg.TemplateByAlias(alias)
if len(templates) < 1 {
return fmt.Errorf("Cannot find any template with alias %v", alias)
} else if len(templates) == 1 {
path = templates[0].Path
} else {
fmt.Printf("There are multiple templates with alias %v\n", alias)
for i, template := range templates {
fmt.Printf(`%3v: "%v"`, i, template.Path)
fmt.Println()
}
i := util.ChooseIndex(len(templates))
path = templates[i].Path
}
} else {
path = cfg.Template[cfg.Default].Path
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
source := parseTemplate(string(b), cfg)
currentPath, err := os.Getwd()
if err != nil {
return err
}
ext := filepath.Ext(path)
path = filepath.Join(currentPath, filepath.Base(currentPath))
savePath := path + ext
i := 1
for _, err := os.Stat(savePath); err == nil; _, err = os.Stat(savePath) {
tmpPath := fmt.Sprintf("%v%v%v", path, i, ext)
fmt.Printf("%v exists. Rename to %v\n", filepath.Base(savePath), filepath.Base(tmpPath))
savePath = tmpPath
i++
}
err = ioutil.WriteFile(savePath, []byte(source), 0644)
if err == nil {
color.Green("Generated! See %v", filepath.Base(savePath))
}
return err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_xalanq/cf-tool.git
git@gitee.com:mirrors_xalanq/cf-tool.git
mirrors_xalanq
cf-tool
cf-tool
v0.7.0

搜索帮助