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

use kong for argument parsing #7

Merged
merged 1 commit into from
May 17, 2024
Merged
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
57 changes: 57 additions & 0 deletions cmd/find/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package find

import (
"bufio"
"fmt"
"os"

"github.com/mkadirtan/feed-toolbelt/pkg/inspect"
)

type FindCMD struct {
Pipe bool `help:"use piped input" short:"p"`
StrategyHeader bool `help:"toggle header strategy" negatable:"" short:"l" default:"true"`
StrategyPage bool `help:"toggle page strategy" negatable:"" short:"c" default:"true"`
StrategyCommon bool `help:"toggle common strategy" negatable:"" short:"b" default:"false"`
Validate bool `help:"validate feed urls" negatable:"" short:"g" default:"false"`
URL string `arg:"" help:"target url"`
}

func (f *FindCMD) Run() error {
inspectorOptions := []inspect.InspectorOption{
inspect.WithTargetURL(f.URL),
inspect.WithOutputHandler(func(o string) { fmt.Println(o) }),
// inspect.WithDebugHandler(func(d string) { a.debugLogger.Println(d) }),
}

if f.Pipe {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
inspectorOptions = append(inspectorOptions, inspect.WithPipedInput(bufio.NewReader(os.Stdin)))
}
}

if f.Validate {
inspectorOptions = append(inspectorOptions, inspect.WithValidate())
}

if f.StrategyHeader {
inspectorOptions = append(inspectorOptions, inspect.WithStrategyHeader())
}
if f.StrategyPage {
inspectorOptions = append(inspectorOptions, inspect.WithStrategyPage())
}
if f.StrategyCommon {
inspectorOptions = append(inspectorOptions, inspect.WithStrategyCommon())
}

inspectorOptions = append(inspectorOptions)

inspector, err := inspect.NewInspector(inspectorOptions...)
if err != nil {
return err
}
inspector.Find()

return nil
}
157 changes: 0 additions & 157 deletions cmd/main.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/mkadirtan/feed-toolbelt
go 1.22.3

require (
github.com/alecthomas/kong v0.9.0
github.com/mmcdole/gofeed v1.3.0
golang.org/x/net v0.25.0
)
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE=
github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk=
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/mmcdole/gofeed v1.3.0 h1:5yn+HeqlcvjMeAI4gu6T+crm7d0anY85+M+v6fIFNG4=
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package main

import (
"github.com/mkadirtan/feed-toolbelt/cmd"
"github.com/alecthomas/kong"
"github.com/mkadirtan/feed-toolbelt/cmd/find"
)

var cli struct {
Find find.FindCMD `cmd:""`
}

func main() {
cmd.Run()
ctx := kong.Parse(&cli, kong.ShortUsageOnError())
err := ctx.Run()
ctx.FatalIfErrorf(err)
}
4 changes: 2 additions & 2 deletions pkg/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func (i *Inspector) Find() {
if i.config.TargetHTML != nil {
if i.config.PipedInput != nil {
i.findTargetHTML()
return
}
Expand All @@ -41,7 +41,7 @@ func (i *Inspector) processFeedCandidate(feedCandidateURL string, mustValidate b
}

func (i *Inspector) findTargetHTML() {
feedsOnPage, _ := inspectPage(*i.config.TargetHTML)
feedsOnPage, _ := inspectPage(*i.config.PipedInput)
for _, feed := range feedsOnPage {
i.processFeedCandidate(feed, false)
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/inspect/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ type Inspector struct {
}

type InspectorConfig struct {
// strategies option is ignored in case TargetHTML is defined
// TargetHTML takes precedence over targetURL
TargetHTML *io.Reader
// strategies option is ignored in case PipedInput is defined
PipedInput *io.Reader

TargetURL *string
Strategies struct {
Expand All @@ -37,9 +36,9 @@ type InspectorConfig struct {

type InspectorOption func(*InspectorConfig)

func WithTargetHTML(htmlBody io.Reader) InspectorOption {
func WithPipedInput(htmlBody io.Reader) InspectorOption {
return func(c *InspectorConfig) {
c.TargetHTML = &htmlBody
c.PipedInput = &htmlBody
}
}

Expand Down Expand Up @@ -97,7 +96,7 @@ func NewInspector(options ...InspectorOption) (*Inspector, error) {
option(config)
}

if config.TargetHTML == nil && config.TargetURL == nil {
if config.PipedInput == nil && config.TargetURL == nil {
return nil, errNoTarget
}

Expand Down
Loading