This repository was archived by the owner on Jan 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathmain.go
144 lines (121 loc) · 3.05 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"os/user"
"path/filepath"
"runtime"
"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 = "~"
}
// Get linux relative path if on windows
if runtime.GOOS == "windows" {
dir = relativeWindowsPath(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,
)
}
func relativeWindowsPath(dir string) string {
usr, err := user.Current()
if err != nil {
flog.Error("Could not get user: %v", err)
return dir
}
rel, err := filepath.Rel(usr.HomeDir, dir)
if err != nil {
return dir
}
rel = "~/" + filepath.ToSlash(rel)
return rel
}
// gitbashWindowsDir translates a directory similar to `C:\Users\username\path` to `~/path` for compatibility with Git bash.
func gitbashWindowsDir(dir string) (res string) {
res = filepath.ToSlash(dir)
res = strings.Replace(res, "C:", "", -1)
//res2 =
return res
}