Skip to content

Commit 5e353a3

Browse files
committed
fixup: cleanup
1 parent 93c49f1 commit 5e353a3

File tree

4 files changed

+449
-39
lines changed

4 files changed

+449
-39
lines changed

cmd/goprompt/cmdQuery.go

+48-32
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"os/signal"
88
"os/user"
99
"strings"
10-
"sync"
1110
"syscall"
1211
"time"
1312

1413
ps "github.com/mitchellh/go-ps"
14+
"github.com/sourcegraph/conc/pool"
1515
"github.com/spf13/cobra"
1616
)
1717

@@ -46,6 +46,7 @@ const (
4646
_partWorkDir = "wd"
4747
_partWorkDirShort = "wd_trim"
4848

49+
_partPid = "pid"
4950
_partPidShell = "pid_shell"
5051
_partPidShellExec = "pid_shell_exec"
5152
_partPidParent = "pid_parent"
@@ -82,16 +83,16 @@ func handleQUIT() context.CancelFunc {
8283
sig := make(chan os.Signal, 1)
8384
signal.Notify(sig, os.Interrupt, os.Kill, syscall.SIGTERM)
8485

85-
// defer flog("terminating")
86+
defer debugLog("quit: terminating")
8687

8788
// Stdout watchdog
8889
go func() {
89-
// flog("start watchdog " + fmt.Sprintf("%d", os.Getppid()))
90+
// debugLog("start watchdog " + fmt.Sprintf("%d", os.Getppid()))
9091
defer bgctxCancel()
9192

9293
for {
9394
if _, err := os.Stdout.Stat(); err != nil {
94-
// flog("terminating early")
95+
debugLog("quit: terminating early")
9596
return
9697
}
9798

@@ -100,7 +101,7 @@ func handleQUIT() context.CancelFunc {
100101
case <-tick:
101102
continue
102103
case <-sig:
103-
// flog("terminating early")
104+
debugLog("quit: terminating early")
104105
return
105106
case <-bgctx.Done():
106107
return
@@ -112,6 +113,7 @@ func handleQUIT() context.CancelFunc {
112113
}
113114

114115
func cmdQueryRun(_ *cobra.Command, _ []string) error {
116+
debugLog("query: start")
115117
defer bgctxCancel()
116118

117119
printerStop, printPart := startPrinter()
@@ -132,7 +134,7 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
132134
}()
133135
}
134136

135-
tasks := new(AsyncTaskDispatcher)
137+
tasks := pool.New().WithContext(bgctx)
136138
defer func() {
137139
tasks.Wait()
138140
printPart("done", "ok")
@@ -145,7 +147,7 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
145147
printPart(_partStatus, fmt.Sprintf("%#v", *flgQCmdStatus))
146148
}
147149

148-
tasks.Dispatch(func() {
150+
tasks.Go(func(ctx context.Context) error {
149151
homeDir := os.Getenv("HOME")
150152

151153
if wd, err := os.Getwd(); err == nil {
@@ -173,12 +175,14 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
173175
printPart(_partDuration, diff)
174176
}
175177
}
178+
179+
return nil
176180
})
177181

178-
tasks.Dispatch(func() {
182+
tasks.Go(func(_ context.Context) error {
179183
psChain, err := moduleFindProcessChain()
180184
if err != nil {
181-
return
185+
return nil
182186
}
183187

184188
if len(psChain) > 3 {
@@ -206,16 +210,18 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
206210
printPart(_partPidRemote, pidRemote.Pid())
207211
printPart(_partPidRemoteExec, pidShellRemoteExecName)
208212
}
213+
214+
return nil
209215
})
210216

211-
tasks.Dispatch(func() {
217+
tasks.Go(func(context.Context) error {
212218
subTasks := new(AsyncTaskDispatcher)
213219
defer subTasks.Wait()
214220

215221
if _, err := stringExec("git", "rev-parse", "--show-toplevel"); err == nil {
216222
printPart(_partVcs, "git")
217223
} else {
218-
return
224+
return nil
219225
}
220226

221227
subTasks.Dispatch(func() {
@@ -288,9 +294,11 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
288294
printPart(_partVcsLogBehind, parts[1])
289295
}
290296
})
297+
298+
return nil
291299
})
292300

293-
tasks.Dispatch(func() {
301+
tasks.Go(func(context.Context) error {
294302
var err error
295303

296304
subTasks := new(AsyncTaskDispatcher)
@@ -301,7 +309,7 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
301309
printPart(_partVcsStg, "1")
302310
printPart(_partVcsStgQlen, stgSeriesLen)
303311
} else {
304-
return
312+
return nil
305313
}
306314

307315
subTasks.Dispatch(func() {
@@ -314,7 +322,7 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
314322
if stgPatchTop, err = stringExec("stg", "top"); err == nil {
315323
printPart(_partVcsStgTop, stgPatchTop)
316324
} else {
317-
return
325+
return nil
318326
}
319327

320328
subTasks.Dispatch(func() {
@@ -327,29 +335,13 @@ func cmdQueryRun(_ *cobra.Command, _ []string) error {
327335
printPart(_partVcsStgDirty, 0)
328336
}
329337
})
338+
339+
return nil
330340
})
331341

332342
return nil
333343
}
334344

335-
func startPrinter() (func(), func(name string, value interface{})) {
336-
printCH := make(chan shellKV)
337-
printerWG := new(sync.WaitGroup)
338-
printerWG.Add(1)
339-
go func() {
340-
defer printerWG.Done()
341-
shellKVStaggeredPrinter(printCH, 20*time.Millisecond, 600*time.Millisecond)
342-
}()
343-
printerStop := func() {
344-
close(printCH)
345-
printerWG.Wait()
346-
}
347-
printPart := func(name string, value interface{}) {
348-
printCH <- shellKV{name, value}
349-
}
350-
return printerStop, printPart
351-
}
352-
353345
func moduleFindProcessChain() ([]ps.Process, error) {
354346
psPTR := os.Getpid()
355347
var pidChain []ps.Process
@@ -368,3 +360,27 @@ func moduleFindProcessChain() ([]ps.Process, error) {
368360

369361
return pidChain, nil
370362
}
363+
364+
365+
func startPrinter() (func(), func(name string, value interface{})) {
366+
debugLog("query-printer: start")
367+
defer debugLog("query-printer: stop")
368+
369+
printCH := make(chan shellKV)
370+
doneSIG := make(chan struct{})
371+
go func() {
372+
defer close(doneSIG)
373+
shellKVStaggeredPrinter(printCH, 20*time.Millisecond, 100*time.Millisecond)
374+
}()
375+
376+
printerStop := func() {
377+
close(printCH)
378+
<-doneSIG
379+
}
380+
printPart := func(name string, value interface{}) {
381+
printCH <- shellKV{name, value}
382+
}
383+
384+
printPart(_partPid, os.Getpid())
385+
return printerStop, printPart
386+
}

cmd/goprompt/main.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package main
22

33
import (
44
"context"
5-
"github.com/spf13/cobra"
65
"os"
6+
7+
"github.com/spf13/cobra"
78
)
89

910
var bgctx, bgctxCancel = context.WithCancel(context.Background())
@@ -12,10 +13,14 @@ var (
1213
cmd = &cobra.Command{
1314
Use: "goprompt",
1415
}
16+
envLogFile = os.Getenv("GOPROMPT_LOG_FILE")
1517
)
1618

17-
func flog(msg string) {
18-
f, err := os.OpenFile(os.Getenv("HOME")+"/.goprompt",
19+
func debugLog(msg string, args ...[]interface{}) {
20+
if len(envLogFile) == 0 {
21+
return
22+
}
23+
f, err := os.OpenFile(envLogFile,
1924
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
2025
if err != nil {
2126
return

go.mod

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,23 @@ require (
88
github.com/spf13/cobra v1.5.0
99
)
1010

11+
require (
12+
github.com/cockroachdb/errors v1.9.0 // indirect
13+
github.com/cockroachdb/logtags v0.0.0-20211118104740-dabe8e521a4f // indirect
14+
github.com/cockroachdb/redact v1.1.3 // indirect
15+
github.com/getsentry/sentry-go v0.13.0 // indirect
16+
github.com/gogo/protobuf v1.3.2 // indirect
17+
github.com/kr/pretty v0.3.0 // indirect
18+
github.com/kr/text v0.2.0 // indirect
19+
github.com/pkg/errors v0.9.1 // indirect
20+
github.com/rogpeppe/go-internal v1.9.0 // indirect
21+
github.com/sourcegraph/sourcegraph/lib v0.0.0-20221216004406-749998a2ac74 // indirect
22+
)
23+
1124
require (
1225
github.com/inconshreveable/mousetrap v1.0.0 // indirect
26+
github.com/sourcegraph/conc v0.2.0
1327
github.com/spf13/pflag v1.0.5 // indirect
1428
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
15-
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 // indirect
29+
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
1630
)

0 commit comments

Comments
 (0)