This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
305 lines (255 loc) · 7.6 KB
/
main.go
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"bytes"
"strings"
"fmt"
"time"
"os/exec"
"log"
"github.com/anjannath/systray"
//"../systray_sm_fork_pim"
"eugene/assets"
"eugene/config"
"eugene/hugo"
"github.com/kr/pretty"
)
const AppVersion = "0.0.5"
var (
siteSubmenus = make(map[string]*systray.MenuItem)
menuItemLiveUrl *systray.MenuItem
menuItemPublish *systray.MenuItem
menuItemToggleHugoServer *systray.MenuItem
menuItemOpenConcept *systray.MenuItem
menuItemExit *systray.MenuItem
menuSelectSite *systray.MenuItem
menuConfig *systray.MenuItem
menuToggles *systray.MenuItem
menuToggleShowConcept *systray.MenuItem
)
func main() {
config.SetCurrentSite()
log.Printf("Eugene Config: %# v", pretty.Formatter(config.CurrentSite))
systray.Run(onReady, onExit)
}
func onReady() {
fmt.Printf("OnReady: %v+\n", time.Now())
hugo.RestartHugo()
systray.SetIcon(images.EugeneMonoData)
if(config.FatalError != "") {
systray.AddMenuItem(config.FatalError,"",0)
} else {
renderCMSMenu()
}
renderFooterMenu()
}
func onExit() {
if hugo.HugoRunning(){
hugo.KillHugo();
}
}
func renderCMSMenu(){
setCurrentSiteMenu()
systray.AddSeparator()
stagingMenu()
switchSitesMenu()
systray.AddSeparator()
togglesMenu()
configMenu()
systray.AddSeparator()
listenToServer()
handleCMSMenuClicks()
}
func renderFooterMenu(){
systray.AddMenuItem(fmt.Sprintf("Version: %s", AppVersion), "", 0)
menuItemExit = systray.AddMenuItem("Quit", "", 0)
handleFooterMenuClicks()
}
func updateCMSMenu() {
//open Live Url
if (config.CurrentSite.Live_Url != "") {
menuItemLiveUrl.SetTitle(fmt.Sprintf("Open %s", config.CurrentSite.Live_Url ))
}
//open concept versie
menuItemOpenConcept.SetTitle(fmt.Sprintf("Open %s in conceptversie", config.CurrentSite.Name))
//open Live Url
log.Println(config.CurrentSite.Live_Publishing_Command)
if (config.CurrentSite.Live_Url != "" && config.CurrentSite.Live_Publishing_Command != "") {
menuItemPublish.SetTitle(fmt.Sprintf("Publish to %s", config.CurrentSite.Live_Url ))
menuItemPublish.Enable()
} else{
menuItemPublish.SetTitle(fmt.Sprintf("Conf not complete for Publishing"))
menuItemPublish.Disable()
}
}
func stagingMenu(){
}
func setCurrentSiteMenu(){
//open Live Url
if (config.CurrentSite.Live_Url != "") {
menuItemLiveUrl = systray.AddMenuItem(fmt.Sprintf("Open %s", config.CurrentSite.Live_Url ) , "", 0)
}
//start Server Concept
//stop Server Concept
if hugo.HugoRunning(){
menuItemToggleHugoServer = systray.AddMenuItem("stop lokale server", "", 0)
} else {
menuItemToggleHugoServer = systray.AddMenuItem("start lokale server", "", 0)
}
//open Live Url
if (config.CurrentSite.Live_Url != "" && config.CurrentSite.Live_Publishing_Command != "") {
menuItemPublish = systray.AddMenuItem(fmt.Sprintf("Publish to %s", config.CurrentSite.Live_Url ) , "", 0)
} else{
menuItemPublish = systray.AddMenuItem(fmt.Sprintf("Config not complete for Publishing") , "", 0)
}
//open concept versie
menuItemOpenConcept = systray.AddMenuItem(fmt.Sprintf("Open %s in conceptversie", config.CurrentSite.Name), "", 0)
}
func configMenu(){
menuConfig = systray.AddSubMenu("Configuration")
menuOpenConfigFile := menuConfig.AddSubMenuItem("Open configuration file","", 0)
go func() {
for {
select {
case <-menuOpenConfigFile.OnClickCh():
var errOut bytes.Buffer
c := exec.Command("/usr/bin/open", config.File2())
c.Dir = config.CurrentSite.Live_Hugo_Output_Dir
c.Stderr = &errOut
out, err := c.Output()
outStr := strings.TrimSpace(string(out))
if err != nil {
err = fmt.Errorf("open: error=%q stderr=%s", err, string(errOut.Bytes()))
}
log.Printf("open Result: %# v", pretty.Formatter(outStr))
}
}
}()
}
func togglesMenu(){
menuToggles = systray.AddSubMenu("Toggles")
menuToggleShowConcept = menuToggles.AddSubMenuItem("Toggle draft items","", 0)
updateTogglesMenu()
go func() {
for {
select {
case <-menuToggleShowConcept.OnClickCh():
if(config.ShowDraftItems){
config.ShowDraftItems = false
} else {
config.ShowDraftItems = true
}
hugo.RestartHugo()
updateTogglesMenu()
}
}
}()
}
func updateTogglesMenu(){
if(config.ShowDraftItems){
menuToggleShowConcept.SetTitle("Hide draft items")
} else {
menuToggleShowConcept.SetTitle("Show draft items")
}
}
func switchSitesMenu(){
if(len(config.CurrentConfig.Sites)>1){
menuSelectSite = systray.AddSubMenu("Switch site")
for _, site := range config.CurrentConfig.Sites {
tmpSubmenuItem := menuSelectSite.AddSubMenuItem(site.Name,"", 0)
siteSubmenus[site.Name] = tmpSubmenuItem
}
for sName, sMenuItem := range siteSubmenus {
go func(name string, siteMenuitem *systray.MenuItem) {
for {
<-siteMenuitem.OnClickCh()
log.Println("Selecting %s", name)
config.FindSiteIndexByName(name)
config.SetCurrentSiteIndexByName(name)
hugo.RestartHugo();
updateCMSMenu()
}
}(sName, sMenuItem)
}
}
}
func listenToServer(){
go func() {
for {
time.Sleep(time.Second)
if hugo.HugoRunning(){
menuItemToggleHugoServer.SetTitle("Stop Server")
menuItemOpenConcept.Enable()
} else{
menuItemToggleHugoServer.SetTitle("Start Server")
menuItemOpenConcept.Disable()
}
}
}()
}
func gitCommand(args ...string){
var errOut bytes.Buffer
c := exec.Command("/usr/bin/git", args...)
c.Dir = config.CurrentSite.Live_Hugo_Output_Dir
c.Stderr = &errOut
out, err := c.Output()
outStr := strings.TrimSpace(string(out))
if err != nil {
err = fmt.Errorf("git: error=%q stderr=%s", err, string(errOut.Bytes()))
}
log.Printf("git args: %# v", pretty.Formatter(args))
log.Printf("Publish Result: %# v", pretty.Formatter(outStr))
}
func rsyncBuildTo(dest_dir string){
var errOut bytes.Buffer
c := exec.Command("/usr/bin/rsync", "-av", config.CurrentSite.Hugo_Build_Dir + "/", config.CurrentSite.Live_Hugo_Output_Dir + "/")
c.Dir = config.CurrentSite.Live_Hugo_Output_Dir
c.Stderr = &errOut
out, err := c.Output()
outStr := strings.TrimSpace(string(out))
if err != nil {
err = fmt.Errorf("git: error=%q stderr=%s", err, string(errOut.Bytes()))
}
log.Printf("Rsync Result: %# v", pretty.Formatter(outStr))
}
func handleCMSMenuClicks(){
go func() {
for {
select {
case <-menuItemLiveUrl.OnClickCh():
exec.Command("/usr/bin/open", config.CurrentSite.Live_Url).Output()
case <-menuItemPublish.OnClickCh():
//open Live Url
if (config.CurrentSite.Live_Url != "" && config.CurrentSite.Live_Publishing_Command != "") {
fmt.Println("Building Hugo")
hugo.Build();
fmt.Println("Rsyncing Build to Output dir")
rsyncBuildTo(config.CurrentSite.Live_Hugo_Output_Dir)
fmt.Println("Git commands")
gitCommand("add", ".")
gitCommand("commit", "-m", "Published with Hugo Control", "-a")
gitCommand("push")
}
case <-menuItemOpenConcept.OnClickCh():
exec.Command("/usr/bin/open", "http://localhost:1313").Output()
case <-menuItemToggleHugoServer.OnClickCh():
if hugo.HugoRunning(){
hugo.KillHugo();
} else{
hugo.StartHugo();
menuItemOpenConcept.Show()
}
}
}
}()
}
func handleFooterMenuClicks(){
go func() {
for {
select {
case <-menuItemExit.OnClickCh():
systray.Quit()
return
}
}
}()
}