diff --git a/.dotam/RELEASE b/.dotam/RELEASE new file mode 100644 index 0000000..c07f30c --- /dev/null +++ b/.dotam/RELEASE @@ -0,0 +1 @@ +{{version}} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 849ddff..5e1dbcd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ dist/ +Dotamfile.demo.* diff --git a/Dotamfile.hcl b/Dotamfile.hcl index e9a53ba..511464c 100644 --- a/Dotamfile.hcl +++ b/Dotamfile.hcl @@ -6,20 +6,28 @@ temp "Tempfile" { } } +temp "RELEASE" { + src = ".dotam/RELEASE" + dest = "." + var { + version = "{{versions.release}}" + } +} + git "dev" { add_type = "u" commit = "" } -docker { - repo = "deoops/dotam" - tag = "{{versions.prod}}" +// docker { +// repo = "deoops/dotam" +// tag = "{{versions.prod}}" - auth { - username = "tom" - password = "pass" - } -} +// auth { +// username = "tom" +// password = "pass" +// } +// } var "data" { temp = "foo" @@ -27,6 +35,7 @@ var "data" { var "versions" { prod = "v0.1.1" + release = "v0.1.3-beta" } arg "reg_user" { diff --git a/Makefile b/Makefile index f6b1d44..6429ddf 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,10 @@ install: test: go test -v -count=1 -.PHONY: run-dev -run-dev: - LOG_LEVEL=debug go run . build \ No newline at end of file +.PHONY: run-dev-build +run-dev-build: + LOG_LEVEL=debug go run . build + +.PHONY: run-dev-init +run-dev-init: + LOG_LEVEL=debug go run . init \ No newline at end of file diff --git a/README.md b/README.md index 36e8543..5f880d2 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Dotam 读法 dot-am, 原意为dotfiles automation。 - [运行](#运行) - [初始化](#初始化) - [注意及常见问题](#注意及常见问题) + - [获取支持](#获取支持) - [语法冲突](#语法冲突) @@ -174,6 +175,13 @@ make install ## 注意及常见问题 + +### 获取支持 + +* 你可以随时发送邮件到techmesh@aliyun.com来获取支持,我会在检查邮件时尽快回复。 +* 你也可以通过邮件来索要我的个人微信来获取即时支持。 + + ### 语法冲突 如果模板中还有一些用于其他工具的模板标记比如我们的`.drone.yml`中用于slack插件的`{{#success}}`语法,这个会和项目自带的 diff --git a/RELEASE b/RELEASE new file mode 100644 index 0000000..f2d1506 --- /dev/null +++ b/RELEASE @@ -0,0 +1 @@ +v0.1.3-beta \ No newline at end of file diff --git a/const.go b/const.go index 7e1f198..7bde7a1 100644 --- a/const.go +++ b/const.go @@ -2,5 +2,8 @@ package main const SETTINGS_PREFIX = "DOTAM_" const DEFAULT_DOTAMFILE = "Dotamfile.hcl" +const DEMO_YAML = "Dotamfile.demo.yml" +const DEMO_JSON = "Dotamfile.demo.json" +const DEMO_HCL = "Dotamfile.demo.hcl" var DEFAULT_DOTAMFILES = [3]string{"Dotamfile.hcl", "Dotamfile.yml", "Dotamfile.json"} diff --git a/demohcl.go b/demohcl.go new file mode 100644 index 0000000..f3ee377 --- /dev/null +++ b/demohcl.go @@ -0,0 +1,27 @@ +package main + +var DemoHcl = string(` +temp "RELEASE" { + src = ".dotam/RELEASE" + dest = "." + var { + version = "{{versions.release}}" + } +} + +docker { + repo = "deoops/dotam" + tag = "{{versions.release}}" + + auth { + username = "tom" + password = "some key takes you home" + } +} + +var "versions" { + prod = "v0.1.1" + release = "v0.1.3-beta" +} + +`) diff --git a/demojson.go b/demojson.go new file mode 100644 index 0000000..2de6a94 --- /dev/null +++ b/demojson.go @@ -0,0 +1,31 @@ +package main + +var DemoJson = string(` +{ + "temp": { + "RELEASE": { + "src": ".dotam/RELEASE", + "dest": ".", + "var": { + "version": "{{versions.release}}" + } + } + }, + "docker": { + "repo": "deoops/dotam", + "tag": "{{versions.release}}", + "auth": { + "username": "tom", + "password": "some key takes you home" + } + }, + "var": { + "versions": { + "prod": "v0.1.0", + "release": "v0.1.3-beta", + } + } + +} + +`) diff --git a/demoyaml.go b/demoyaml.go new file mode 100644 index 0000000..9d33930 --- /dev/null +++ b/demoyaml.go @@ -0,0 +1,23 @@ +package main + +var DemoYaml = string(` +temp: + RELEASE: + src: ".dotam/RELEASE" + dest: "." + var: + version: "{{versions.release}}" + +docker: + repo: "deoops/dotam" + tag: "{{versions.release}}" + auth: + username: tom + password: "some key takes you home" + +var: + versions: + prod: "v0.1.0" + release: "v0.1.3-beta" + +`) diff --git a/example/.dotam/Makefile b/example/.dotam/Makefile index 97c6dfc..0f14a42 100644 --- a/example/.dotam/Makefile +++ b/example/.dotam/Makefile @@ -10,4 +10,8 @@ test: .PHONY: run-dev run-dev: - LOG_LEVEL=debug go run . build \ No newline at end of file + LOG_LEVEL=debug go run . build + +.PHONY: run-dev-init +run-dev-init: + LOG_LEVEL=debug go run . init \ No newline at end of file diff --git a/init.go b/init.go index be2c43e..2a4d5e6 100644 --- a/init.go +++ b/init.go @@ -1,8 +1,8 @@ package main import ( - "github.com/flosch/pongo2" - "github.com/hashicorp/hcl" + "strings" + "github.com/mitchellh/cli" log "github.com/sirupsen/logrus" ) @@ -18,51 +18,52 @@ func (r InitCmd) Help() string { return "this is help message" } -func (r InitCmd) Run(args []string) int { - var dotamFile string - var renderData pongo2.Context +func (r InitCmd) Run(args []string) (exitCode int) { + var destFile string + var destData string + var err error + + defer func() { + if err != nil { + log.Error(err) + exitCode = -1 + return + } + log.Infof("Congratulations! %s generated.", destFile) + }() + log.WithFields(log.Fields{"CMD INIT": "RUN"}).Debug(args) + // gen hcl as default if len(args) == 0 { - dotamFile = Abs("Dotamfile.hcl") + destFile = DEMO_HCL + if err = genDemoFile(DemoHcl, destFile); err != nil { + return + } } else { - dotamFile = Abs(args[0]) - } - - data := ReadFile(dotamFile) - config := DotamConf{} - err := hcl.Decode(&config, string(data)) - if err != nil { - log.Error(err) - } - - if config.Var != nil { - renderData = VarToTplContext(config.Var) - } - - newDotamSrc, err := Render(string(data), renderData) - if err != nil { - log.Error(err) - return -1 + // TODO maybe we need a better parser + switch strings.Join(args, "") { + case "-tyaml", "-tyml": + destFile = DEMO_YAML + destData = DemoYaml + case "-thcl": + destFile = DEMO_HCL + destData = DemoHcl + case "-tjson": + destFile = DEMO_JSON + destData = DemoJson + } + log.Debug(destFile) + genDemoFile(destData, destFile) } - log.Debug(newDotamSrc) - newConfig := DotamConf{} - err = hcl.Decode(&newConfig, newDotamSrc) - if err != nil { - panic(err) - } - - // log.Debug(newConfig.Temp) - if err = RunTasks(newConfig); err != nil { - log.Error(err) - return -1 - } - - log.Info("Congratulations! All works done!") - - return 0 + return } func (r InitCmd) Synopsis() string { - return "initial a demo conf file in current dir" + return string(`initial a demo conf file in current dir, as default it will generate a .hcl file, for specific format use: -t [yaml|yml, json, hcl] + `) +} + +func genDemoFile(data, file string) error { + return WriteFile(data, file) } diff --git a/main.go b/main.go index d66338a..ab3e64d 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,10 @@ import ( ) var CWD string +var RELEASE_VERSION []byte func init() { + RELEASE_VERSION = ReadFile("RELEASE") CWD, _ = os.Getwd() initLogLevel() } @@ -18,10 +20,8 @@ func initLogLevel() { level := os.Getenv("LOG_LEVEL") if level != "debug" { - // fmt.Println("log at error level") log.SetLevel(log.InfoLevel) } else { - // fmt.Println("log at debug level") log.SetLevel(log.DebugLevel) } @@ -34,7 +34,7 @@ func main() { } func initCli() { - c := cli.NewCLI("dotam", "1.0.0-beta") + c := cli.NewCLI("dotam", string(RELEASE_VERSION)) c.Args = os.Args[1:] c.Commands = map[string]cli.CommandFactory{