repolist/common.go

207 lines
3.7 KiB
Go

package repolist
import (
"fmt"
"os"
"path/filepath"
"sort"
"github.com/go-cmd/cmd"
"go.wit.com/gui"
"go.wit.com/log"
)
func (r *RepoList) Hidden() bool {
return r.reposbox.Hidden()
}
func (r *RepoList) Show() {
r.reposbox.Show()
}
func (r *RepoList) Hide() {
r.reposbox.Hide()
}
func (r *RepoRow) IsProtobuf() (bool, []string, error) {
return r.pb.IsProtobuf()
}
// better name: use this
// matches by path or by name
func (r *RepoList) FindByName(name string) *RepoRow {
loop := r.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
if repo.pb.GoPath == name {
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)
}
func (r *RepoList) FindRepo(path string) *RepoRow {
repo, _ := me.allrepos[path]
return repo
}
/*
func (r *RepoList) AllRepos() []*RepoRow {
var all []*RepoRow
for _, repo := range me.allrepos {
all = append(all, repo)
}
return all
}
// deprecate this
func AllRepos() []*RepoRow {
var all []*RepoRow
for _, repo := range me.allrepos {
all = append(all, repo)
}
return all
}
*/
// a human readable state of the current repo
func (r *RepoRow) State() string {
return r.gitState.String()
}
func (r *RepoRow) Scan() int {
return r.NewScan()
}
// returns a name for human consuption only
// todo: implement nicknames
func (rs *RepoRow) Name() string {
return rs.pb.GoPath
}
func (r *RepoRow) GoPath() string {
return r.pb.GoPath
}
func (r *RepoRow) CheckDirty() bool {
return r.pb.CheckDirty()
}
func (r *RepoRow) IsDirty() bool {
return r.pb.IsDirty()
}
func (r *RepoRow) RepoType() string {
return r.pb.RepoType()
}
func (r *RepoRow) ReadOnly() bool {
if r == nil {
log.Warn("ReadOnly() repo == nil")
return false
}
if r.pb == nil {
log.Warn("ReadOnly() repo.pb == nil")
return false
}
return r.pb.ReadOnly
}
func (r *RepoRow) LastTag() string {
if r == nil {
log.Warn("LastTag() repo == nil")
return ""
}
return r.lastTag.String()
}
func (r *RepoRow) IsPerfect() bool {
if r.gitState.String() == "PERFECT" {
return true
}
if r.gitState.String() == "unchanged" {
return true
}
return false
}
func (r *RepoRow) Run(cmd []string) cmd.Status {
return r.pb.Run(cmd)
}
/*
func (r *RepoRow) AllTags() []*repostatus.Tag {
return r.Status.Tags.ListAll()
}
func (r *RepoRow) TagsBox() *repostatus.GitTagBox {
return r.Status.Tags
}
// todo, fix bool return for deletetag()
func (r *RepoRow) DeleteTag(t *repostatus.Tag) bool {
r.Status.DeleteTag(t)
return true
}
*/
func (rl *RepoList) MirrorShownCount() *gui.Node {
return gui.RawMirror(rl.shownCount)
}
func (rl *RepoList) MirrorScanDuration() *gui.Node {
return gui.RawMirror(rl.duration)
}
func (rl *RepoList) Total() int {
return len(me.allrepos)
}
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()
fmt.Fprintln(f, "go 1.20") // fix this
fmt.Fprintln(f, "")
fmt.Fprintln(f, "use (")
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]
if repo.pb.GoPath == "" {
// skip repos that aren't go
// todo: handle non-flat repos?
log.Info("SKIPPED REPO", repo.pb.GoPath)
continue
}
if repo.pb.Exists("go.mod") {
fmt.Fprintln(f, "\t"+repo.pb.GoPath)
log.Info("ADDING REPO", goSrcDir, repo.pb.GoPath)
} else {
log.Log(REPO, "missing go.mod for", repo.pb.GoPath)
}
}
fmt.Fprintln(f, ")")
return nil
}