119 lines
2.1 KiB
Go
119 lines
2.1 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// FirstUpper 字符串首字母大写
|
|
func FirstUpper(s string) string {
|
|
if s == "" {
|
|
return ""
|
|
}
|
|
return strings.ToUpper(s[:1]) + s[1:]
|
|
}
|
|
|
|
type GiteaClient struct {
|
|
client *gitea.Client
|
|
}
|
|
|
|
func NewGiteaClient(addr string) (*GiteaClient, error) {
|
|
client, err := gitea.NewClient(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &GiteaClient{client: client}, nil
|
|
}
|
|
|
|
func (c *GiteaClient) GetLatestCommitID(owner, repoName string) (string, error) {
|
|
commits, _, err := c.client.ListRepoCommits(owner, repoName, gitea.ListCommitOptions{
|
|
ListOptions: gitea.ListOptions{
|
|
Page: 1,
|
|
PageSize: 1,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return commits[0].SHA, nil
|
|
}
|
|
|
|
type EasYaml struct {
|
|
}
|
|
|
|
func NewEasYaml() *EasYaml {
|
|
return &EasYaml{}
|
|
}
|
|
|
|
func (y *EasYaml) Read(filename string, ptr interface{}) error {
|
|
body, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := yaml.Unmarshal(body, ptr); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (y *EasYaml) Write(filename string, ptr interface{}) error {
|
|
body, err := yaml.Marshal(ptr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(filename, body, os.ModePerm)
|
|
}
|
|
|
|
type Runtime struct {
|
|
}
|
|
|
|
func NewRuntime() *Runtime {
|
|
return &Runtime{}
|
|
}
|
|
|
|
func (r *Runtime) Exec(cmd string, args ...string) (output string, err error) {
|
|
command := exec.Command(cmd, args...)
|
|
body, err := command.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(body), nil
|
|
}
|
|
|
|
type GOOS string
|
|
|
|
const (
|
|
Linux GOOS = "linux"
|
|
Windows GOOS = "windows"
|
|
Darwin GOOS = "darwin"
|
|
Freebsd GOOS = "freebsd"
|
|
)
|
|
|
|
// GetGOOS get GOOS value
|
|
func (r *Runtime) GetGOOS() GOOS {
|
|
return GOOS(runtime.GOOS)
|
|
}
|
|
|
|
// GetHomeDir get home directory
|
|
func (r *Runtime) GetHomeDir() string {
|
|
switch r.GetGOOS() {
|
|
case Linux:
|
|
return os.Getenv("HOME")
|
|
case Windows:
|
|
return os.Getenv("USERPROFILE")
|
|
default:
|
|
return os.Getenv("HOME")
|
|
}
|
|
}
|
|
|
|
// ReplaceEachSlash replacing each slash ('/') character
|
|
func (r *Runtime) ReplaceEachSlash(filename string) string {
|
|
return filepath.FromSlash(filename)
|
|
}
|