lf/main.go

116 lines
2.6 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 (
2016-09-02 20:04:26 +00:00
envUser = os.Getenv("USER")
envHome = os.Getenv("HOME")
envHost = os.Getenv("HOSTNAME")
envPath = os.Getenv("PATH")
envConfig = os.Getenv("XDG_CONFIG_HOME")
2016-08-13 12:49:04 +00:00
)
var (
2016-12-17 21:47:37 +00:00
gClientID int
2016-08-14 12:45:24 +00:00
gLastDirPath string
2016-08-13 12:49:04 +00:00
gSelectionPath string
gSocketPath string
gLogPath string
gServerLogPath string
gConfigPath string
)
func init() {
if envUser == "" {
log.Print("$USER not set")
2016-08-13 12:49:04 +00:00
}
if envHome == "" {
envHome = "/home/" + envUser
}
if envHost == "" {
host, err := os.Hostname()
if err != nil {
2016-08-17 20:06:45 +00:00
log.Printf("hostname: %s", err)
2016-08-13 12:49:04 +00:00
}
envHost = host
}
2016-09-02 20:04:26 +00:00
if envConfig == "" {
envConfig = filepath.Join(envHome, ".config")
2016-09-02 20:04:26 +00:00
}
2016-08-13 12:49:04 +00:00
tmp := os.TempDir()
gSocketPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.sock", envUser))
2016-08-13 12:49:04 +00:00
2016-12-17 21:47:37 +00:00
gClientID = 1000
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientID))
for _, err := os.Stat(gLogPath); err == nil; _, err = os.Stat(gLogPath) {
2016-12-17 21:47:37 +00:00
gClientID++
gLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.%d.log", envUser, gClientID))
}
gServerLogPath = filepath.Join(tmp, fmt.Sprintf("lf.%s.server.log", envUser))
2016-08-13 12:49:04 +00:00
gConfigPath = filepath.Join(envConfig, "lf", "lfrc")
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)")
2016-08-13 12:49:04 +00:00
flag.StringVar(&gSelectionPath, "selection-path", "", "path to the file to write selected files on exit (to use as open file dialog)")
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()
}
2016-08-13 12:49:04 +00:00
if *serverMode {
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()
}
client()
}
}