gowit/listWindow.go

330 lines
6.6 KiB
Go
Raw Normal View History

2024-02-13 21:45:25 -06:00
package gowit
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"go.wit.com/gui"
"go.wit.com/log"
"go.wit.com/lib/gadgets"
2024-02-18 15:09:19 -06:00
"go.wit.com/lib/gui/repolist"
2024-03-01 18:02:44 -06:00
"go.wit.com/lib/gui/repostatus"
2024-02-13 21:45:25 -06:00
"go.wit.com/lib/gui/shell"
)
var lw *gadgets.BasicWindow
var allsections []*section
type witRepo struct {
2024-03-09 18:05:49 -06:00
hidden bool
2024-03-01 18:02:44 -06:00
sec *section
path *gui.Node
downloadB *gui.Node
configureB *gui.Node
}
func (w *witRepo) Show() {
w.hidden = false
w.path.Show()
w.downloadB.Show()
w.configureB.Show()
}
func (w *witRepo) Hide() {
w.hidden = true
w.path.Hide()
w.downloadB.Hide()
w.configureB.Hide()
2024-02-13 21:45:25 -06:00
}
type section struct {
name string
hidden bool
parent *gui.Node
box *gui.Node
group *gui.Node
grid *gui.Node // where the repos are listed
hideCB *gui.Node
downloadAllB *gui.Node
witRepos []*witRepo
}
func CheckRegistered(rs *repostatus.RepoStatus) (bool, string) {
var found bool = false
var source string
url := "https://" + rs.String()
lines := dumpURL(url)
for _, line := range lines {
// log.Info("HTTP:", i, line)
if strings.Contains(line, "\"go-import\"") {
line = strings.TrimSuffix(line, "\">")
parts := strings.Split(line, " ")
source = parts[len(parts)-1]
// log.Verbose("FOUND IMPORT:", i, source)
found = true
}
}
return found, source
}
2024-03-01 18:02:44 -06:00
func myrepolist(cfgfile string) []string {
2024-02-15 22:50:01 -06:00
homeDir, _ := os.UserHomeDir()
2024-03-01 18:02:44 -06:00
cfgfile = filepath.Join(homeDir, cfgfile)
2024-02-15 22:50:01 -06:00
content, _ := ioutil.ReadFile(cfgfile)
out := string(content)
out = strings.TrimSpace(out)
lines := strings.Split(out, "\n")
return lines
}
2024-02-18 15:09:19 -06:00
func ListWindow(view *repolist.RepoList) *gadgets.BasicWindow {
2024-02-13 21:45:25 -06:00
if lw != nil {
if lw.Hidden() {
lw.Show()
} else {
lw.Hide()
}
return lw
}
lw = gadgets.RawBasicWindow("go.wit.com repositories")
lw.Custom = func() {
log.Warn("got to close")
}
lw.Make()
box := lw.Box()
group := box.NewGroup("list")
group.NewButton("make new go version list", func() {
2024-02-20 21:27:29 -06:00
DumpVersions(view)
2024-02-13 21:45:25 -06:00
})
var lines []string
2024-02-15 22:50:01 -06:00
var currents *section
2024-03-09 18:05:49 -06:00
currents = NewSection(group, "local "+view.Cfgfile())
2024-03-01 18:02:44 -06:00
for i, line := range myrepolist(view.Cfgfile()) {
2024-02-22 15:28:32 -06:00
line = strings.TrimSpace(line)
if line == "" {
// skip blank lines
continue
}
if strings.HasPrefix(line, "#") {
// skip comment lines
continue
}
2024-02-15 22:50:01 -06:00
parts := strings.Split(line, " ")
log.Info("adding:", i, parts)
2024-02-18 15:09:19 -06:00
currents.add(view, parts[0])
2024-02-15 22:50:01 -06:00
}
2024-02-13 21:45:25 -06:00
lines = dumpURL("https://go.wit.com/list")
for i, line := range lines {
if line == "" {
continue
}
if line[0] == '#' {
2024-02-15 22:50:01 -06:00
currents = NewSection(group, line)
2024-02-13 21:45:25 -06:00
log.Warn("new group:", line)
continue
}
log.Warn(i, line)
parts := strings.Split(line, " ")
2024-02-15 22:50:01 -06:00
if currents != nil {
2024-02-18 15:09:19 -06:00
currents.add(view, parts[0])
2024-02-13 21:45:25 -06:00
}
}
for i, sec := range allsections {
log.Info("section name:", sec.name, "hidden:", sec.hidden, i)
parts := strings.Split(sec.name, " ")
if len(parts) > 1 {
2024-02-15 22:50:01 -06:00
if parts[1] == "Applications" {
// leave expanded
} else if parts[0] == "local" {
// leave expanded
} else {
2024-02-13 21:45:25 -06:00
sec.Hide()
}
}
}
return lw
}
func downloadRepo(path string) bool {
log.Info("downloading", path, "here")
os.Setenv("GO111MODULE", "off")
homeDir, _ := os.UserHomeDir()
goSrcDir := filepath.Join(homeDir, "go/src")
2024-02-13 21:45:25 -06:00
cmd := []string{"go-clone", "--recursive", "--no-work", "--go-src", path}
err := shell.NewRun(goSrcDir, cmd)
if err != nil {
log.Info("go-clone failed")
2024-02-13 21:45:25 -06:00
log.Info("err =", err)
// log.Info("output =", output)
2024-02-13 21:45:25 -06:00
return false
}
log.Info("go-clone worked")
2024-02-13 21:45:25 -06:00
return true
}
func (r *witRepo) doDownload() bool {
/*
if me.autoDryRun.Checked() {
r.downloadB.SetLabel("uncheck --dry-run")
return false
}
*/
if r.downloadB.String() == "downloaded" {
log.Info("skipping already downloaded", r.path.String())
2024-02-15 22:50:01 -06:00
r.downloadB.Disable()
2024-02-13 21:45:25 -06:00
return true
}
if downloadRepo(r.path.String()) {
log.Info("download", r.path.String(), "worked")
r.downloadB.SetLabel("downloaded")
r.downloadB.Disable()
} else {
r.downloadB.SetLabel("failed")
log.Info("download", r.path.String(), "failed")
return false
}
return true
}
2024-02-18 15:09:19 -06:00
func (s *section) add(view *repolist.RepoList, path string) {
2024-02-13 21:45:25 -06:00
if s == nil {
return
}
tmp := new(witRepo)
tmp.sec = s
tmp.path = s.grid.NewLabel(path)
tmp.downloadB = s.grid.NewButton("download", func() {
lw.Disable()
tmp.doDownload()
lw.Enable()
})
2024-02-18 15:09:19 -06:00
repo := view.FindRepo(path)
if repo != nil {
log.Verbose("repo is already downloaded", path)
2024-02-13 21:45:25 -06:00
tmp.downloadB.SetLabel("downloaded")
tmp.downloadB.Disable()
2024-03-01 18:02:44 -06:00
tmp.configureB = s.grid.NewButton("Configure", func() {
2024-02-22 15:28:32 -06:00
log.Log(WIT, "todo: open the repo window here")
})
2024-02-13 21:45:25 -06:00
}
2024-02-22 15:28:32 -06:00
s.grid.NextRow()
2024-02-13 21:45:25 -06:00
s.witRepos = append(s.witRepos, tmp)
}
func NewSection(parent *gui.Node, desc string) *section {
news := new(section)
news.name = desc
news.parent = parent
news.box = news.parent.NewBox("bw vbox", true)
news.group = news.box.NewGroup(desc)
news.hideCB = news.box.NewCheckbox("hide")
news.hideCB.Custom = func() {
news.toggle()
}
news.downloadAllB = news.box.NewButton("download all", func() {
lw.Disable()
log.Warn("Download all here")
for i, wrepo := range news.witRepos {
log.Warn("download:", i, wrepo.path.String())
wrepo.doDownload()
}
lw.Enable()
})
2024-02-22 15:28:32 -06:00
news.grid = news.parent.NewGrid("sections", 0, 0)
2024-02-13 21:45:25 -06:00
allsections = append(allsections, news)
return news
}
func (s *section) toggle() {
log.Warn(s.name)
if s.hidden {
s.hidden = false
for i, wrepo := range s.witRepos {
log.Warn(i, wrepo.path.String())
2024-03-01 18:02:44 -06:00
wrepo.Show()
2024-02-13 21:45:25 -06:00
}
} else {
s.Hide()
}
}
func (s *section) Hide() {
s.hidden = true
s.hideCB.SetChecked(true)
for i, wrepo := range s.witRepos {
log.Warn(i, wrepo.path.String())
2024-03-01 18:02:44 -06:00
wrepo.Hide()
2024-02-13 21:45:25 -06:00
}
}
/*
func dumpURL(url string) string {
resp, err := http.Get(url)
if err != nil {
return ""
}
defer resp.Body.Close()
return resp.Body.String()
_, err = io.Copy(os.Stdout, resp.Body)
if err != nil {
return ""
}
}
*/
func dumpURL(url string) []string {
resp, err := http.Get(url)
if err != nil {
return nil
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil
}
return strings.Split(string(bodyBytes), "\n")
}
func formatDuration(d time.Duration) string {
seconds := int(d.Seconds()) % 60
minutes := int(d.Minutes()) % 60
hours := int(d.Hours()) % 24
days := int(d.Hours()) / 24
result := ""
if days > 0 {
result += fmt.Sprintf("%dd ", days)
return result
}
if hours > 0 {
result += fmt.Sprintf("%dh ", hours)
return result
}
if minutes > 0 {
result += fmt.Sprintf("%dm ", minutes)
return result
}
if seconds > 0 {
result += fmt.Sprintf("%ds", seconds)
}
return result
}