Skip to content

Commit

Permalink
(refactor) cmd refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jun 29, 2024
1 parent c3a6cf7 commit da19f3b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
60 changes: 14 additions & 46 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,16 @@ 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
cors int64
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
Expand All @@ -47,7 +37,6 @@ var version = "dev"
func main() {
pkg.SetupLoggingStdout()
flags()
wantsVersion()

if pkg.IsInputFromPipe() {
tmpFile, err := os.Create(pkg.GetTmpFileNameForSTDIN())
Expand All @@ -65,16 +54,16 @@ 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)

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
Expand All @@ -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{}
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
Expand All @@ -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)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions pkg/slices.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions pkg/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit da19f3b

Please sign in to comment.