forked from coder/sshcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (94 loc) · 2.37 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"strings"
"time"
"go.coder.com/cli"
"go.coder.com/flog"
)
func init() {
rand.Seed(time.Now().Unix())
}
const helpTabWidth = 5
var (
helpTab = strings.Repeat(" ", helpTabWidth)
// version is overwritten by ci/build.sh.
version string
)
func main() {
cli.RunRoot(&rootCmd{})
}
var _ interface {
cli.Command
cli.FlaggedCommand
} = new(rootCmd)
type rootCmd struct {
skipSync bool
syncBack bool
printVersion bool
noReuseConnection bool
bindAddr string
sshFlags string
}
func (c *rootCmd) Spec() cli.CommandSpec {
return cli.CommandSpec{
Name: "sshcode",
Usage: c.usage(),
Desc: c.description(),
}
}
func (c *rootCmd) RegisterFlags(fl *flag.FlagSet) {
fl.BoolVar(&c.skipSync, "skipsync", false, "skip syncing local settings and extensions to remote host")
fl.BoolVar(&c.syncBack, "b", false, "sync extensions back on termination")
fl.BoolVar(&c.printVersion, "version", false, "print version information and exit")
fl.BoolVar(&c.noReuseConnection, "no-reuse-connection", false, "do not reuse SSH connection via control socket")
fl.StringVar(&c.bindAddr, "bind", "", "local bind address for SSH tunnel, in [HOST][:PORT] syntax (default: 127.0.0.1)")
fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags")
}
func (c *rootCmd) Run(fl *flag.FlagSet) {
if c.printVersion {
fmt.Printf("%v\n", version)
os.Exit(0)
}
host := fl.Arg(0)
if host == "" {
// If no host is specified output the usage.
fl.Usage()
os.Exit(1)
}
dir := fl.Arg(1)
if dir == "" {
dir = "~"
}
err := sshCode(host, dir, options{
skipSync: c.skipSync,
sshFlags: c.sshFlags,
bindAddr: c.bindAddr,
syncBack: c.syncBack,
reuseConnection: !c.noReuseConnection,
})
if err != nil {
flog.Fatal("error: %v", err)
}
}
func (c *rootCmd) usage() string {
return "[FLAGS] HOST [DIR]"
}
func (c *rootCmd) description() string {
return fmt.Sprintf(`Start VS Code via code-server over SSH.
Environment variables:
%v%v use special VS Code settings dir.
%v%v use special VS Code extensions dir.
More info: https://github.com/cdr/sshcode
Arguments:
%vHOST is passed into the ssh command. Valid formats are '<ip-address>' or 'gcp:<instance-name>'.
%vDIR is optional.`,
helpTab, vsCodeConfigDirEnv,
helpTab, vsCodeExtensionsDirEnv,
helpTab,
helpTab,
)
}