重构msg的包

This commit is contained in:
陈景阳
2019-09-10 09:32:48 +08:00
parent e5d33b8982
commit 9514a8a6af
14 changed files with 398 additions and 220 deletions

View File

@@ -1,8 +1,25 @@
package model
import (
"crawlab/utils"
"github.com/apex/log"
"os"
)
type File struct {
Name string `json:"name"`
Path string `json:"path"`
IsDir bool `json:"is_dir"`
Size int64 `json:"size"`
}
func RemoveFile(path string) error {
if !utils.Exists(path) {
log.Info("file not found: " + path)
return nil
}
if err := os.Remove(path); err != nil {
return err
}
return nil
}

43
backend/model/log.go Normal file
View File

@@ -0,0 +1,43 @@
package model
import (
"github.com/apex/log"
"os"
"runtime/debug"
)
// 获取本地日志
func GetLocalLog(logPath string) (fileBytes []byte, err error) {
f, err := os.Open(logPath)
if err != nil {
log.Error(err.Error())
debug.PrintStack()
return nil, err
}
fi, err := f.Stat()
if err != nil {
log.Error(err.Error())
debug.PrintStack()
return nil, err
}
defer f.Close()
const bufLen = 2 * 1024 * 1024
logBuf := make([]byte, bufLen)
off := int64(0)
if fi.Size() > int64(len(logBuf)) {
off = fi.Size() - int64(len(logBuf))
}
n, err := f.ReadAt(logBuf, off)
//到文件结尾会有EOF标识
if err != nil && err.Error() != "EOF" {
log.Error(err.Error())
debug.PrintStack()
return nil, err
}
logBuf = logBuf[:n]
return logBuf, nil
}

View File

@@ -1,5 +1,40 @@
package model
import (
"github.com/apex/log"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
)
var executableNameMap = map[string]string{
// python
"python": "Python",
"python2": "Python 2",
"python2.7": "Python 2.7",
"python3": "Python 3",
"python3.5": "Python 3.5",
"python3.6": "Python 3.6",
"python3.7": "Python 3.7",
"python3.8": "Python 3.8",
// java
"java": "Java",
// go
"go": "Go",
// node
"node": "NodeJS",
// php
"php": "PHP",
// windows command
"cmd": "Windows Command Prompt",
// linux shell
"sh": "Shell",
"bash": "bash",
}
type SystemInfo struct {
ARCH string `json:"arch"`
OS string `json:"os"`
@@ -13,3 +48,64 @@ type Executable struct {
FileName string `json:"file_name"`
DisplayName string `json:"display_name"`
}
func GetLocalSystemInfo() (sysInfo SystemInfo, err error) {
executables, err := GetExecutables()
if err != nil {
return sysInfo, err
}
hostname, err := os.Hostname()
if err != nil {
debug.PrintStack()
return sysInfo, err
}
return SystemInfo{
ARCH: runtime.GOARCH,
OS: runtime.GOOS,
NumCpu: runtime.GOMAXPROCS(0),
Hostname: hostname,
Executables: executables,
}, nil
}
func GetSystemEnv(key string) string {
return os.Getenv(key)
}
func GetPathValues() (paths []string) {
pathEnv := GetSystemEnv("PATH")
return strings.Split(pathEnv, ":")
}
func GetExecutables() (executables []Executable, err error) {
pathValues := GetPathValues()
cache := map[string]string{}
for _, path := range pathValues {
fileList, err := ioutil.ReadDir(path)
if err != nil {
log.Errorf(err.Error())
debug.PrintStack()
continue
}
for _, file := range fileList {
displayName := executableNameMap[file.Name()]
filePath := filepath.Join(path, file.Name())
if cache[filePath] == "" {
if displayName != "" {
executables = append(executables, Executable{
Path: filePath,
FileName: file.Name(),
DisplayName: displayName,
})
}
cache[filePath] = filePath
}
}
}
return executables, nil
}