commit 9c465bf7d511e56e292f8eb3f830a6df8965e969
parent 0401b66150477f5fe53a63dfdaedc23dff5e6501
Author: Lukas Henkel <lh@entf.net>
Date: Tue, 16 Feb 2021 21:42:45 +0100
Refactored cli related code into cmd package
Diffstat:
8 files changed, 62 insertions(+), 56 deletions(-)
diff --git a/cmd/cmd.go b/cmd/cmd.go
@@ -0,0 +1,50 @@
+package cmd
+
+import (
+ "fmt"
+ "io"
+ "os"
+
+ "golang.org/x/net/html"
+)
+
+func readerFromFile(file string) (f io.Reader, name string, err error) {
+ if file == "-" {
+ name = "[stdin]"
+ f = os.Stdin
+ } else {
+ name = file
+ f, err = os.Open(file)
+ if err != nil {
+ return
+ }
+ }
+ return
+}
+
+func logErr(fileName string, err error) {
+ fmt.Fprintf(os.Stderr, "%s: %v\n", fileName, err)
+}
+
+func Fatal(err error) {
+ fmt.Fprintf(os.Stderr, "%v\n", err)
+}
+
+func Main(args []string, handleFunc func(*html.Node)) {
+ if len(args) == 0 {
+ args = append(args, "-")
+ }
+ for _, a := range args {
+ f, fn, err := readerFromFile(a)
+ if err != nil {
+ logErr(fn, err)
+ continue
+ }
+ doc, err := html.Parse(f)
+ if err != nil {
+ logErr(fn, err)
+ return
+ }
+ handleFunc(doc)
+ }
+}
diff --git a/cmd/htmlattr/main.go b/cmd/htmlattr/main.go
@@ -7,6 +7,7 @@ import (
"strings"
"entf.net/htmltools"
+ "entf.net/htmltools/cmd"
"golang.org/x/net/html"
)
@@ -20,7 +21,7 @@ func main() {
os.Exit(1)
}
attrs := strings.Split(args[0], fs)
- htmltools.Main(args[1:], func(doc *html.Node) {
+ cmd.Main(args[1:], func(doc *html.Node) {
body, err := htmltools.Body(doc)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
diff --git a/cmd/htmlindentheadings/main.go b/cmd/htmlindentheadings/main.go
@@ -8,6 +8,7 @@ import (
"golang.org/x/net/html"
"entf.net/htmltools"
+ "entf.net/htmltools/cmd"
)
const usage = "usage: htmlindentheadings INDENT_LEVELS [FILES...]"
@@ -23,7 +24,7 @@ func main() {
fmt.Println(usage)
os.Exit(1)
}
- htmltools.Main(args[1:], func(doc *html.Node) {
+ cmd.Main(args[1:], func(doc *html.Node) {
for node := range htmltools.FindRecursive(doc, nil) {
htmltools.IndentHeadings(lvls, node)
}
diff --git a/cmd/htmlremove/main.go b/cmd/htmlremove/main.go
@@ -7,7 +7,7 @@ import (
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
- "entf.net/htmltools"
+ "entf.net/htmltools/cmd"
)
func main() {
@@ -21,7 +21,7 @@ func main() {
fmt.Fprintf(os.Stderr, "selector invalid: %v\n", err)
os.Exit(1)
}
- htmltools.Main(args[1:], func(doc *html.Node) {
+ cmd.Main(args[1:], func(doc *html.Node) {
remove(sel, doc)
})
}
diff --git a/cmd/htmlselect/main.go b/cmd/htmlselect/main.go
@@ -9,7 +9,7 @@ import (
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
- "entf.net/htmltools"
+ "entf.net/htmltools/cmd"
)
func main() {
@@ -23,7 +23,7 @@ func main() {
fmt.Fprintf(os.Stderr, "selector invalid: %v\n", err)
os.Exit(1)
}
- htmltools.Main(args[1:], func(doc *html.Node) {
+ cmd.Main(args[1:], func(doc *html.Node) {
dosel(sel, doc)
})
}
diff --git a/cmd/htmltotext/main.go b/cmd/htmltotext/main.go
@@ -8,10 +8,11 @@ import (
"golang.org/x/net/html"
"entf.net/htmltools"
+ "entf.net/htmltools/cmd"
)
func main() {
- htmltools.Main(os.Args[1:], func(doc *html.Node) {
+ cmd.Main(os.Args[1:], func(doc *html.Node) {
for n := range htmltools.FindRecursive(
doc,
htmltools.MatchNodeTypeFunc(html.TextNode)) {
diff --git a/cmd/htmlunwrap/main.go b/cmd/htmlunwrap/main.go
@@ -8,6 +8,7 @@ import (
"golang.org/x/net/html"
"entf.net/htmltools"
+ "entf.net/htmltools/cmd"
)
func main() {
@@ -21,7 +22,7 @@ func main() {
fmt.Fprintf(os.Stderr, "selector invalid: %v\n", err)
os.Exit(1)
}
- htmltools.Main(args[1:], func(doc *html.Node) {
+ cmd.Main(args[1:], func(doc *html.Node) {
unwrap(sel, doc)
})
}
diff --git a/shared.go b/shared.go
@@ -1,48 +0,0 @@
-package htmltools
-
-import (
- "fmt"
- "io"
- "os"
-
- "golang.org/x/net/html"
-)
-
-var currentFile string
-
-func readerFromFile(file string) (f io.Reader, err error) {
- if file == "-" {
- currentFile = "[stdin]"
- f = os.Stdin
- } else {
- currentFile = file
- f, err = os.Open(file)
- if err != nil {
- return
- }
- }
- return
-}
-
-func LogErr(err error) {
- fmt.Fprintf(os.Stderr, "%s: %v\n", currentFile, err)
-}
-
-func Main(args []string, handleFunc func(*html.Node)) {
- if len(args) == 0 {
- args = append(args, "-")
- }
- for _, a := range args {
- f, err := readerFromFile(a)
- if err != nil {
- LogErr(err)
- continue
- }
- doc, err := html.Parse(f)
- if err != nil {
- LogErr(err)
- return
- }
- handleFunc(doc)
- }
-}