8000 feat: handle stdin piped input by DnFreddie · Pull Request #71 · jesusprubio/up · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Merged
merged 5 commits into from
Nov 20, 2024
Merged

Conversation

DnFreddie
Copy link
Contributor
@DnFreddie DnFreddie commented Nov 17, 2024

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

@jesusprubio
Copy link
Owner
jesusprubio commented Nov 17, 2024

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?

@DnFreddie
Copy link
Contributor Author

A ok now I understand thank u for feedback I will fix this

@DnFreddie
Copy link
Contributor Author

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.

Copy link
Owner
@jesusprubio jesusprubio left a comment

Choose a reason for hiding this comment

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

Thank you (again) :).

"Sending report back for address",
"count", count, "address", addr, "report", report,
)
p.ReportCh <- &report
Copy link
Owner

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)
Copy link
Owner

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))

got, _ := ReadStdin()
addrs := ProcessAddrs(got)

if !reflect.DeepEqual(addrs, tt.wantAddrs) {
Copy link
Owner

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.

@jesusprubio jesusprubio changed the title Fix #39: Handle STDIN piped input feat: handle stdin piped input Nov 18, 2024
- 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.
@DnFreddie
Copy link
Contributor Author

Fixed the issue i think it's ready to merge details in commit message

Copy link
Owner
@jesusprubio jesusprubio left a 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 :).

}
}
p.Logger.Debug(
"Iteration finished", "count", count, "p.Count", p.Count,
Copy link
Owner

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

@@ -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).
Copy link
Owner

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 //

if err != nil {
return "", fmt.Errorf("failed to retrieve stdin information: %w", err)
}

Copy link
Owner

Choose a reason for hiding this comment

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

Empty line.

if (info.Mode() & os.ModeCharDevice) != 0 {
return "", nil
}

Copy link
Owner

Choose a reason for hiding this comment

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

Empty line.

if err != nil {
return "", fmt.Errorf("reading from stdin: %w", err)
}

Copy link
Owner

Choose a reason for hiding this comment

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

Empty line.

)

func validateInput(addr string) bool {

Copy link
Owner

Choose a reason for hiding this comment

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

Empty lines.

func ProcessInputs(s string) ([]string, error) {
var inputs []string
var errFormats []error

Copy link
Owner

Choose a reason for hiding this comment

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

Empty lines.

{"invalid@domain", false},
{"not-a-domain", false},
}

Copy link
Owner

Choose a reason for hiding this comment

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

Empty line.

wantAddrs: []string{},
},
}

Copy link
Owner

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()

Copy link
Owner

Choose a reason for hiding this comment

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

Empty line.

func ReadStdin() (string, error) {
info, err := os.Stdin.Stat()
if err != nil {
return "", fmt.Errorf("failed to retrieve stdin information: %w", err)
Copy link
Owner

Choose a reason for hiding this comment

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

retrieving stdin is enough

w.Close()

got, _ := ReadStdin()
addrs, _ := ProcessInputs(got)
Copy link
Owner

Choose a reason for hiding this comment

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

inputs

{
name: "single valid url",
input: "http://192.168.1.1\n",
wantAddrs: []string{"http://192.168.1.1"},
Copy link
Owner

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)
Copy link
Owner

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() {
}
}
}()

Copy link
Owner

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))
}

Copy link
Owner

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 {
Copy link
Owner

Choose a reason for hiding this comment

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

Repeated lines?

}
return false
}

Copy link
Owner

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 {
Copy link
Owner

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

w.Write([]byte(tt.input))
w.Close()

got, _ := ReadStdin()
Copy link
Owner

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.

if validateInput(input) {
inputs = append(inputs, input)
} else {
errFormats = append(errFormats, fmt.Errorf("invalid address format: %s", input))
Copy link
Owner

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.

@jesusprubio jesusprubio merged commit f1668f9 into jesusprubio:main Nov 20, 2024
1 check failed
@jesusprubio jesusprubio mentioned this pull request Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

STDIN piped input
2 participants
0