Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

删除了部分冗余代码 #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 51 additions & 40 deletions models/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
package models

import (
"errors"
"fmt"
"github.com/astaxie/beego/orm"
"reflect"
"strconv"
"strings"

"github.com/astaxie/beego/orm"
)

type TaskServer struct {
Id int
GroupId int
ConnectionType int
ServerName string
ServerAccount string
ServerName string `info:"服务器名不能为空"`
ServerAccount string `info:"登录账户不能为空"`
ServerOuterIp string
ServerIp string
ServerIp string `info:"服务器IP不能为空"`
Port int
Password string
PrivateKeySrc string
Expand All @@ -34,28 +35,43 @@ type TaskServer struct {
Status int
}

func Detection(object interface{}) error {
objectValue := reflect.ValueOf(object)
if objectValue.Kind() == reflect.Ptr {
objectValue = objectValue.Elem()
}
for i := 0; i < objectValue.NumField(); i++ {
fieldValue := objectValue.Field(i)
fieldType := objectValue.Type().Field(i)
jsonTag := fieldType.Tag.Get("info")
if jsonTag == "" {
continue
}
if fieldValue.IsZero() {
return errors.New(jsonTag)
}
}
return nil
}

func (t *TaskServer) TableName() string {
return TableName("task_server")
}

func (t *TaskServer) Update(fields ...string) error {
if t.ServerName == "" {
return fmt.Errorf("服务器名不能为空")
}
if t.ServerIp == "" {
return fmt.Errorf("服务器IP不能为空")
}

if t.ServerAccount == "" {
return fmt.Errorf("登录账户不能为空")
}

if t.Type == 0 && t.Password == "" {
return fmt.Errorf("服务器密码不能为空")
}

if t.Type == 1 && t.PrivateKeySrc == "" {
return fmt.Errorf("私钥不能为空")
err := Detection(t)
if err != nil {
return fmt.Errorf(err.Error())
}
if t.Password == "" || t.PrivateKeySrc == "" {
switch t.Type {
case 0:
return errors.New("服务器密码不能为空")
case 1:
return errors.New("私钥不能为空")
default:
return errors.New("类型不在规范之内")
}
}

if _, err := orm.NewOrm().Update(t, fields...); err != nil {
Expand All @@ -65,23 +81,19 @@ func (t *TaskServer) Update(fields ...string) error {
}

func TaskServerAdd(obj *TaskServer) (int64, error) {
if obj.ServerName == "" {
return 0, fmt.Errorf("服务器名不能为空")
}
if obj.ServerIp == "" {
return 0, fmt.Errorf("服务器IP不能为空")
}

if obj.ServerAccount == "" {
return 0, fmt.Errorf("登录账户不能为空")
}

if obj.Type == 0 && obj.Password == "" {
return 0, fmt.Errorf("服务器密码不能为空")
}

if obj.Type == 1 && obj.PrivateKeySrc == "" {
return 0, fmt.Errorf("私钥不能为空")
err := Detection(obj)
if err != nil {
return 0, fmt.Errorf(err.Error())
}
if obj.Password == "" || obj.PrivateKeySrc == "" {
switch obj.Type {
case 0:
return 0, errors.New("服务器密码不能为空")
case 1:
return 0, errors.New("私钥不能为空")
default:
return 0, errors.New("类型不在规范之内")
}
}
return orm.NewOrm().Insert(obj)
}
Expand Down Expand Up @@ -111,7 +123,6 @@ func TaskServerForActuator(serverIp string, port int) int {
return 0
}

//
func TaskServerGetByIds(ids string) ([]*TaskServer, int64) {

serverFilters := make([]interface{}, 0)
Expand Down
36 changes: 36 additions & 0 deletions models/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package models

import (
"errors"
"fmt"
"testing"
)

func TestDetection(t *testing.T) {
T := &TaskServer{
Type: 0,
ServerName: "ServerName",
ServerAccount: "ServerAccount",
ServerIp: "ServerIp",
}
err := T.Update()
fmt.Print(err)
}

func (t *TaskServer) update() error {
err := Detection(t)
if err != nil {
return fmt.Errorf(err.Error())
}
if t.Password == "" || t.PrivateKeySrc == "" {
switch t.Type {
case 0:
return errors.New("服务器密码不能为空")
case 1:
return errors.New("私钥不能为空")
default:
return errors.New("类型不在规范之内")
}
}
return nil
}