Skip to content
This repository was archived by the owner on Jan 17, 2021. It is now read-only.

Commit 304ae28

Browse files
committed
Use go.coder.com/cli for argument/flag parsing
1 parent 2f522f5 commit 304ae28

File tree

3 files changed

+56
-60
lines changed

3 files changed

+56
-60
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
77
github.com/pkg/errors v0.8.1 // indirect
88
github.com/stretchr/testify v1.3.0
9+
go.coder.com/cli v0.1.0
910
go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8
1011
go.coder.com/retry v0.0.0-20180926062817-cf12c95974ac
1112
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
1717
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
1818
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
1919
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
20+
go.coder.com/cli v0.1.0 h1:ZAjpjXJxMnwj1TqXUi7nnXXuxiPRfwfoC2kViN93oMM=
21+
go.coder.com/cli v0.1.0/go.mod h1:pbVagI9YH/HHMManxPFML4P527GDREwsb+yciZ7mtB8=
2022
go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8 h1:PtQ3moPi4EAz3cyQhkUs1IGIXa2QgJpP60yMjOdu0kk=
2123
go.coder.com/flog v0.0.0-20190129195112-eaed154a0db8/go.mod h1:83JsYgXYv0EOaXjIMnaZ1Fl6ddNB3fJnDZ/8845mUJ8=
2224
go.coder.com/retry v0.0.0-20180926062817-cf12c95974ac h1:ekdpsuykRy/E+SDq5BquFomNhRCk8OOyhtnACW9Bi50=

main.go

+53-60
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"math/rand"
77
"os"
88
"strings"
9-
"text/tabwriter"
109
"time"
1110

11+
"go.coder.com/cli"
1212
"go.coder.com/flog"
1313
)
1414

@@ -18,98 +18,91 @@ func init() {
1818

1919
const helpTabWidth = 5
2020

21-
var helpTab = strings.Repeat(" ", helpTabWidth)
22-
23-
// version is overwritten by ci/build.sh.
24-
var version string
21+
var (
22+
helpTab = strings.Repeat(" ", helpTabWidth)
23+
// version is overwritten by ci/build.sh.
24+
version string
25+
)
2526

2627
func main() {
27-
var (
28-
skipSyncFlag = flag.Bool("skipsync", false, "skip syncing local settings and extensions to remote host")
29-
sshFlags = flag.String("ssh-flags", "", "custom SSH flags")
30-
syncBack = flag.Bool("b", false, "sync extensions back on termination")
31-
printVersion = flag.Bool("version", false, "print version information and exit")
32-
)
28+
cli.RunRoot(&rootCmd{})
29+
}
30+
31+
var _ interface {
32+
cli.Command
33+
cli.FlaggedCommand
34+
} = new(rootCmd)
3335

34-
flag.Usage = usage
36+
type rootCmd struct {
37+
skipSync bool
38+
syncBack bool
39+
printVersion bool
40+
sshFlags string
41+
}
42+
43+
func (c *rootCmd) Spec() cli.CommandSpec {
44+
return cli.CommandSpec{
45+
Name: "sshcode",
46+
Usage: c.usage(),
47+
Desc: c.description(),
48+
}
49+
}
50+
51+
func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) {
52+
fl.BoolVar(&c.skipSync, "skipsync", false, "skip syncing local settings and extensions to remote host")
53+
fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination")
54+
fl.BoolVar(&c.printVersion, "version", false, "print version information and exit")
55+
fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags")
56+
}
3557

36-
flag.Parse()
37-
if *printVersion {
58+
func (c *rootCmd) Run(fl *flag.FlagSet) {
59+
if c.printVersion {
3860
fmt.Printf("%v\n", version)
3961
os.Exit(0)
4062
}
4163

42-
host := flag.Arg(0)
43-
64+
host := fl.Arg(0)
4465
if host == "" {
4566
// If no host is specified output the usage.
46-
flag.Usage()
67+
fl.Usage()
4768
os.Exit(1)
4869
}
4970

50-
dir := flag.Arg(1)
71+
dir := fl.Arg(1)
5172
if dir == "" {
5273
dir = "~"
5374
}
5475

5576
err := sshCode(host, dir, options{
56-
skipSync: *skipSyncFlag,
57-
sshFlags: *sshFlags,
58-
syncBack: *syncBack,
77+
skipSync: c.skipSync,
78+
sshFlags: c.sshFlags,
79+
syncBack: c.syncBack,
5980
})
6081

6182
if err != nil {
6283
flog.Fatal("error: %v", err)
6384
}
6485
}
6586

66-
func usage() {
67-
fmt.Printf(`Usage: %v [FLAGS] HOST [DIR]
68-
Start VS Code via code-server over SSH.
87+
func (c *rootCmd) usage() string {
88+
return "[FLAGS] HOST [DIR]"
89+
}
90+
91+
func (c *rootCmd) description() string {
92+
return fmt.Sprintf(`Start VS Code via code-server over SSH.
6993
7094
Environment variables:
71-
%v use special VS Code settings dir.
72-
%v use special VS Code extensions dir.
95+
%v%v use special VS Code settings dir.
96+
%v%v use special VS Code extensions dir.
7397
7498
More info: https://github.com/cdr/sshcode
7599
76100
Arguments:
77101
%vHOST is passed into the ssh command. Valid formats are '<ip-address>' or 'gcp:<instance-name>'.
78-
%vDIR is optional.
79-
80-
%v`,
81-
os.Args[0],
82-
vsCodeConfigDirEnv,
83-
vsCodeExtensionsDirEnv,
102+
%vDIR is optional.`,
103+
helpTab, vsCodeConfigDirEnv,
104+
helpTab, vsCodeExtensionsDirEnv,
84105
helpTab,
85106
helpTab,
86-
flagHelp(),
87107
)
88-
89-
}
90-
91-
// flagHelp generates a friendly help string for all globally registered command
92-
// line flags.
93-
func flagHelp() string {
94-
var bd strings.Builder
95-
96-
w := tabwriter.NewWriter(&bd, 3, 10, helpTabWidth, ' ', 0)
97-
98-
fmt.Fprintf(w, "Flags:\n")
99-
var count int
100-
flag.VisitAll(func(f *flag.Flag) {
101-
count++
102-
if f.DefValue == "" {
103-
fmt.Fprintf(w, "\t-%v\t%v\n", f.Name, f.Usage)
104-
} else {
105-
fmt.Fprintf(w, "\t-%v\t%v\t(%v)\n", f.Name, f.Usage, f.DefValue)
106-
}
107-
})
108-
if count == 0 {
109-
return "\n"
110-
}
111-
112-
w.Flush()
113-
114-
return bd.String()
115108
}

0 commit comments

Comments
 (0)