This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
/
file.go
110 lines (101 loc) · 2.93 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package components
import (
"errors"
"fmt"
"github.com/linclin/gopub/src/library/common"
"os"
"strings"
)
type BaseFile struct {
baseComponents BaseComponents
}
func (c *BaseFile) SetBaseComponents(b BaseComponents) {
c.baseComponents = b
}
func (c *BaseFile) UpdateRepo(url string, fileDir string) error {
if fileDir == "" {
fileDir = c.baseComponents.GetDeployFromDir()
}
dotFile := strings.TrimRight(fileDir, "/")
if common.SubString(url, 0, 4) != "http" {
if url == "" {
url = c.baseComponents.project.RepoUrl
} else {
url = strings.TrimRight(c.baseComponents.project.RepoUrl, "/") + "/" + strings.TrimLeft(url, "/")
}
}
if _, err := os.Stat(dotFile); err != nil {
if os.IsNotExist(err) {
cmds := []string{}
cmds = append(cmds, fmt.Sprintf("mkdir -p %s ", dotFile))
cmds = append(cmds, fmt.Sprintf("cd %s ", fileDir))
cmds = append(cmds, fmt.Sprintf("wget '%s'", url))
cmd := strings.Join(cmds, "&&")
_, err := c.baseComponents.runLocalCommand(cmd)
return err
}
}
cmds := []string{}
cmds = append(cmds, fmt.Sprintf("cd %s ", dotFile))
cmds = append(cmds, fmt.Sprintf("rm -rf *"))
cmds = append(cmds, fmt.Sprintf("wget --no-check-certificate '%s'", url))
cmd := strings.Join(cmds, "&&")
_, err := c.baseComponents.runLocalCommand(cmd)
return err
}
/**
* 更新到指定commit版本
*/
func (c *BaseFile) UpdateToVersion() error {
destination := c.baseComponents.getDeployWorkspace(c.baseComponents.task.LinkId)
err := c.UpdateRepo(c.baseComponents.task.CommitId, destination)
if err != nil {
return err
}
if c.baseComponents.task.FileMd5 != "" {
ss, err := c.CheckFiles(c.baseComponents.task.CommitId, destination)
if err != nil && len(ss) == 0 {
return errors.New("md5校验失败--" + err.Error())
} else {
if ss[0]["message"] != c.baseComponents.task.FileMd5 {
return errors.New("md5校验失败--md5值不相等")
}
}
}
return nil
}
/**
* 查询文件md5
*
*/
func (c *BaseFile) CheckFiles(url string, fileDir string) ([]map[string]string, error) {
if fileDir == "" {
fileDir = c.baseComponents.GetDeployFromDir()
}
history := []map[string]string{}
if common.SubString(url, 0, 4) != "http" {
if url == "" {
url = c.baseComponents.project.RepoUrl
} else {
url = strings.TrimRight(c.baseComponents.project.RepoUrl, "/") + "/" + strings.TrimLeft(url, "/")
}
}
s := strings.Split(url, "/")
fileName := s[len(s)-1]
cmds := []string{}
cmds = append(cmds, fmt.Sprintf("cd %s ", fileDir))
cmds = append(cmds, fmt.Sprintf("test -f /usr/bin/md5sum && md5sum %s ", fileName))
cmd := strings.Join(cmds, "&&")
ss, err := c.baseComponents.runLocalCommand(cmd)
if err != nil {
return history, err
}
items := strings.Split(ss.Result, "\n")
for _, item := range items {
if strings.Index(item, " ") > -1 {
md5 := common.SubString(item, 0, strings.Index(item, " "))
history = append(history, map[string]string{"id": fileName, "message": md5})
}
}
return history, nil
}