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
115 lines (90 loc) · 2.06 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"strings"
"text/tabwriter"
"time"
"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.
var version string
func main() {
var (
skipSyncFlag = flag.Bool("skipsync", false, "skip syncing local settings and extensions to remote host")
sshFlags = flag.String("ssh-flags", "", "custom SSH flags")
syncBack = flag.Bool("b", false, "sync extensions back on termination")
printVersion = flag.Bool("version", false, "print version information and exit")
)
flag.Usage = usage
flag.Parse()
if *printVersion {
fmt.Printf("%v\n", version)
os.Exit(0)
}
host := flag.Arg(0)
if host == "" {
// If no host is specified output the usage.
flag.Usage()
os.Exit(1)
}
dir := flag.Arg(1)
if dir == "" {
dir = "~"
}
err := sshCode(host, dir, options{
skipSync: *skipSyncFlag,
sshFlags: *sshFlags,
syncBack: *syncBack,
})
if err != nil {
flog.Fatal("error: %v", err)
}
}
func usage() {
fmt.Printf(`Usage: %v [FLAGS] HOST [DIR]
Start VS Code via code-server over SSH.
Environment variables:
%v use special VS Code settings dir.
%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.
%v`,
os.Args[0],
vsCodeConfigDirEnv,
vsCodeExtensionsDirEnv,
helpTab,
helpTab,
flagHelp(),
)
}
// flagHelp generates a friendly help string for all globally registered command
// line flags.
func flagHelp() string {
var bd strings.Builder
w := tabwriter.NewWriter(&bd, 3, 10, helpTabWidth, ' ', 0)
fmt.Fprintf(w, "Flags:\n")
var count int
flag.VisitAll(func(f *flag.Flag) {
count++
if f.DefValue == "" {
fmt.Fprintf(w, "\t-%v\t%v\n", f.Name, f.Usage)
} else {
fmt.Fprintf(w, "\t-%v\t%v\t(%v)\n", f.Name, f.Usage, f.DefValue)
}
})
if count == 0 {
return "\n"
}
w.Flush()
return bd.String()
}