Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refactor) cmd refactor #22

Merged
merged 6 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func wantsVersion() { 
-         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) 
-          }
+        if  len(os.Args) != 2 {
+                  return
+         }
+         switch os.Args[1] {
+               case "-v", "--v", "-version", "--version":
+            fmt.Println(version) 
+                os.Exit(0) 
+         }
}

(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
20 changes: 20 additions & 0 deletions pkg/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,23 @@ func sshFilesByPattern(pattern string, config *SSHConfig) ([]string, error) {
filePaths := buf.String()
return strings.Split(strings.TrimSpace(filePaths), "\n"), nil
}

func UniqueFileInfos(fileInfos []FileInfo) []FileInfo {
kevincobain2000 marked this conversation as resolved.
Show resolved Hide resolved
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
}

// func UniqueFileInfos(fileInfos []FileInfo) []FileInfo {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You kept them here for a later use ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed them. I tested the asis and Tobe first, as there were no tests for that.

// eq := func(a, b FileInfo) bool {
// return a.FilePath == b.FilePath && a.Type == b.Type && a.Host == b.Host
// }
// return slices.CompactFunc(fileInfos, eq)
// }
14 changes: 14 additions & 0 deletions pkg/slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pkg

type SliceFlags []string

// provide a String() method on the type so we can use it with flag.Var
func (i *SliceFlags) String() string {
kevincobain2000 marked this conversation as resolved.
Show resolved Hide resolved
return ""
}

// provide a Set() method on the type so we can use it with flag.Var
func (i *SliceFlags) Set(value string) error {
kevincobain2000 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner Author

@kevincobain2000 kevincobain2000 Jun 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be append, reverting back from append -> set

Reason:

./main.go:201:11: cannot use &f.filePaths (value of type *"github.com/kevincobain2000/gol/pkg".SliceFlags) as flag.Value value in argument to flag.Var: *"github.com/kevincobain2000/gol/pkg".SliceFlags does not implement flag.Value (missing method Set)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, in such case, I always try to add a comment like:

// required to implement flag.Value interface

*i = append(*i, value)
return nil
}
12 changes: 12 additions & 0 deletions pkg/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@ func HandleCltrC(f func()) {
os.Exit(1)
}()
}

func Cleanup() {
if GlobalPipeTmpFilePath == "" {
return
}
err := os.Remove(GlobalPipeTmpFilePath)
if err != nil {
slog.Error("removing temp file", GlobalPipeTmpFilePath, err)
return
}
slog.Info("temp file removed", "path", GlobalPipeTmpFilePath)
}
14 changes: 14 additions & 0 deletions pkg/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ func TestIsInputFromPipe(t *testing.T) {
t.Error("Expected IsInputFromPipe to return true")
}
}

// func TestUniqueFileInfos(t *testing.T)
// {
// GlobalFilePaths = []FileInfo{
// {FilePath: "/tmp/file1"},
// {FilePath: "/tmp/file2"},
// {FilePath: "/tmp/file1"},
// }

// uniqueFileInfos := UniqueFileInfos(GlobalFilePaths)
// if len(uniqueFileInfos) != 2 {
// t.Errorf("Expected 2 unique file infos, got %d", len(uniqueFileInfos))
// }
// }
Loading