-
Notifications
You must be signed in to change notification settings - Fork 20
feat: handle stdin piped input #71
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
Conversation
I was thinking in this use case. For example an user checking a list of IP addresses/URLs domains. I can't see why an user would use this feature? What use case do you have in mind? |
A ok now I understand thank u for feedback I will fix this |
I updated the program to read from stdin, parse URLs, added testing, and modified Probe.Do to accept an array of addresses or use defaults if empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you (again) :).
internal/probe.go
Outdated
"Sending report back for address", | ||
"count", count, "address", addr, "report", report, | ||
) | ||
p.ReportCh <- &report |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need another sleep
here. Isn't it?
main.go
Outdated
@@ -45,7 +43,14 @@ func main() { | |||
Level: lvl, | |||
})) | |||
var opts internal.Options | |||
stdin, err := internal.ReadStdin() | |||
if err != nil { | |||
fatal(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please enrich the error: fatal(fmt.Errorf("reading stdin: %w", err))
internal/utils_test.go
Outdated
got, _ := ReadStdin() | ||
addrs := ProcessAddrs(got) | ||
|
||
if !reflect.DeepEqual(addrs, tt.wantAddrs) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please lets avoid reflect
and use a simple for
.
- Moved regex patterns out of function definitions. - Changed the test input structure and migrated example-based tests to standard Go tests. - Change the utils.go to stdin.go - Fixed the probe function to sleep properly. - Renamed the 'address' variable to 'input' for clarity. - Added a line comment to describe the 'input' field for better documentation.
Fixed the issue i think it's ready to merge details in commit message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, only some details more and we are ready to go :).
internal/probe.go
Outdated
} | ||
} | ||
p.Logger.Debug( | ||
"Iteration finished", "count", count, "p.Count", p.Count, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be one line
internal/probe.go
Outdated
@@ -20,6 +20,8 @@ type Probe struct { | |||
Logger *slog.Logger | |||
// Channel to send back partial results. | |||
ReportCh chan *Report | |||
//URLs (HTTP), host/port strings (TCP) or domains (DNS). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an space after //
internal/stdin.go
Outdated
if err != nil { | ||
return "", fmt.Errorf("failed to retrieve stdin information: %w", err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line.
internal/stdin.go
Outdated
if (info.Mode() & os.ModeCharDevice) != 0 { | ||
return "", nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line.
internal/stdin.go
Outdated
if err != nil { | ||
return "", fmt.Errorf("reading from stdin: %w", err) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line.
internal/stdin.go
Outdated
) | ||
|
||
func validateInput(addr string) bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty lines.
internal/stdin.go
Outdated
func ProcessInputs(s string) ([]string, error) { | ||
var inputs []string | ||
var errFormats []error | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty lines.
internal/stdin_test.go
Outdated
{"invalid@domain", false}, | ||
{"not-a-domain", false}, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line.
internal/stdin_test.go
Outdated
wantAddrs: []string{}, | ||
}, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty lines.
main.go
Outdated
opts.Parse() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line.
internal/stdin.go
Outdated
func ReadStdin() (string, error) { | ||
info, err := os.Stdin.Stat() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to retrieve stdin information: %w", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
retrieving stdin
is enough
internal/stdin_test.go
Outdated
w.Close() | ||
|
||
got, _ := ReadStdin() | ||
addrs, _ := ProcessInputs(got) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inputs
internal/stdin_test.go
Outdated
{ | ||
name: "single valid url", | ||
input: "http://192.168.1.1\n", | ||
wantAddrs: []string{"http://192.168.1.1"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wantInput?
main.go
Outdated
} | ||
inputs, err := internal.ProcessInputs(stdin) | ||
if err != nil { | ||
fmt.Printf("failed to process the inputs:\n%v\n", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
processing inputs
and use fatal
main.go
Outdated
@@ -125,8 +133,13 @@ func main() { | |||
} | |||
} | |||
}() | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line
main.go
Outdated
if err != nil { | ||
fatal(fmt.Errorf("running probe: %w", err)) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty line
main.go
Outdated
if err != nil { | ||
fatal(fmt.Errorf("running probe: %w", err)) | ||
} | ||
|
||
if err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repeated lines?
internal/stdin.go
Outdated
} | ||
return false | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment
domainRegex = regexp.MustCompile(domainPattern) | ||
) | ||
|
||
func validateInput(addr string) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment: Ensures the inputs are correct
internal/stdin_test.go
Outdated
w.Write([]byte(tt.input)) | ||
w.Close() | ||
|
||
got, _ := ReadStdin() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to capture the errors and check them here.
internal/stdin.go
Outdated
if validateInput(input) { | ||
inputs = append(inputs, input) | ||
} else { | ||
errFormats = append(errFormats, fmt.Errorf("invalid address format: %s", input)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return an error and use fatal
in the caller.
Added functionality to handle input via STDIN.
If STDIN input is available, it is read and used to set the DNS resolver.
If no STDIN input is detected, the program falls back to parsing command-line arguments.
Errors encountered while reading from STDIN are logged as fatal.
I can add also the validation for the right DNS string if u want .
Closes #39