Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ dit run https://github.com/login
# Classify forms in a local file
dit run login.html

# Read HTML from stdin ("-" reads stdin explicitly)
curl -s https://github.com/login | dit run -

# With probabilities
dit run https://github.com/login --proba

Expand Down
32 changes: 32 additions & 0 deletions dit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ func TestFunctional_RunStdin(t *testing.T) {
}
}

func TestFunctional_RunStdinDash(t *testing.T) {
binary := buildBinary(t)

// Simulating: curl https://example.com | dit run -
cmd := exec.Command(binary, "run", "-s", "-")
cmd.Stdin = strings.NewReader(loginFormHTML)

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
t.Fatalf("Functional dash test failed: %v\nStderr: %s", err, stderr.String())
}

// Output can be either page+forms object or just forms array
var pageResult struct {
Type string `json:"type"`
Forms []struct {
Type string `json:"type"`
Fields map[string]string `json:"fields"`
} `json:"forms"`
}
if err := json.Unmarshal(stdout.Bytes(), &pageResult); err != nil {
t.Fatalf("Output is not valid JSON: %v\nOutput: %s", err, stdout.String())
}

if len(pageResult.Forms) == 0 {
t.Fatal("No forms found in output")
}
}

func TestFunctional_RunStdinURL(t *testing.T) {
binary := buildBinary(t)

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ module github.com/happyhackingspace/dit

go 1.26.0

toolchain go1.26.9
toolchain go1.26.10

require (
github.com/PuerkitoBio/goquery v1.13.0
github.com/chromedp/chromedp v0.16.0
github.com/creativeprojects/go-selfupdate v1.6.0
github.com/spf13/cobra v1.10.2
golang.org/x/net v0.59.0
golang.org/x/net v0.62.0
)

require (
Expand All @@ -34,9 +34,9 @@ require (
github.com/spf13/pflag v1.0.9 // indirect
github.com/ulikunitz/xz v0.5.15 // indirect
gitlab.com/gitlab-org/api/client-go v1.46.0 // indirect
golang.org/x/crypto v0.57.0 // indirect
golang.org/x/crypto v0.58.0 // indirect
golang.org/x/oauth2 v0.36.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/time v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY=
golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
Expand Down
19 changes: 16 additions & 3 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *CLI) newRunCommand() *cobra.Command {
var renderTimeout int

cmd := &cobra.Command{
Use: "run [url-or-file]",
Use: "run [url-or-file|-]",
Short: "Classify page type and forms in a URL, HTML file, or stdin",
Args: cobra.MaximumNArgs(1),
Example: ` # Classify a URL directly
Expand All @@ -36,6 +36,9 @@ func (c *CLI) newRunCommand() *cobra.Command {
# Pipe HTML content from a file
cat login.html | dit run

# Read HTML from stdin explicitly with "-"
curl -s https://github.com/login | dit run -

# Pipe a URL from stdin
echo "https://github.com/login" | dit run

Expand Down Expand Up @@ -68,8 +71,11 @@ func (c *CLI) newRunCommand() *cobra.Command {
timeout: time.Duration(renderTimeout) * time.Second,
}

if len(args) == 0 {
if len(args) == 0 || args[0] == "-" {
if isStdinTerminal() {
if len(args) == 1 {
return fmt.Errorf(`no input on stdin: pipe HTML or a URL when using "-"`)
}
return cmd.Help()
}
htmlContent, target, err = readFromStdin(fetchOpts)
Expand Down Expand Up @@ -237,7 +243,14 @@ func isURL(target string) bool {

func readFromStdin(opts fetchOptions) (string, string, error) {
slog.Debug("Reading from stdin")
body, err := io.ReadAll(os.Stdin)
return readInput(os.Stdin, opts)
}

// readInput reads HTML content or a URL from r. It returns the HTML content
// and the target it came from: the URL itself when r holds a URL, "stdin"
// otherwise.
func readInput(r io.Reader, opts fetchOptions) (string, string, error) {
body, err := io.ReadAll(r)
if err != nil {
return "", "", fmt.Errorf("read stdin: %w", err)
}
Expand Down
48 changes: 48 additions & 0 deletions internal/cli/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestReadInputHTML(t *testing.T) {
html := `<html><body><form><input type="text" name="q"/></form></body></html>`

content, target, err := readInput(strings.NewReader(html), fetchOptions{})
if err != nil {
t.Fatalf("readInput: %v", err)
}
if content != html {
t.Errorf("expected HTML content to pass through, got %q", content)
}
if target != "stdin" {
t.Errorf("expected target %q, got %q", "stdin", target)
}
}

func TestReadInputEmpty(t *testing.T) {
if _, _, err := readInput(strings.NewReader(" \n\t"), fetchOptions{}); err == nil {
t.Error("expected error for empty input")
}
}

func TestReadInputURL(t *testing.T) {
html := `<html><body>fetched</body></html>`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(html))
}))
defer srv.Close()

content, target, err := readInput(strings.NewReader(srv.URL+"\n"), fetchOptions{})
if err != nil {
t.Fatalf("readInput: %v", err)
}
if content != html {
t.Errorf("expected fetched HTML, got %q", content)
}
if target != srv.URL {
t.Errorf("expected target %q, got %q", srv.URL, target)
}
}
Loading