lf/main.go

99 lines
2.2 KiB
Go
Raw Normal View History

2016-08-13 12:49:04 +00:00
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"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
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-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() {
showDoc := flag.Bool("doc", false, "show documentation")
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)")
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()
if *showDoc {
2016-09-15 14:08:05 +00:00
fmt.Print(genDocString)
return
}
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 {
log.Fatalf("could not create CPU profile: %s", err)
2016-09-01 20:49:56 +00:00
}
if err := pprof.StartCPUProfile(f); err != nil {
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()
}
}