Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
shiguanghuxian committed Jan 17, 2019
2 parents 7ffe9c2 + 8003f03 commit 0f754ff
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bin/config/cfg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ key_file = "key_file"
[[server]]
# 显示名称
title = "make docker_cluster_run"
# 标识名
# 标识名 - 只能是字母数字或下划线
name = "docker_cluster_run"
# etcd连接地址 如果为集群请填写全部地址
address = ["etcd0:2379","etcd1:2379","etcd2:2379"]
Expand Down
18 changes: 18 additions & 0 deletions program/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"errors"
"os"
"regexp"

"github.com/naoina/toml"
"github.com/shiguanghuxian/etcd-manage/program/common"
Expand Down Expand Up @@ -110,6 +112,13 @@ func LoadConfig(cfgPath string) (*Config, error) {
return nil, err
}

// 验证服务name是否非字母和数字
for _, v := range cfg.Server {
if chackEtcdServerName(v.Name) == false {
return nil, errors.New("etcd server name can only be letters or numbers or '_'")
}
}

return cfg, nil
}

Expand All @@ -119,3 +128,12 @@ func getCfgPath(cfgPath string) string {
}
return cfgPath
}

// 判断etcd服务名是否包含非字母和数字
func chackEtcdServerName(name string) bool {
if name == "" {
return false
}
reg := regexp.MustCompile("[^0-9A-Za-z_]+")
return !reg.MatchString(name)
}
15 changes: 13 additions & 2 deletions program/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package logger

import (
"net/url"
"os"
"runtime"
"strings"
"time"

Expand All @@ -20,7 +22,7 @@ func InitLogger(logPath string, isDebug bool) (*zap.SugaredLogger, error) {
infoLogPath := ""
// errorLogPath := ""
if logPath == "" {
logRoot := common.GetRootDir() + "logs/"
logRoot := common.GetRootDir() + "logs" + string(os.PathSeparator)
if isExt, _ := common.PathExists(logRoot); isExt == false {
os.MkdirAll(logRoot, os.ModePerm)
}
Expand All @@ -32,6 +34,11 @@ func InitLogger(logPath string, isDebug bool) (*zap.SugaredLogger, error) {
// errorLogPath = logPath + string(os.PathSeparator) + time.Now().Format("20060102_error") + ".log"
}

// 兼容win根完整路径问题
zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})

cfg := &zap.Config{
Encoding: "json",
}
Expand All @@ -43,7 +50,11 @@ func InitLogger(logPath string, isDebug bool) (*zap.SugaredLogger, error) {
// cfg.ErrorOutputPaths = []string{"stdout"}
} else {
atom.SetLevel(zapcore.InfoLevel)
cfg.OutputPaths = []string{infoLogPath}
if runtime.GOOS == "windows" {
cfg.OutputPaths = []string{"winfile:///" + infoLogPath}
} else {
cfg.OutputPaths = []string{infoLogPath}
}
// cfg.ErrorOutputPaths = []string{errorLogPath}
}
cfg.Level = atom
Expand Down
26 changes: 19 additions & 7 deletions program/program.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package program

import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"runtime"
"time"

"github.com/shiguanghuxian/etcd-manage/program/config"
Expand All @@ -31,8 +31,8 @@ func New() (*Program, error) {
return nil, err
}

jj, _ := json.Marshal(cfg)
fmt.Println(string(jj))
// jj, _ := json.Marshal(cfg)
// fmt.Println(string(jj))

return &Program{
cfg: cfg,
Expand All @@ -47,8 +47,7 @@ func (p *Program) Run() error {
// 打开浏览器
go func() {
time.Sleep(100 * time.Millisecond)
cmd := exec.Command("open", fmt.Sprintf("http://127.0.0.1:%d/ui/", p.cfg.HTTP.Port))
cmd.Run()
openURL(fmt.Sprintf("http://127.0.0.1:%d/ui/", p.cfg.HTTP.Port))
}()

return nil
Expand All @@ -61,5 +60,18 @@ func (p *Program) Stop() {
}
}

// 重新加载配置文件
// 使用go-cache
// 打开url
func openURL(urlAddr string) {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd", " /c start "+urlAddr)
} else if runtime.GOOS == "darwin" {
cmd = exec.Command("open", urlAddr)
} else {
return
}
err := cmd.Start()
if err != nil {
logger.Log.Errorw("打开浏览器错误", "err", err)
}
}

0 comments on commit 0f754ff

Please sign in to comment.