Skip to content

Commit

Permalink
support init command
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang committed Jul 26, 2019
1 parent cd74cb7 commit d88b436
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 56 deletions.
1 change: 1 addition & 0 deletions .dotam/RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{version}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/
Dotamfile.demo.*
25 changes: 17 additions & 8 deletions Dotamfile.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,36 @@ 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"
}

var "versions" {
prod = "v0.1.1"
release = "v0.1.3-beta"
}

arg "reg_user" {
Expand Down
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ install:
test:
go test -v -count=1

.PHONY: run-dev
run-dev:
LOG_LEVEL=debug go run . build
.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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Dotam 读法 dot-am, 原意为dotfiles automation。
- [运行](#运行)
- [初始化](#初始化)
- [注意及常见问题](#注意及常见问题)
- [获取支持](#获取支持)
- [语法冲突](#语法冲突)

<!-- /TOC -->
Expand Down Expand Up @@ -174,6 +175,13 @@ make install

## 注意及常见问题


### 获取支持

* 你可以随时发送邮件到[email protected]来获取支持,我会在检查邮件时尽快回复。
* 你也可以通过邮件来索要我的个人微信来获取即时支持。


### 语法冲突

如果模板中还有一些用于其他工具的模板标记比如我们的`.drone.yml`中用于slack插件的`{{#success}}`语法,这个会和项目自带的
Expand Down
1 change: 1 addition & 0 deletions RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v0.1.3-beta
3 changes: 3 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
27 changes: 27 additions & 0 deletions demohcl.go
Original file line number Diff line number Diff line change
@@ -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"
}
`)
31 changes: 31 additions & 0 deletions demojson.go
Original file line number Diff line number Diff line change
@@ -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",
}
}
}
`)
23 changes: 23 additions & 0 deletions demoyaml.go
Original file line number Diff line number Diff line change
@@ -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"
`)
6 changes: 5 additions & 1 deletion example/.dotam/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ test:

.PHONY: run-dev
run-dev:
LOG_LEVEL=debug go run . build
LOG_LEVEL=debug go run . build

.PHONY: run-dev-init
run-dev-init:
LOG_LEVEL=debug go run . init
83 changes: 42 additions & 41 deletions init.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
)

var CWD string
var RELEASE_VERSION []byte

func init() {
RELEASE_VERSION = ReadFile("RELEASE")
CWD, _ = os.Getwd()
initLogLevel()
}
Expand All @@ -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)
}

Expand All @@ -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{
Expand Down

0 comments on commit d88b436

Please sign in to comment.