repolist/common.go

207 lines
3.7 KiB
Go
Raw Normal View History

2024-02-17 08:39:55 -06:00
package repolist
import (
2024-03-09 09:23:41 -06:00
"fmt"
"os"
"path/filepath"
2024-03-09 16:49:57 -06:00
"sort"
2024-03-09 09:23:41 -06:00
2024-11-08 06:43:45 -06:00
"github.com/go-cmd/cmd"
2024-02-19 19:43:21 -06:00
"go.wit.com/gui"
2024-02-18 17:56:25 -06:00
"go.wit.com/log"
2024-02-17 08:39:55 -06:00
)
func (r *RepoList) Hidden() bool {
2024-02-17 14:22:24 -06:00
return r.reposbox.Hidden()
2024-02-17 08:39:55 -06:00
}
func (r *RepoList) Show() {
2024-02-17 14:22:24 -06:00
r.reposbox.Show()
2024-02-17 08:39:55 -06:00
}
func (r *RepoList) Hide() {
2024-02-17 14:22:24 -06:00
r.reposbox.Hide()
2024-02-17 08:39:55 -06:00
}
2024-11-29 23:18:18 -06:00
func (r *RepoRow) IsProtobuf() (bool, []string, error) {
return r.pb.IsProtobuf()
}
// better name: use this
2024-11-22 21:05:20 -06:00
// matches by path or by name
func (r *RepoList) FindByName(name string) *RepoRow {
loop := r.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
2024-12-03 18:03:09 -06:00
if repo.pb.GoPath == name {
2024-11-22 21:05:20 -06:00
return repo
}
base := filepath.Base(repo.GoPath())
if base == name {
return repo
}
}
return nil
}
func (r *RepoList) FindByPath(path string) *RepoRow {
return r.FindRepo(path)
}
2024-02-23 09:02:51 -06:00
func (r *RepoList) FindRepo(path string) *RepoRow {
2024-02-18 15:09:35 -06:00
repo, _ := me.allrepos[path]
return repo
}
2024-11-14 21:48:00 -06:00
/*
2024-02-23 09:02:51 -06:00
func (r *RepoList) AllRepos() []*RepoRow {
var all []*RepoRow
2024-02-17 08:39:55 -06:00
for _, repo := range me.allrepos {
all = append(all, repo)
}
return all
}
// deprecate this
2024-02-23 09:02:51 -06:00
func AllRepos() []*RepoRow {
var all []*RepoRow
2024-02-17 08:39:55 -06:00
for _, repo := range me.allrepos {
all = append(all, repo)
}
return all
}
2024-11-14 21:48:00 -06:00
*/
2024-02-17 08:39:55 -06:00
2024-02-17 15:48:56 -06:00
// a human readable state of the current repo
2024-02-23 09:02:51 -06:00
func (r *RepoRow) State() string {
return r.gitState.String()
2024-02-17 14:22:24 -06:00
}
2024-02-23 14:25:00 -06:00
func (r *RepoRow) Scan() int {
2024-02-17 14:22:24 -06:00
return r.NewScan()
2024-02-17 08:39:55 -06:00
}
2024-02-17 15:48:56 -06:00
// returns a name for human consuption only
// todo: implement nicknames
2024-02-23 09:02:51 -06:00
func (rs *RepoRow) Name() string {
2024-12-03 18:03:09 -06:00
return rs.pb.GoPath
2024-02-17 08:39:55 -06:00
}
2024-02-23 09:02:51 -06:00
func (r *RepoRow) GoPath() string {
2024-12-03 18:03:09 -06:00
return r.pb.GoPath
2024-02-17 08:39:55 -06:00
}
2024-02-23 09:02:51 -06:00
func (r *RepoRow) CheckDirty() bool {
2024-12-03 18:03:09 -06:00
return r.pb.CheckDirty()
2024-02-17 08:39:55 -06:00
}
2024-02-23 09:02:51 -06:00
func (r *RepoRow) IsDirty() bool {
2024-12-03 18:03:09 -06:00
return r.pb.IsDirty()
2024-02-17 08:39:55 -06:00
}
2024-11-28 21:03:23 -06:00
func (r *RepoRow) RepoType() string {
return r.pb.RepoType()
}
2024-02-23 09:02:51 -06:00
func (r *RepoRow) ReadOnly() bool {
2024-02-19 19:43:21 -06:00
if r == nil {
log.Warn("ReadOnly() repo == nil")
return false
}
2024-12-03 18:03:09 -06:00
if r.pb == nil {
log.Warn("ReadOnly() repo.pb == nil")
2024-02-19 19:43:21 -06:00
return false
}
2024-12-03 18:03:09 -06:00
return r.pb.ReadOnly
2024-02-17 08:39:55 -06:00
}
2024-02-23 09:02:51 -06:00
func (r *RepoRow) LastTag() string {
2024-02-19 19:43:21 -06:00
if r == nil {
log.Warn("LastTag() repo == nil")
return ""
}
return r.lastTag.String()
2024-02-18 15:09:35 -06:00
}
2024-02-23 09:02:51 -06:00
func (r *RepoRow) IsPerfect() bool {
if r.gitState.String() == "PERFECT" {
2024-02-17 08:39:55 -06:00
return true
}
if r.gitState.String() == "unchanged" {
2024-02-17 14:22:24 -06:00
return true
}
2024-02-17 08:39:55 -06:00
return false
}
2024-11-08 06:43:45 -06:00
func (r *RepoRow) Run(cmd []string) cmd.Status {
2024-12-03 18:03:09 -06:00
return r.pb.Run(cmd)
2024-02-17 08:39:55 -06:00
}
2024-12-03 18:03:09 -06:00
/*
2024-02-23 09:02:51 -06:00
func (r *RepoRow) AllTags() []*repostatus.Tag {
2024-02-17 15:48:56 -06:00
return r.Status.Tags.ListAll()
2024-02-17 08:39:55 -06:00
}
2024-02-23 09:02:51 -06:00
func (r *RepoRow) TagsBox() *repostatus.GitTagBox {
2024-02-17 15:48:56 -06:00
return r.Status.Tags
2024-02-17 08:39:55 -06:00
}
// todo, fix bool return for deletetag()
2024-02-23 09:02:51 -06:00
func (r *RepoRow) DeleteTag(t *repostatus.Tag) bool {
2024-02-17 15:48:56 -06:00
r.Status.DeleteTag(t)
2024-02-17 08:39:55 -06:00
return true
}
2024-12-03 18:03:09 -06:00
*/
2024-02-19 19:43:21 -06:00
func (rl *RepoList) MirrorShownCount() *gui.Node {
return gui.RawMirror(rl.shownCount)
}
2024-02-20 14:45:09 -06:00
func (rl *RepoList) MirrorScanDuration() *gui.Node {
return gui.RawMirror(rl.duration)
}
2024-02-21 11:17:54 -06:00
func (rl *RepoList) Total() int {
return len(me.allrepos)
}
2024-03-09 09:23:41 -06:00
func (rl *RepoList) MakeGoWork() error {
goSrcDir := os.Getenv("REPO_WORK_PATH")
filename := filepath.Join(goSrcDir, "go.work")
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer f.Close()
2024-12-07 16:49:20 -06:00
fmt.Fprintln(f, "go 1.20") // fix this
2024-03-09 09:23:41 -06:00
fmt.Fprintln(f, "")
fmt.Fprintln(f, "use (")
2024-03-09 16:49:57 -06:00
keys := make([]string, 0, len(rl.allrepos))
for k := range rl.allrepos {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
repo := rl.allrepos[k]
2024-12-03 18:03:09 -06:00
if repo.pb.GoPath == "" {
2024-03-09 09:23:41 -06:00
// skip repos that aren't go
// todo: handle non-flat repos?
2024-12-03 18:03:09 -06:00
log.Info("SKIPPED REPO", repo.pb.GoPath)
2024-03-09 09:23:41 -06:00
continue
}
2024-12-03 18:03:09 -06:00
if repo.pb.Exists("go.mod") {
fmt.Fprintln(f, "\t"+repo.pb.GoPath)
log.Info("ADDING REPO", goSrcDir, repo.pb.GoPath)
2024-03-09 09:23:41 -06:00
} else {
2024-12-03 18:03:09 -06:00
log.Log(REPO, "missing go.mod for", repo.pb.GoPath)
2024-03-09 09:23:41 -06:00
}
}
fmt.Fprintln(f, ")")
return nil
}