Skip to content

Commit

Permalink
refactor packages
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Nov 26, 2024
1 parent 9909162 commit 58bc2fc
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 165 deletions.
9 changes: 5 additions & 4 deletions cmd/portpatrol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"os/signal"
"syscall"

"github.com/containeroo/portpatrol/internal/config"
"github.com/containeroo/portpatrol/internal/flags"
"github.com/containeroo/portpatrol/internal/logging"
"github.com/containeroo/portpatrol/internal/parser"
"github.com/containeroo/portpatrol/internal/wait"
"golang.org/x/sync/errgroup"
)
Expand All @@ -23,9 +24,9 @@ func run(ctx context.Context, args []string, output io.Writer) error {
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer cancel()
// Parse command-line flags
f, err := config.ParseFlags(args, version)
f, err := flags.ParseFlags(args, parser.ParamPrefix, version, parser.GenerateDocs())
if err != nil {
var helpErr *config.HelpRequested
var helpErr *flags.HelpRequested
if errors.As(err, &helpErr) {
fmt.Fprint(output, helpErr.Message)
return nil
Expand All @@ -34,7 +35,7 @@ func run(ctx context.Context, args []string, output io.Writer) error {
}

// Initialize target checkers
checkers, err := config.LoadTargetCheckers(f.Targets, f.DefaultCheckInterval)
checkers, err := parser.LoadTargetCheckers(f.Targets, f.DefaultCheckInterval)
if err != nil {
return fmt.Errorf("failed to initialize target checkers: %w", err)
}
Expand Down
119 changes: 0 additions & 119 deletions internal/config/docs.go

This file was deleted.

33 changes: 0 additions & 33 deletions internal/config/tcp_flags.go

This file was deleted.

10 changes: 5 additions & 5 deletions internal/config/flags.go → internal/flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package flags

import (
"bytes"
Expand Down Expand Up @@ -34,14 +34,14 @@ type ParsedFlags struct {
}

// ParseFlags parses command-line arguments and returns the parsed flags.
func ParseFlags(args []string, version string) (*ParsedFlags, error) {
func ParseFlags(args []string, paramPrefix, version string, flagDocs map[string][]FlagDoc) (*ParsedFlags, error) {
var knownArgs []string
var dynamicArgs []string

// Separate known flags and dynamic target flags
for i := 0; i < len(args); i++ {
arg := args[i]
if !strings.HasPrefix(arg, fmt.Sprintf("--%s.", ParamPrefix)) {
if !strings.HasPrefix(arg, fmt.Sprintf("--%s.", paramPrefix)) {
knownArgs = append(knownArgs, arg)
continue
}
Expand All @@ -64,9 +64,9 @@ func ParseFlags(args []string, version string) (*ParsedFlags, error) {

// Custom usage function
flagSet.Usage = func() {
fmt.Fprintf(&buf, "Usage: %s [OPTIONS] [--%s.<IDENTIFIER>.<PROPERTY> value]\n\nOptions:\n", flagSetName, ParamPrefix)
fmt.Fprintf(&buf, "Usage: %s [OPTIONS] [--%s.<IDENTIFIER>.<PROPERTY> value]\n\nOptions:\n", flagSetName, paramPrefix)
flagSet.PrintDefaults()
displayCheckerProperties(&buf)
displayCheckerProperties(&buf, flagDocs)
}

// Define known flags
Expand Down
30 changes: 30 additions & 0 deletions internal/flags/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package flags

import (
"bytes"
"fmt"
"text/tabwriter"
)

type FlagDoc struct {
Flag string
Description string
}

func displayCheckerProperties(buf *bytes.Buffer, docs map[string][]FlagDoc) {
w := tabwriter.NewWriter(buf, 0, 0, 2, ' ', 0)

appendFlagDocs := func(title string, docs []FlagDoc) {
fmt.Fprintf(w, "\n%s:\n", title)
fmt.Fprintln(w, " Flag\tDescription")
for _, doc := range docs {
fmt.Fprintf(w, " %s\t%s\n", doc.Flag, doc.Description)
}
}

for title, doc := range docs {
appendFlagDocs(title, doc)
}

w.Flush()
}
42 changes: 41 additions & 1 deletion internal/config/http_flags.go → internal/parser/http_flags.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package config
package parser

import (
"fmt"
"strconv"
"time"

"github.com/containeroo/portpatrol/internal/checker"
"github.com/containeroo/portpatrol/internal/flags"
"github.com/containeroo/portpatrol/pkg/httputils"
)

Expand All @@ -20,6 +21,45 @@ const (
defaultHTTPSkipTLSVerify bool = false
)

var httpFlagDocs = []flags.FlagDoc{
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamAddress),
Description: "The IP address or hostname of the target in the following format: scheme://hostname[:port]",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamName),
Description: "The name of the target. If not specified, it's derived from the target address.",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamType),
Description: "The type of check to perform. If a scheme (e.g. http://) is specified in --%s.<identifier>.address, this flag can be omitted as the type will be inferred.",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamHTTPMethod),
Description: "The HTTP method to use (e.g., GET, POST). Defaults to \"GET\".",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamHTTPHeaders),
Description: "A comma-separated list of HTTP headers to include in the request in \"key=value\" format.\n\tExample: Authorization=Bearer token,Content-Type=application/json",
},
{
Flag: fmt.Sprintf("--%s.<identifier>%s=string", ParamPrefix, ParamHTTPExpectedStatusCodes),
Description: "A comma-separated list of expected HTTP status codes or ranges. Defaults to 200.\n\tExample: \"200,301,404\" or \"200,300-302\" or \"200,301-302,404,500-502\"",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=bool", ParamPrefix, ParamHTTPSkipTLSVerify),
Description: "Whether to skip TLS verification. Defaults to false.",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=duration", ParamPrefix, ParamHTTPTimeout),
Description: "The timeout for the HTTP request (e.g., 5s).",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=duration", ParamPrefix, ParamInterval),
Description: "Override the default interval for this target (e.g., 10s).",
},
}

// parseHTTPCheckerOptions parses HTTP checker-specific options from parameters.
func parseHTTPCheckerOptions(params map[string]string) ([]checker.Option, error) {
var opts []checker.Option
Expand Down
30 changes: 29 additions & 1 deletion internal/config/icmp_flags.go → internal/parser/icmp_flags.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
package config
package parser

import (
"fmt"
"time"

"github.com/containeroo/portpatrol/internal/checker"
"github.com/containeroo/portpatrol/internal/flags"
)

const (
ParamICMPReadTimeout string = "read-timeout"
ParamICMPWriteTimeout string = "write-timeout"
)

var icmpFlagDocs = []flags.FlagDoc{
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamAddress),
Description: "The IP address or hostname of the target in the following format: icmp://hostname (no port allowed).",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamName),
Description: "The name of the target. If not specified, it's derived from the target address.",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=string", ParamPrefix, ParamType),
Description: "The type of check to perform. If the scheme (icmp://) is specified in --%s.<identifier>.address, this flag can be omitted as the type will be inferred.",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=duration", ParamPrefix, ParamICMPReadTimeout),
Description: "The read timeout for the ICMP connection (e.g., 1s).",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=duration", ParamPrefix, ParamICMPWriteTimeout),
Description: "The write timeout for the ICMP connection (e.g., 1s).",
},
{
Flag: fmt.Sprintf("--%s.<identifier>.%s=duration", ParamPrefix, ParamInterval),
Description: "Override the default interval for this target (e.g., 5s).",
},
}

// parseICMPCheckerOptions parses ICMP checker-specific options from parameters.
func parseICMPCheckerOptions(params map[string]string) ([]checker.Option, error) {
var opts []checker.Option
Expand Down
2 changes: 1 addition & 1 deletion internal/config/parser.go → internal/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package parser

import (
"fmt"
Expand Down
Loading

0 comments on commit 58bc2fc

Please sign in to comment.