代码格式化

pull/21/merge
ouqiang 2017-04-02 10:19:52 +08:00
parent 7e5b84c320
commit 16582a5775
18 changed files with 272 additions and 286 deletions

View File

@ -1,17 +1,18 @@
package cmd
import (
"github.com/urfave/cli"
"gopkg.in/macaron.v1"
"github.com/go-macaron/csrf"
"github.com/go-macaron/gzip"
"github.com/go-macaron/session"
"github.com/go-macaron/csrf"
"github.com/ouqiang/cron-scheduler/modules/app"
"github.com/ouqiang/cron-scheduler/routers"
"github.com/urfave/cli"
"gopkg.in/macaron.v1"
)
// web服务器默认端口
const DefaultPort = 5920
// 静态文件目录
const StaticDir = "public"
@ -64,7 +65,7 @@ func registerMiddleware(m *macaron.Macaron) {
// 解析端口
func parsePort(ctx *cli.Context) int {
var port int
if (ctx.IsSet("port")) {
if ctx.IsSet("port") {
port = ctx.Int("port")
}
if port <= 0 || port >= 65535 {

View File

@ -1,6 +1,5 @@
package models
// 主机
type Host struct {
Id int16 `xorm:"smallint pk autoincr"`
@ -15,7 +14,7 @@ type Host struct {
PageSize int `xorm:"-"`
}
type LoginType int8;
type LoginType int8
const (
PublicKey = 1
@ -55,10 +54,10 @@ func(host *Host) Total() (int64, error) {
}
func (host *Host) parsePageAndPageSize() {
if (host.Page <= 0) {
if host.Page <= 0 {
host.Page = Page
}
if (host.PageSize >= 0 || host.PageSize > MaxPageSize) {
if host.PageSize >= 0 || host.PageSize > MaxPageSize {
host.PageSize = PageSize
}
}

View File

@ -13,7 +13,7 @@ func(migration *Migration) Exec(dbName string) error {
tables := []interface{}{
&User{}, &Task{}, &TaskLog{}, &Host{},
}
for _, table := range(tables) {
for _, table := range tables {
err := Db.Sync2(table)
if err != nil {
return err

View File

@ -1,11 +1,11 @@
package models
import (
"github.com/go-xorm/xorm"
"fmt"
"github.com/ouqiang/cron-scheduler/modules/setting"
"github.com/go-xorm/core"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"github.com/ouqiang/cron-scheduler/modules/setting"
"gopkg.in/macaron.v1"
"strings"
)
@ -70,7 +70,7 @@ func getDbEngineDSN(engine string, config map[string]string) string {
}
// 获取数据库配置
func getDbConfig(configFile string) (map[string]string){
func getDbConfig(configFile string) map[string]string {
config, err := setting.Read(configFile)
if err != nil {
panic(err)

View File

@ -91,10 +91,10 @@ func(taskLog *TaskLog) Total() (int64, error) {
}
func (taskLog *TaskLog) parsePageAndPageSize() {
if (taskLog.Page <= 0) {
if taskLog.Page <= 0 {
taskLog.Page = Page
}
if (taskLog.PageSize >= 0 || taskLog.PageSize > MaxPageSize) {
if taskLog.PageSize >= 0 || taskLog.PageSize > MaxPageSize {
taskLog.PageSize = PageSize
}
}

View File

@ -49,10 +49,10 @@ func(task *Task) Total() (int64, error) {
}
func (task *Task) parsePageAndPageSize() {
if (task.Page <= 0) {
if task.Page <= 0 {
task.Page = Page
}
if (task.PageSize >= 0 || task.PageSize > MaxPageSize) {
if task.PageSize >= 0 || task.PageSize > MaxPageSize {
task.PageSize = PageSize
}
}

View File

@ -1,11 +1,11 @@
package models
import (
"time"
"github.com/ouqiang/cron-scheduler/modules/utils"
"time"
)
const PasswordSaltLength = 6;
const PasswordSaltLength = 6
// 用户model
type User struct {
@ -65,7 +65,7 @@ func(user *User) Match(username, password string) bool {
return false
}
hashPassword := user.encryptPassword(password, user.Salt)
if (hashPassword != user.Password) {
if hashPassword != user.Password {
return false
}
@ -82,7 +82,6 @@ func(user *User) EmailExists(email string) (int64, error) {
return Db.Where("email = ?", email).Count(user)
}
func (user *User) List() ([]User, error) {
user.parsePageAndPageSize()
list := make([]User, 0)
@ -96,10 +95,10 @@ func(user *User) Total() (int64, error) {
}
func (user *User) parsePageAndPageSize() {
if (user.Page <= 0) {
if user.Page <= 0 {
user.Page = Page
}
if (user.PageSize >= 0 || user.PageSize > MaxPageSize) {
if user.PageSize >= 0 || user.PageSize > MaxPageSize {
user.PageSize = PageSize
}
}

View File

@ -7,8 +7,6 @@ import (
"github.com/ouqiang/cron-scheduler/modules/utils"
)
/**
* ad-hoc
* hosts

View File

@ -1,12 +1,12 @@
package ansible
import (
"github.com/ouqiang/cron-scheduler/models"
"sync"
"io/ioutil"
"bytes"
"strconv"
"github.com/ouqiang/cron-scheduler/models"
"github.com/ouqiang/cron-scheduler/modules/utils"
"io/ioutil"
"strconv"
"sync"
)
// 主机名
@ -32,7 +32,6 @@ func(h *Hosts) GetFilename() string {
return h.filename
}
// 写入hosts
func (h *Hosts) Write() {
host := new(models.Host)
@ -46,7 +45,7 @@ func(h *Hosts) Write() {
return
}
buffer := bytes.Buffer{}
for _, hostModel := range(hostModels) {
for _, hostModel := range hostModels {
buffer.WriteString(strconv.Itoa(int(hostModel.Id)))
buffer.WriteString(" ansible_ssh_host=")
buffer.WriteString(hostModel.Name)
@ -54,7 +53,7 @@ func(h *Hosts) Write() {
buffer.WriteString(strconv.Itoa(hostModel.Port))
buffer.WriteString(" ansible_ssh_user=")
buffer.WriteString(hostModel.Username)
if (hostModel.LoginType != models.PublicKey && hostModel.Password != "") {
if hostModel.LoginType != models.PublicKey && hostModel.Password != "" {
buffer.WriteString(" ansible_ssh_pass=")
buffer.WriteString(hostModel.Password)
}
@ -66,5 +65,3 @@ func(h *Hosts) Write() {
return
}

View File

@ -4,10 +4,10 @@ import (
"os"
"runtime"
"github.com/ouqiang/cron-scheduler/modules/utils"
"github.com/ouqiang/cron-scheduler/modules/crontask"
"github.com/ouqiang/cron-scheduler/models"
"github.com/ouqiang/cron-scheduler/modules/ansible"
"github.com/ouqiang/cron-scheduler/modules/crontask"
"github.com/ouqiang/cron-scheduler/modules/utils"
"github.com/ouqiang/cron-scheduler/service"
)
@ -79,7 +79,6 @@ func CreateInstallLock() error {
return err
}
// 初始化资源
func InitResource() {
// 初始化ansible Hosts
@ -97,7 +96,7 @@ func InitDb() {
// 检测目录是否存在
func checkDirExists(path ...string) {
for _, value := range(path) {
for _, value := range path {
_, err := os.Stat(value)
if os.IsNotExist(err) {
panic(value + "目录不存在")

View File

@ -1,10 +1,10 @@
package crontask
import (
"github.com/robfig/cron"
"errors"
"sync"
"github.com/robfig/cron"
"strings"
"sync"
)
var DefaultCronTask *CronTask
@ -39,13 +39,13 @@ func(cronTask *CronTask) Add(name string, spec string, cmd cron.FuncJob ) (err e
defer cronTask.Unlock()
cronTask.tasks[name] = cron.New()
specs := strings.Split(spec, "|||")
for _, item := range(specs) {
for _, item := range specs {
_, err = cron.Parse(item)
if err != nil {
return err
}
}
for _, item := range(specs) {
for _, item := range specs {
err = cronTask.tasks[name].AddFunc(item, cmd)
}
cronTask.tasks[name].Start()
@ -62,7 +62,6 @@ func(cronTask *CronTask) AddOrReplace(name string, spec string, cmd cron.FuncJob
return cronTask.Add(name, spec, cmd)
}
// 判断任务是否存在
func (cronTask *CronTask) IsExist(name string) bool {
cronTask.RLock()

View File

@ -1,8 +1,8 @@
package setting
import (
"gopkg.in/ini.v1"
"errors"
"gopkg.in/ini.v1"
)
// 读取配置
@ -15,15 +15,14 @@ func Read(filename string) (config *ini.File, err error) {
return
}
// 写入配置
func Write(config map[string]map[string]string, filename string) (error) {
func Write(config map[string]map[string]string, filename string) error {
if len(config) == 0 {
return errors.New("参数不能为空")
}
file := ini.Empty()
for sectionName, items := range(config) {
for sectionName, items := range config {
if sectionName == "" {
return errors.New("节名称不能为空")
}
@ -31,7 +30,7 @@ func Write(config map[string]map[string]string, filename string) (error) {
if err != nil {
return err
}
for key, value := range(items) {
for key, value := range items {
_, err = section.NewKey(key, value)
if err != nil {
return err

View File

@ -12,8 +12,8 @@ type response struct {
type Json struct{}
const ResponseSuccess = 0;
const ResponseFailure = 1;
const ResponseSuccess = 0
const ResponseFailure = 1
func (j *Json) Success(message string, data interface{}) string {
return j.response(ResponseSuccess, message, data)
@ -23,7 +23,7 @@ func(j *Json) Failure(code int, message string) string {
return j.response(code, message, nil)
}
func(j *Json) response(code int, message string, data interface{}) (string) {
func (j *Json) response(code int, message string, data interface{}) string {
resp := response{
Code: code,
Message: message,
@ -37,4 +37,3 @@ func(j *Json) response(code int, message string, data interface{}) (string) {
return string(result)
}

View File

@ -1,15 +1,14 @@
package utils
import (
"os/exec"
"math/rand"
"time"
"crypto/md5"
"encoding/hex"
"log"
"math/rand"
"os/exec"
"time"
)
// 执行shell命令
func ExecShell(command string, args ...string) (string, error) {
result, err := exec.Command(command, args...).CombinedOutput()

View File

@ -1,11 +1,11 @@
package install
import (
"gopkg.in/macaron.v1"
"github.com/ouqiang/cron-scheduler/modules/app"
"github.com/ouqiang/cron-scheduler/modules/utils"
"github.com/ouqiang/cron-scheduler/models"
"github.com/ouqiang/cron-scheduler/modules/app"
"github.com/ouqiang/cron-scheduler/modules/setting"
"github.com/ouqiang/cron-scheduler/modules/utils"
"gopkg.in/macaron.v1"
"strconv"
)

View File

@ -1,9 +1,9 @@
package routers
import (
"gopkg.in/macaron.v1"
"github.com/ouqiang/cron-scheduler/routers/install"
"github.com/go-macaron/binding"
"github.com/ouqiang/cron-scheduler/routers/install"
"gopkg.in/macaron.v1"
)
// 路由注册
@ -19,7 +19,7 @@ func Register(m *macaron.Macaron) {
ctx.HTML(500, "error/500")
})
// 首页
m.Get("/", func(ctx *macaron.Context) (string) {
m.Get("/", func(ctx *macaron.Context) string {
return "go home"
})
// 系统安装

View File

@ -1,16 +1,16 @@
package service
import (
"fmt"
"github.com/ouqiang/cron-scheduler/models"
"github.com/ouqiang/cron-scheduler/modules/ansible"
"github.com/ouqiang/cron-scheduler/modules/crontask"
"github.com/ouqiang/cron-scheduler/modules/utils"
"net/http"
"github.com/robfig/cron"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/ouqiang/cron-scheduler/modules/crontask"
"github.com/robfig/cron"
"github.com/ouqiang/cron-scheduler/modules/ansible"
"fmt"
)
type Task struct{}
@ -27,13 +27,11 @@ func(task *Task) Initialize() {
utils.RecordLog("任务列表为空")
return
}
for _, item := range(taskList) {
for _, item := range taskList {
task.Add(item)
}
}
// 添加任务
func (task *Task) Add(taskModel models.Task) {
taskFunc := createHandlerJob(taskModel)
@ -62,7 +60,7 @@ type HTTPHandler struct {}
func (h *HTTPHandler) Run(taskModel models.Task) (result string, err error) {
client := &http.Client{}
if (taskModel.Timeout > 0) {
if taskModel.Timeout > 0 {
client.Timeout = time.Duration(taskModel.Timeout) * time.Second
}
req, err := http.NewRequest("POST", taskModel.Command, nil)
@ -98,7 +96,6 @@ func(ssh *SSHCommandHandler) Run(taskModel models.Task) (string, error) {
return execSSHHandler("shell", taskModel)
}
// SSH-script任务
type SSHScriptHandler struct{}
@ -109,7 +106,7 @@ func(ssh *SSHScriptHandler) Run(taskModel models.Task) (string, error) {
// SSH任务
func execSSHHandler(module string, taskModel models.Task) (string, error) {
var args []string = []string{taskModel.Command}
if (taskModel.Timeout > 0) {
if taskModel.Timeout > 0 {
// -B 异步执行超时时间, -P 轮询时间
args = append(args, "-B", strconv.Itoa(taskModel.Timeout), "-P", "10")
}
@ -146,7 +143,7 @@ func updateTaskLog(taskLogId int, result string, err error) (int64, error) {
return taskLogModel.Update(taskLogId, models.CommonMap{
"status": status,
"result": result,
});
})
}