2016-08-13 12:49:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2016-09-06 20:05:18 +00:00
|
|
|
"path/filepath"
|
2016-09-01 20:49:56 +00:00
|
|
|
"runtime/pprof"
|
2016-08-13 12:49:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-08-06 08:05:46 +00:00
|
|
|
envPath = os.Getenv("PATH")
|
2016-08-13 12:49:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-17 21:47:37 +00:00
|
|
|
gClientID int
|
2017-08-06 08:05:46 +00:00
|
|
|
gHostname string
|
2016-08-14 12:45:24 +00:00
|
|
|
gLastDirPath string
|
2016-08-13 12:49:04 +00:00
|
|
|
gSelectionPath string
|
2017-07-20 22:26:36 +00:00
|
|
|
gSocketProt string
|
2016-08-13 12:49:04 +00:00
|
|
|
gSocketPath string
|
|
|
|
gLogPath string
|
|
|
|
gServerLogPath string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-08-06 08:05:46 +00:00
|
|
|
var err error
|
2016-08-13 12:49:04 +00:00
|
|
|
|
2017-08-06 08:05:46 +00:00
|
|
|
gHostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("hostname: %s", err)
|
2016-11-05 21:08:51 +00:00
|
|
|
}
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startServer() {
|
|
|
|
cmd := exec.Command(os.Args[0], "-server")
|
2016-08-17 20:28:42 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
2016-08-17 20:06:45 +00:00
|
|
|
log.Printf("starting server: %s", err)
|
2016-08-13 12:49:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-09-13 21:40:14 +00:00
|
|
|
showDoc := flag.Bool("doc", false, "show documentation")
|
2016-12-19 18:28:57 +00:00
|
|
|
remoteCmd := flag.String("remote", "", "send remote command to server")
|
2016-08-13 12:49:04 +00:00
|
|
|
serverMode := flag.Bool("server", false, "start server (automatic)")
|
2016-09-01 20:49:56 +00:00
|
|
|
cpuprofile := flag.String("cpuprofile", "", "path to the file to write the cpu profile")
|
2016-08-14 12:45:24 +00:00
|
|
|
flag.StringVar(&gLastDirPath, "last-dir-path", "", "path to the file to write the last dir on exit (to use for cd)")
|
2017-05-14 15:22:46 +00:00
|
|
|
flag.StringVar(&gSelectionPath, "selection-path", "", "path to the file to write selected files on open (to use as open file dialog)")
|
2016-08-13 12:49:04 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2016-09-13 21:40:14 +00:00
|
|
|
if *showDoc {
|
2016-09-15 14:08:05 +00:00
|
|
|
fmt.Print(genDocString)
|
2016-09-13 21:40:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:28:57 +00:00
|
|
|
if *remoteCmd != "" {
|
|
|
|
if err := sendRemote(*remoteCmd); err != nil {
|
|
|
|
log.Fatalf("remote command: %s", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-01 20:49:56 +00:00
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
2016-12-19 18:28:57 +00:00
|
|
|
log.Fatalf("could not create CPU profile: %s", err)
|
2016-09-01 20:49:56 +00:00
|
|
|
}
|
|
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
2016-12-19 18:28:57 +00:00
|
|
|
log.Fatalf("could not start CPU profile: %s", err)
|
2016-09-01 20:49:56 +00:00
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2017-08-06 08:05:46 +00:00
|
|
|
gSocketProt = gDefaultSocketProt
|
|
|
|
gSocketPath = gDefaultSocketPath
|
|
|
|
|
2016-08-13 12:49:04 +00:00
|
|
|
if *serverMode {
|
2017-08-06 08:05:46 +00:00
|
|
|
gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username))
|
2016-08-13 12:49:04 +00:00
|
|
|
serve()
|
|
|
|
} else {
|
|
|
|
// TODO: check if the socket is working
|
2016-08-17 20:06:45 +00:00
|
|
|
if _, err := os.Stat(gSocketPath); os.IsNotExist(err) {
|
2016-08-13 12:49:04 +00:00
|
|
|
startServer()
|
|
|
|
}
|
|
|
|
|
2017-08-06 08:05:46 +00:00
|
|
|
gClientID = 1000
|
|
|
|
gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID))
|
|
|
|
for _, err := os.Stat(gLogPath); err == nil; _, err = os.Stat(gLogPath) {
|
|
|
|
gClientID++
|
|
|
|
gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID))
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:49:04 +00:00
|
|
|
client()
|
|
|
|
}
|
|
|
|
}
|