forked from gokcehan/lf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
310 lines (260 loc) · 5.91 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"reflect"
"runtime"
"runtime/pprof"
"strconv"
"strings"
)
var (
envPath = os.Getenv("PATH")
envLevel = os.Getenv("LF_LEVEL")
)
type arrayFlag []string
var (
gSingleMode bool
gClientID int
gHostname string
gLastDirPath string
gSelectionPath string
gSocketProt string
gSocketPath string
gLogPath string
gServerLogPath string
gSelect string
gConfigPath string
gCommands arrayFlag
gVersion string
)
func (a *arrayFlag) Set(v string) error {
*a = append(*a, v)
return nil
}
func (a *arrayFlag) String() string {
return strings.Join(*a, ", ")
}
func init() {
h, err := os.Hostname()
if err != nil {
log.Printf("hostname: %s", err)
}
gHostname = h
if envLevel == "" {
envLevel = "0"
}
}
func exportEnvVars() {
os.Setenv("id", strconv.Itoa(gClientID))
os.Setenv("OPENER", envOpener)
os.Setenv("EDITOR", envEditor)
os.Setenv("PAGER", envPager)
os.Setenv("SHELL", envShell)
dir, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
}
os.Setenv("OLDPWD", dir)
level, err := strconv.Atoi(envLevel)
if err != nil {
log.Printf("reading lf level: %s", err)
}
level++
os.Setenv("LF_LEVEL", strconv.Itoa(level))
}
// used by exportOpts below
func fieldToString(field reflect.Value) string {
kind := field.Kind()
var value string
switch kind {
case reflect.Int:
value = strconv.Itoa(int(field.Int()))
case reflect.Bool:
value = strconv.FormatBool(field.Bool())
case reflect.Slice:
for i := 0; i < field.Len(); i++ {
element := field.Index(i)
if i == 0 {
value = fieldToString(element)
} else {
value += ":" + fieldToString(element)
}
}
default:
value = field.String()
}
return value
}
func exportOpts() {
e := reflect.ValueOf(&gOpts).Elem()
for i := 0; i < e.NumField(); i++ {
// Get name and prefix it with lf_
name := e.Type().Field(i).Name
name = fmt.Sprintf("lf_%s", name)
// Skip maps
if name == "lf_keys" || name == "lf_cmdkeys" || name == "lf_cmds" {
continue
}
// Get string representation of the value
if name == "lf_sortType" {
var sortby string
switch gOpts.sortType.method {
case naturalSort:
sortby = "natural"
case nameSort:
sortby = "name"
case sizeSort:
sortby = "size"
case timeSort:
sortby = "time"
case ctimeSort:
sortby = "ctime"
case atimeSort:
sortby = "atime"
case extSort:
sortby = "ext"
}
os.Setenv("lf_sortby", sortby)
reverse := strconv.FormatBool(gOpts.sortType.option&reverseSort != 0)
os.Setenv("lf_reverse", reverse)
hidden := strconv.FormatBool(gOpts.sortType.option&hiddenSort != 0)
os.Setenv("lf_hidden", hidden)
dirfirst := strconv.FormatBool(gOpts.sortType.option&dirfirstSort != 0)
os.Setenv("lf_dirfirst", dirfirst)
} else {
field := e.Field(i)
value := fieldToString(field)
os.Setenv(name, value)
}
}
}
func startServer() {
cmd := detachedCommand(os.Args[0], "-server")
if err := cmd.Start(); err != nil {
log.Printf("starting server: %s", err)
}
}
func checkServer() {
if gSocketProt == "unix" {
if _, err := os.Stat(gSocketPath); os.IsNotExist(err) {
startServer()
} else if _, err := net.Dial(gSocketProt, gSocketPath); err != nil {
os.Remove(gSocketPath)
startServer()
}
} else {
if _, err := net.Dial(gSocketProt, gSocketPath); err != nil {
startServer()
}
}
}
func main() {
showDoc := flag.Bool(
"doc",
false,
"show documentation")
showVersion := flag.Bool(
"version",
false,
"show version")
serverMode := flag.Bool(
"server",
false,
"start server (automatic)")
singleMode := flag.Bool(
"single",
false,
"start a client without server")
remoteCmd := flag.String(
"remote",
"",
"send remote command to server")
cpuprofile := flag.String(
"cpuprofile",
"",
"path to the file to write the CPU profile")
memprofile := flag.String(
"memprofile",
"",
"path to the file to write the memory profile")
flag.StringVar(&gLastDirPath,
"last-dir-path",
"",
"path to the file to write the last dir on exit (to use for cd)")
flag.StringVar(&gSelectionPath,
"selection-path",
"",
"path to the file to write selected files on open (to use as open file dialog)")
flag.StringVar(&gConfigPath,
"config",
"",
"path to the config file (instead of the usual paths)")
flag.Var(&gCommands,
"command",
"command to execute on client initialization")
flag.Parse()
gSocketProt = gDefaultSocketProt
gSocketPath = gDefaultSocketPath
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatalf("could not create CPU profile: %s", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatalf("could not start CPU profile: %s", err)
}
defer pprof.StopCPUProfile()
}
switch {
case *showDoc:
fmt.Print(genDocString)
case *showVersion:
fmt.Println(gVersion)
case *remoteCmd != "":
if err := remote(*remoteCmd); err != nil {
log.Fatalf("remote command: %s", err)
}
case *serverMode:
os.Chdir(gUser.HomeDir)
gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username))
serve()
default:
gSingleMode = *singleMode
if !gSingleMode {
checkServer()
}
gClientID = os.Getpid()
gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID))
switch flag.NArg() {
case 0:
_, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(2)
}
case 1:
gSelect = flag.Arg(0)
default:
fmt.Fprintf(os.Stderr, "only single file or directory is allowed\n")
os.Exit(2)
}
exportEnvVars()
run()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}
}