diff --git a/main.go b/main.go index 2cad37c..4a5295d 100644 --- a/main.go +++ b/main.go @@ -15,16 +15,6 @@ import ( //go:embed all:frontend/dist/* var publicDir embed.FS -type sliceFlags []string - -func (i *sliceFlags) String() string { - return "my string representation" -} -func (i *sliceFlags) Set(value string) error { - *i = append(*i, value) - return nil -} - type Flags struct { host string port int64 @@ -32,9 +22,9 @@ type Flags struct { every int64 limit int baseURL string - filePaths sliceFlags - sshPaths sliceFlags - dockerPaths sliceFlags + filePaths pkg.SliceFlags + sshPaths pkg.SliceFlags + dockerPaths pkg.SliceFlags access bool open bool version bool @@ -47,7 +37,6 @@ var version = "dev" func main() { pkg.SetupLoggingStdout() flags() - wantsVersion() if pkg.IsInputFromPipe() { tmpFile, err := os.Create(pkg.GetTmpFileNameForSTDIN()) @@ -65,7 +54,7 @@ func main() { } }(tmpFile) } - defaultFilePaths() + setFilePaths() go watchFilePaths(f.every) slog.Info("Flags", "host", f.host, "port", f.port, "baseURL", f.baseURL, "open", f.open, "cors", f.cors, "access", f.access) @@ -73,8 +62,8 @@ func main() { if f.open { pkg.OpenBrowser(fmt.Sprintf("http://%s:%d%s", f.host, f.port, f.baseURL)) } - defer cleanup() - pkg.HandleCltrC(cleanup) + defer pkg.Cleanup() + pkg.HandleCltrC(pkg.Cleanup) err := pkg.NewEcho(func(o *pkg.EchoOptions) error { o.Host = f.host @@ -91,9 +80,9 @@ func main() { } } -func defaultFilePaths() { +func setFilePaths() { if len(os.Args) > 1 { - filePaths := sliceFlags{} + filePaths := pkg.SliceFlags{} for _, arg := range os.Args[1:] { if strings.HasPrefix(arg, "-") { filePaths = []string{} @@ -193,19 +182,7 @@ func updateGlobalFilePaths() { } } - pkg.GlobalFilePaths = uniqueFileInfos(fileInfos) -} - -func cleanup() { - slog.Info("cleaning up") - if pkg.GlobalPipeTmpFilePath != "" { - err := os.Remove(pkg.GlobalPipeTmpFilePath) - if err != nil { - slog.Error("removing temp file", pkg.GlobalPipeTmpFilePath, err) - return - } - slog.Info("temp file removed", "path", pkg.GlobalPipeTmpFilePath) - } + pkg.GlobalFilePaths = pkg.UniqueFileInfos(fileInfos) } func watchFilePaths(seconds int64) { @@ -220,19 +197,6 @@ func watchFilePaths(seconds int64) { } } -func uniqueFileInfos(fileInfos []pkg.FileInfo) []pkg.FileInfo { - keys := make(map[string]bool) - list := []pkg.FileInfo{} - for _, entry := range fileInfos { - key := entry.FilePath + entry.Type + entry.Host - if _, value := keys[key]; !value { - keys[key] = true - list = append(list, entry) - } - } - return list -} - func flags() { flag.Var(&f.filePaths, "f", "full path pattern to the log file") flag.Var(&f.sshPaths, "s", "full ssh path pattern to the log file") @@ -248,10 +212,14 @@ func flags() { flag.StringVar(&f.baseURL, "base-url", "/", "base url with slash") flag.Parse() + wantsVersion() } func wantsVersion() { - if f.version { + if len(os.Args) == 2 && + (os.Args[1] == "-version" || os.Args[1] == "--version" || + os.Args[1] == "-v" || os.Args[1] == "--v" || + os.Args[1] == "version") || f.version { fmt.Println(version) os.Exit(0) } diff --git a/pkg/files.go b/pkg/files.go index b8d4f03..d0d4e28 100644 --- a/pkg/files.go +++ b/pkg/files.go @@ -370,3 +370,16 @@ func sshFilesByPattern(pattern string, config *SSHConfig) ([]string, error) { filePaths := buf.String() return strings.Split(strings.TrimSpace(filePaths), "\n"), nil } + +func UniqueFileInfos(fileInfos []FileInfo) []FileInfo { + keys := make(map[string]bool) + list := []FileInfo{} + for _, entry := range fileInfos { + key := entry.FilePath + entry.Type + entry.Host + if _, value := keys[key]; !value { + keys[key] = true + list = append(list, entry) + } + } + return list +} diff --git a/pkg/slices.go b/pkg/slices.go new file mode 100644 index 0000000..103a88e --- /dev/null +++ b/pkg/slices.go @@ -0,0 +1,11 @@ +package pkg + +type SliceFlags []string + +func (i *SliceFlags) String() string { + return "" +} +func (i *SliceFlags) Set(value string) error { + *i = append(*i, value) + return nil +} diff --git a/pkg/system.go b/pkg/system.go index 0bca7ab..bfcb66a 100644 --- a/pkg/system.go +++ b/pkg/system.go @@ -123,3 +123,14 @@ func HandleCltrC(f func()) { os.Exit(1) }() } + +func Cleanup() { + if GlobalPipeTmpFilePath != "" { + err := os.Remove(GlobalPipeTmpFilePath) + if err != nil { + slog.Error("removing temp file", GlobalPipeTmpFilePath, err) + return + } + slog.Info("temp file removed", "path", GlobalPipeTmpFilePath) + } +}