Skip to content

Commit 7348841

Browse files
committed
drop subcommands in favor of simpler flags
1 parent dcf0e51 commit 7348841

File tree

3 files changed

+35
-54
lines changed

3 files changed

+35
-54
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/progrium/topframe
33
go 1.16
44

55
require (
6-
github.com/google/subcommands v1.2.0
76
github.com/progrium/macdriver v0.0.2
87
github.com/progrium/watcher v1.0.7
98
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
2-
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
31
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
42
github.com/manifold/qtalk v0.0.0-20201222233608-81c04ab41f37/go.mod h1:UX1KjRclAJyV+ydrpEO1CnF8x95Le/Adx8BMGPJaYqg=
53
github.com/mitchellh/mapstructure v1.4.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=

topframe.go

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"bufio"
5-
"context"
5+
"embed"
66
"flag"
77
"fmt"
88
"io"
@@ -19,9 +19,6 @@ import (
1919
"text/template"
2020
"time"
2121

22-
"embed"
23-
24-
"github.com/google/subcommands"
2522
"github.com/progrium/macdriver/cocoa"
2623
"github.com/progrium/macdriver/core"
2724
"github.com/progrium/macdriver/objc"
@@ -42,42 +39,40 @@ func init() {
4239
}
4340

4441
func main() {
45-
subcommands.Register(&agentCmd{}, "")
46-
subcommands.Register(&docsCmd{}, "")
47-
subcommands.Register(&versionCmd{}, "")
48-
subcommands.Register(subcommands.HelpCommand(), "")
49-
42+
var (
43+
flagHelp = flag.Bool("help", false, "show help")
44+
flagHelpShort = flag.Bool("h", false, "show help")
45+
flagVersion = flag.Bool("version", false, "show version")
46+
flagVersionShort = flag.Bool("v", false, "show help")
47+
flagDocs = flag.Bool("docs", false, "open documentation in browser")
48+
flagPlist = flag.Bool("plist", false, "generate launch agent plist")
49+
)
5050
flag.Parse()
5151

52-
status := subcommands.Execute(context.Background())
53-
os.Exit(int(status))
54-
}
55-
56-
type agentCmd struct {
57-
plist bool
58-
}
59-
60-
func (*agentCmd) Name() string { return "agent" }
61-
func (*agentCmd) Synopsis() string { return "fullscreen webview overlay agent" }
62-
func (*agentCmd) Usage() string { return "Usage: topframe agent [-plist]\n" }
63-
func (c *agentCmd) SetFlags(f *flag.FlagSet) {
64-
f.BoolVar(&c.plist, "plist", false, "generate agent plist")
65-
}
52+
if *flagHelp || *flagHelpShort {
53+
printHelp()
54+
return
55+
}
56+
if *flagVersion || *flagVersionShort {
57+
fmt.Println(version)
58+
return
59+
}
60+
if *flagDocs {
61+
fatal(exec.Command("open", docsURL).Run())
62+
return
63+
}
6664

67-
func (c *agentCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
6865
dir := ensureDir()
6966

70-
if c.plist {
67+
if *flagPlist {
7168
generatePlist(dir)
72-
return subcommands.ExitSuccess
69+
return
7370
}
7471

7572
addr := startServer(dir)
7673
fw := startWatcher(dir)
7774

7875
runApp(dir, addr, fw)
79-
80-
return subcommands.ExitSuccess
8176
}
8277

8378
func ensureDir() (dir string) {
@@ -104,7 +99,7 @@ func ensureDir() (dir string) {
10499
}
105100

106101
func generatePlist(dir string) {
107-
tmpl, err := template.New("plist").ParseFS(data, "data/agent.plist")
102+
tmpl, err := template.New("plist").Parse(string(mustReadFile(data, "data/agent.plist")))
108103
fatal(err)
109104

110105
p, err := exec.LookPath(os.Args[0])
@@ -268,28 +263,6 @@ func runApp(dir string, addr *net.TCPAddr, fw *watcher.Watcher) {
268263
app.Run()
269264
}
270265

271-
type docsCmd struct{}
272-
273-
func (*docsCmd) Name() string { return "docs" }
274-
func (*docsCmd) Synopsis() string { return "open documentation in browser" }
275-
func (*docsCmd) Usage() string { return "Usage: topframe docs\n" }
276-
func (*docsCmd) SetFlags(f *flag.FlagSet) {}
277-
func (p *docsCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
278-
fatal(exec.Command("open", docsURL).Run())
279-
return subcommands.ExitSuccess
280-
}
281-
282-
type versionCmd struct{}
283-
284-
func (*versionCmd) Name() string { return "version" }
285-
func (*versionCmd) Synopsis() string { return "show version" }
286-
func (*versionCmd) Usage() string { return "Usage: topframe version\n" }
287-
func (*versionCmd) SetFlags(f *flag.FlagSet) {}
288-
func (p *versionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
289-
fmt.Println(version)
290-
return subcommands.ExitSuccess
291-
}
292-
293266
func streamExecScript(w http.ResponseWriter, dirpath string, args []string) {
294267
flusher, ok := w.(http.Flusher)
295268
if !ok || !isExecScript(dirpath) {
@@ -345,6 +318,17 @@ func mustReadFile(fs embed.FS, name string) []byte {
345318
return b
346319
}
347320

321+
func printHelp() {
322+
fmt.Printf("Usage: topframe <flags>\n")
323+
fmt.Printf("Topframe is a fullscreen webview overlay agent\n\n")
324+
fmt.Printf("Flags:\n")
325+
flag.VisitAll(func(f *flag.Flag) {
326+
if len(f.Name) > 1 {
327+
fmt.Printf(" -%-10s %s\n", f.Name, f.Usage)
328+
}
329+
})
330+
}
331+
348332
func fatal(err error) {
349333
if err != nil {
350334
log.Fatal(err)

0 commit comments

Comments
 (0)