package buildqueue import ( "brunel/config" "brunel/domain" "fmt" "os" "path/filepath" "strconv" "strings" "time" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/transport/http" ) const configsFolder = "pika-build-config" func InitGit() error { _, err := git.PlainClone(config.Configs.GitCache, false, &git.CloneOptions{ URL: config.Configs.Buildrepo, SingleBranch: true, Depth: 1, Auth: &http.BasicAuth{ Password: config.Configs.GitToken, Username: config.Configs.GitUser, }, }) return err } func ResetAndPull() error { gt, err := git.PlainOpen(config.Configs.GitCache) if err != nil { return err } wk, err := gt.Worktree() if err != nil { return err } err = wk.Reset(&git.ResetOptions{ Mode: git.HardReset, }) if err != nil { return err } err = wk.Pull(&git.PullOptions{ Auth: &http.BasicAuth{ Password: config.Configs.GitToken, Username: config.Configs.GitUser, }, RemoteName: "origin", }) return err } func UpdateBuildFile(buildItem domain.BuildQueueItem) error { if _, err := os.Stat(config.Configs.GitCache); os.IsNotExist(err) { err := InitGit() if err != nil { return err } } err := ResetAndPull() if err != nil { return err } repo, err := git.PlainOpen(config.Configs.GitCache) if err != nil { return err } wt, err := repo.Worktree() if err != nil { return err } content := constructFileContent(buildItem) filePath := filepath.Join(config.Configs.GitCache, configsFolder, string(buildItem.Type)+".sh") err = os.WriteFile(filePath, []byte(content), 0644) if err != nil { return err } _, err = wt.Add(configsFolder + "/" + string(buildItem.Type) + ".sh") if err != nil { return err } _, err = wt.Commit(buildItem.Source.Name+"="+buildItem.BuildVersion, &git.CommitOptions{ Author: &object.Signature{ Name: "Brunel", Email: "brunel@pika-os.com", When: time.Now(), }, }) if err != nil { return err } err = repo.Push(&git.PushOptions{ Auth: &http.BasicAuth{ Password: config.Configs.GitToken, Username: config.Configs.GitUser, }, RemoteName: "origin", }) if err != nil { return err } return nil } func constructFileContent(buildItem domain.BuildQueueItem) string { content := []string{ fmt.Sprintf(`export PIKA_PACKAGE_VERSION="%s"`, buildItem.BuildVersion), fmt.Sprintf(`export PIKA_PACKAGE_NAME="%s"`, buildItem.Source.Name), fmt.Sprintf(`export PIKA_PACKAGE_PATCH=%t`, buildItem.Patch), fmt.Sprintf(`export PIKA_REBUILD=%t`, buildItem.Rebuild), fmt.Sprintf(`export PIKA_REBUILD_VERSION="%s"`, "b"+strconv.Itoa(buildItem.BuildNumber)), fmt.Sprintf(`export PIKA_BUILD_ATTEMPT="%d"`, buildItem.BuildNumber), } return strings.Join(content, "\n") }