1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package main
import (
"embed"
"fmt"
"net/http"
"strings"
"time"
"go.wit.com/lib/gui/prep"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
// are sent via -ldflags at buildtime
var VERSION string
var BUILDTIME string
//go:embed resources/*
var resources embed.FS
// used for shell auto completion
var ARGNAME string = "gowebd"
var repoMap map[string]string
var gitMap map[string]*gitpb.Repo
var configfile []string
var keysSorted []string
var HOSTNAME string = "go.wit.com"
var REPOMAP string = "/etc/gowebd/repomap"
var FOOTER string = "/etc/gowebd/footer.html"
var LIBDIR string = "/var/lib/gowebd/"
func main() {
me = new(mainType)
me.sh = prep.Bash(&argv) // adds shell auto complete to go-args
if argv.RepoMap != "" {
REPOMAP = argv.RepoMap
}
if argv.Hostname != "" {
HOSTNAME = argv.Hostname
}
gitMap = make(map[string]*gitpb.Repo)
repoMap = make(map[string]string)
me.forge = forgepb.Init()
// parse the repomap file
readRepomap()
if argv.Test != nil {
if s, err := newMakeRepomap(); err != nil {
me.sh.BadExit(s, err)
} else {
me.sh.GoodExit(s)
}
}
http.HandleFunc("/", okHandler)
// go https() // use caddy instead
p := fmt.Sprintf(":%d", argv.Port)
log.Println("HOSTNAME set to:", HOSTNAME)
log.Println("Running on port", p)
err := http.ListenAndServe(p, nil)
if err != nil {
log.Println("Error starting server:", err)
}
}
func newMakeRepomap() (string, error) {
submit := gitpb.NewRepos()
for key, val := range repoMap {
if !strings.HasPrefix(key, "go.wit.com") {
// only handling go.wit.com here
continue
}
log.Info(key)
r := new(gitpb.Repo)
r.Namespace = key
r.State = val
submit.Append(r)
}
server := me.forge.GetForgeURL()
updatepb, regPB, err := submit.HttpPost(server, "check")
if err != nil {
log.Info("ReposPB HttpPost() failed", err)
return "HttpPost() failed", err
}
if regPB == nil {
log.Info("regPB==nil")
return "HttpPost() internally returned nil http PB", err
}
if updatepb == nil {
log.Info("server sent nil back nil PB")
return "HttpPost() internally returned nil http PB", err
}
for repo := range updatepb.IterByNamespace() {
log.Info(repo.Namespace, repo.URL)
}
makePullTable(updatepb)
return "new dynamic repomap!", nil
}
func makePullTable(pb *gitpb.Repos) {
t := pb.NewTable("pullTable")
t.NewUuid()
var col *gitpb.RepoFunc
// var col int
col = t.AddNamespace()
col.Width = 30
col = t.AddMasterVersion()
// col.SetTitle("mver")
col.Width = 15
col = t.AddStringFunc("Tags", func(r *gitpb.Repo) string {
if r.Tags == nil {
return "nil"
}
log.Infof("repo: %v\n", r)
return log.Sprintf("len(%d)", r.Tags.Len())
})
col.Width = 9
col = t.AddStringFunc("cur tag", func(r *gitpb.Repo) string {
if r.CurrentTag == nil {
return "nil"
}
return "ok"
})
col.Width = 9
col = t.AddDevelVersion()
col.Width = 15
col = t.AddFullPath()
col.Width = -1
t.PrintTable()
}
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
}
|