Skip to content

Commit

Permalink
refactor: drop pkg folder (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusprubio authored Nov 12, 2024
1 parent 1afb705 commit 11f0022
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 125 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ Troubleshoot problems with your Internet connection based on different
go install -v github.com/jesusprubio/up@latest
```

### Dependencies

- [Go](https://go.dev/doc/install) stable version.

## Use

The default behavior is to verify all the [supported protocols](pkg/protocol.go)
Expand All @@ -28,12 +24,6 @@ against a randomly selected [public server](pkg/servers.go) for each one.
up
```

This will display help for the tool.

```sh
up -h
```

## License

This project is under the MIT License. See the [LICENSE](LICENSE) file for the full text.
Expand Down
8 changes: 4 additions & 4 deletions examples/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"os"
"time"

"github.com/jesusprubio/up/pkg"
"github.com/jesusprubio/up/internal"
)

func main() {
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
reportCh := make(chan *pkg.Report)
reportCh := make(chan *internal.Report)
defer close(reportCh)
probe := pkg.Probe{
Protocols: []pkg.Protocol{&pkg.TCP{Timeout: 2 * time.Second}},
probe := internal.Probe{
Protocols: []internal.Protocol{&internal.TCP{Timeout: 2 * time.Second}},
Count: 3,
Logger: logger,
ReportCh: reportCh,
Expand Down
3 changes: 0 additions & 3 deletions internal/doc.go

This file was deleted.

53 changes: 0 additions & 53 deletions internal/internal.go

This file was deleted.

32 changes: 0 additions & 32 deletions internal/internal_test.go

This file was deleted.

1 change: 1 addition & 0 deletions internal/options.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package internal provides the core functionality of the application.
package internal

import (
Expand Down
2 changes: 1 addition & 1 deletion pkg/probe.go → internal/probe.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package internal

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/probe_test.go → internal/probe_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package internal

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol.go → internal/protocol.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package internal

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol_test.go → internal/protocol_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package internal

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion pkg/report.go → internal/report.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package internal

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion pkg/servers.go → internal/servers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package internal

import (
"crypto/rand"
Expand Down
2 changes: 1 addition & 1 deletion pkg/servers_test.go → internal/servers_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package internal

import (
"fmt"
Expand Down
71 changes: 57 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,24 @@ import (

"github.com/fatih/color"
"github.com/jesusprubio/up/internal"
"github.com/jesusprubio/up/pkg"
)

const (
appName = "up"
appDesc = `
Troubleshoot problems with your Internet connection based on different
protocols and public servers.
OUTPUT
Details about each request:
{Protocol used} {Response time} {Remote server} {Extra info}
EXIT STATUS
This utility exits with one of the following values:
0 At least one response was heard.
2 The transmission was successful but no responses were received.
1 Any other error occurred.
`
)

// TODO(#39): STDIN piped input.
Expand All @@ -33,31 +50,31 @@ func main() {
lvl.Set(slog.LevelDebug)
}
logger.Debug("Starting ...", "options", opts)
dnsProtocol := &pkg.DNS{Timeout: opts.Timeout}
dnsProtocol := &internal.DNS{Timeout: opts.Timeout}
if opts.DNSResolver != "" {
dnsProtocol.Resolver = opts.DNSResolver
}
protocols := []pkg.Protocol{
&pkg.HTTP{Timeout: opts.Timeout},
&pkg.TCP{Timeout: opts.Timeout},
protocols := []internal.Protocol{
&internal.HTTP{Timeout: opts.Timeout},
&internal.TCP{Timeout: opts.Timeout},
dnsProtocol,
}
if opts.Protocol != "" {
var protocol pkg.Protocol
var protocol internal.Protocol
for _, p := range protocols {
if p.String() == opts.Protocol {
protocol = p
break
}
}
if protocol == nil {
internal.Fatal(fmt.Errorf("unknown protocol: %s", opts.Protocol))
fatal(fmt.Errorf("unknown protocol: %s", opts.Protocol))
}
protocols = []pkg.Protocol{protocol}
protocols = []internal.Protocol{protocol}
}
logger.Info("Starting ...", "protocols", protocols, "count", opts.Count)
if opts.Help {
fmt.Fprintf(os.Stderr, "%s\n", internal.AppDesc)
fmt.Fprintf(os.Stderr, "%s\n", appDesc)
flag.Usage()
os.Exit(1)
}
Expand All @@ -76,9 +93,9 @@ func main() {
logger.Debug("Termination signal received")
cancel()
}()
reportCh := make(chan *pkg.Report)
reportCh := make(chan *internal.Report)
defer close(reportCh)
probe := pkg.Probe{
probe := internal.Probe{
Protocols: protocols,
Count: opts.Count,
Delay: opts.Delay,
Expand All @@ -93,11 +110,11 @@ func main() {
if opts.JSONOutput {
reportJSON, err := json.Marshal(report)
if err != nil {
internal.Fatal(fmt.Errorf("marshaling report: %w", err))
fatal(fmt.Errorf("marshaling report: %w", err))
}
line = string(reportJSON)
} else {
line = internal.ReportToLine(report)
line = reportToLine(report)
}
fmt.Println(line)
if report.Error == nil {
Expand All @@ -111,6 +128,32 @@ func main() {
logger.Debug("Running ...", "setup", probe)
err := probe.Run(ctx)
if err != nil {
internal.Fatal(fmt.Errorf("running probe: %w", err))
fatal(fmt.Errorf("running probe: %w", err))
}
}

// Fatal logs the error to the standard output and exits with status 1.
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s: %s\n", appName, err)
os.Exit(1)
}

// ReportToLine returns a human-readable representation of the report.
func reportToLine(r *internal.Report) string {
symbol := green("✔")
suffix := r.RHost
if r.Error != nil {
symbol = red("✘")
suffix = r.Error.Error()
}
return fmt.Sprintf("%s %s", symbol, fmt.Sprintf(
"%-15s %-14s %-15s", bold(r.ProtocolID), r.Time, faint(suffix),
))
}

var (
green = color.New(color.FgGreen).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
bold = color.New(color.Bold).SprintFunc()
faint = color.New(color.Faint).SprintFunc()
)
2 changes: 0 additions & 2 deletions pkg/doc.go

This file was deleted.

0 comments on commit 11f0022

Please sign in to comment.