Files
houston/shared/operator.go

115 lines
1.9 KiB
Go
Raw Normal View History

2023-05-21 23:37:54 +09:00
package shared
2023-05-23 10:57:24 +09:00
import (
"io/fs"
"os"
"strings"
)
2023-05-21 23:37:54 +09:00
type Operation string
const (
Deploy = Operation("deploy")
Withdraw = Operation("withdraw")
Upgrade = Operation("upgrade")
Start = Operation("start")
Restart = Operation("restart")
Stop = Operation("stop")
Upload = Operation("upload")
)
type DeployRequest struct {
Name string
Version string
Url string
AccessToken string
}
type WithdrawRequest struct {
Name string
Version string
}
type StartProcessRequest struct {
Name string
Version string
Args string
}
type StopProcessRequest struct {
Name string
Version string
Pid int32
}
type RestartProcessRequest struct {
Name string
Version string
}
type UploadRequest struct {
Name string
Version string
Url string
Filter string
DeleteAfterUploaded string // true, false
}
2023-05-23 10:57:24 +09:00
type ParsedVersionString = []string
func ParseVersionString(ver string) ParsedVersionString {
return strings.Split(ver, ".")
}
func CompareVersionString(lhs, rhs ParsedVersionString) int {
minlen := len(lhs)
if minlen > len(rhs) {
minlen = len(rhs)
}
for i := 0; i < minlen; i++ {
if len(lhs[i]) < len(rhs[i]) {
return -1
}
if len(lhs[i]) > len(rhs[i]) {
return 1
}
if lhs[i] < rhs[i] {
return -1
}
if lhs[i] > rhs[i] {
return 1
}
}
return len(lhs) - len(rhs)
}
func FindLastestVersion(root string) (string, error) {
// 최신 버전을 찾음
entries, err := os.ReadDir(root)
if err != nil {
return "", err
}
if len(entries) == 0 {
return "", nil
}
var dironly []fs.DirEntry
for _, ent := range entries {
if ent.IsDir() {
dironly = append(dironly, ent)
}
}
latest := ParseVersionString(dironly[0].Name())
for i := 1; i < len(dironly); i++ {
next := ParseVersionString(dironly[i].Name())
if CompareVersionString(latest, next) < 0 {
latest = next
}
}
return strings.Join(latest, "."), nil
}