Skip to content

Commit

Permalink
Improvements for golint
Browse files Browse the repository at this point in the history
  • Loading branch information
waybackarchiver committed Aug 14, 2021
1 parent a40c0e5 commit 7c954cb
Show file tree
Hide file tree
Showing 57 changed files with 314 additions and 189 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,12 @@ jobs:

- name: Run alex with reviewdog
uses: reviewdog/action-alex@v1

goreportcard:
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
steps:
- name: Run Go report card
run: |
path=$(curl -sf -X POST -F "repo=github.com/$GITHUB_REPOSITORY" https://goreportcard.com/checks | jq -r '.redirect')
echo -e "\nSee report for https://goreportcard.com${path}"
12 changes: 6 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ var Opts *Options

// nolint:stylecheck
const (
SLOT_IA = "ia"
SLOT_IS = "is"
SLOT_IP = "ip"
SLOT_PH = "ph"
SLOT_TT = "tt"
SLOT_GC = "gc"
SLOT_IA = "ia" // Internet Archive
SLOT_IS = "is" // archive.today
SLOT_IP = "ip" // IPFS
SLOT_PH = "ph" // Telegraph
SLOT_TT = "tt" // Time Travel
SLOT_GC = "gc" // Google Cache

PB_SLUG = "/playback" // Identity for playback
UNKNOWN = "unknown"
Expand Down
1 change: 0 additions & 1 deletion config/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
/*
Package config handles configuration management for the application.
*/

package config // import "github.com/wabarc/wayback/config"
3 changes: 2 additions & 1 deletion config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ var (
defTorRemotePorts = []int{80}
)

// Options represents a configuration options in the application.
type Options struct {
debug bool
logTime bool
Expand Down Expand Up @@ -583,7 +584,7 @@ func (o *Options) PoolingSize() int {
return o.poolingSize
}

// SrorageDir returns the directory to storage binary file, e.g. html file, PDF
// StorageDir returns the directory to storage binary file, e.g. html file, PDF
func (o *Options) StorageDir() string {
return o.storageDir
}
Expand Down
4 changes: 2 additions & 2 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ func NewParser() *Parser {
func (p *Parser) ParseEnvironmentVariables() (*Options, error) {
if err := p.parseLines(os.Environ()); err != nil {
return nil, err
} else {
return p.opts, nil
}
return p.opts, nil
}

// ParseFile loads configuration values from a local file.
Expand Down Expand Up @@ -73,6 +72,7 @@ func (p *Parser) parseFileContent(r io.Reader) (lines []string) {
}

// nolint:gocyclo,unparam
// gocyclo:ignore
func (p *Parser) parseLines(lines []string) (err error) {
for _, line := range lines {
fields := strings.SplitN(line, "=", 2)
Expand Down
3 changes: 1 addition & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// license that can be found in the LICENSE file.

/*
Wayback is a toolkit for snapshot webpage to
Package wayback is a toolkit for snapshot webpage to
Internet Archive, archive.today, IPFS and beyond.
*/

package wayback // import "github.com/wabarc/wayback"
3 changes: 1 addition & 2 deletions entity/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
/*
Package entity contains all data structures used by the application.
*/

package entity // import "github.com/wabarc/entity"
package entity // import "github.com/wabarc/wayback/entity"
1 change: 1 addition & 0 deletions entity/playback.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package entity // import "github.com/wabarc/entity"

// EntityPlayback represents a keyword for playback entity.
const EntityPlayback = "playback"

// Playback represents a Playback in the application.
Expand Down
1 change: 0 additions & 1 deletion errors/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
/*
Package errors handles errors.
*/

package errors // import "github.com/wabarc/wayback/errors"
1 change: 0 additions & 1 deletion metrics/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
/*
Package metrics exposes wayback service status.
*/

package metrics // import "github.com/wabarc/wayback/metrics"
1 change: 1 addition & 0 deletions metrics/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Gather holds configured collector.
var Gather *Collector

// Export export metrics by the given labels for matching.
func (c *Collector) Export(labels ...string) string {
logger.Debug("export metrics family: %#v", prometheus.DefaultRegisterer)

Expand Down
1 change: 0 additions & 1 deletion pooling/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
/*
Package pooling implements the wayback workers pool.
*/

package pooling // import "github.com/wabarc/wayback/pooling"
4 changes: 2 additions & 2 deletions pooling/pooling.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
const maxTime = 5 * time.Minute

var (
ErrPoolNotExist = errors.New("pool not exist")
ErrTimeout = errors.New("process timeout")
ErrPoolNotExist = errors.New("pool not exist") // ErrPoolNotExist pool not exist
ErrTimeout = errors.New("process timeout") // ErrTimeout process timeout
)

type resource struct {
Expand Down
3 changes: 3 additions & 0 deletions publish/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func NewDiscord(bot *discord.Session) *discordBot {
return &discordBot{bot: bot}
}

// Publish publish text to the Discord channel of given cols and args.
// A context should contain a `reduxer.Bundle` via `publish.PubBundle` constant.
func (d *discordBot) Publish(ctx context.Context, cols []wayback.Collect, args ...string) {
metrics.IncrementPublish(metrics.PublishDiscord, metrics.StatusRequest)

Expand Down Expand Up @@ -97,6 +99,7 @@ func (d *discordBot) toChannel(_ context.Context, bundle *reduxer.Bundle, text s
return true
}

// UploadToDiscord composes files that share with Discord by a given bundle.
func UploadToDiscord(bundle *reduxer.Bundle) (files []*discord.File) {
if bundle != nil {
var fsize int64
Expand Down
3 changes: 3 additions & 0 deletions publish/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type gitHub struct {
client *github.Client
}

// NewGitHub returns a gitHub client.
func NewGitHub(httpClient *http.Client) *gitHub {
if config.Opts.GitHubToken() == "" || config.Opts.GitHubOwner() == "" {
logger.Error("GitHub personal access token is required")
Expand All @@ -43,6 +44,8 @@ func NewGitHub(httpClient *http.Client) *gitHub {
return &gitHub{client: client}
}

// Publish publish markdown text to the GitHub issues of given cols and args.
// A context should contain a `reduxer.Bundle` via `publish.PubBundle` constant.
func (gh *gitHub) Publish(ctx context.Context, cols []wayback.Collect, args ...string) {
metrics.IncrementPublish(metrics.PublishGithub, metrics.StatusRequest)

Expand Down
3 changes: 3 additions & 0 deletions publish/mastodon.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type mastodon struct {
client *mstdn.Client
}

// NewMastodon returns a mastodon client.
func NewMastodon(client *mstdn.Client) *mastodon {
if !config.Opts.PublishToMastodon() {
logger.Error("Missing required environment variable")
Expand All @@ -38,6 +39,8 @@ func NewMastodon(client *mstdn.Client) *mastodon {
return &mastodon{client: client}
}

// Publish publish toot to the Mastodon of given cols and args.
// A context should contain a `reduxer.Bundle` via `publish.PubBundle` constant.
func (m *mastodon) Publish(ctx context.Context, cols []wayback.Collect, args ...string) {
var id string
if len(args) > 1 {
Expand Down
3 changes: 3 additions & 0 deletions publish/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type matrixBot struct {
client *matrix.Client
}

// NewMatrix returns a matrixBot client.
func NewMatrix(client *matrix.Client) *matrixBot {
if !config.Opts.PublishToMatrixRoom() {
logger.Error("Missing required environment variable, abort.")
Expand Down Expand Up @@ -48,6 +49,8 @@ func NewMatrix(client *matrix.Client) *matrixBot {
return &matrixBot{client: client}
}

// Publish publish text to the Matrix room of given cols and args.
// A context should contain a `reduxer.Bundle` via `publish.PubBundle` constant.
func (m *matrixBot) Publish(ctx context.Context, cols []wayback.Collect, args ...string) {
metrics.IncrementPublish(metrics.PublishMatrix, metrics.StatusRequest)

Expand Down
66 changes: 38 additions & 28 deletions publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math/rand"
"net/url"
"os"
"strconv"
"strings"
"text/template"
"time"
Expand All @@ -29,17 +30,20 @@ import (
matrix "maunium.net/go/mautrix"
)

// Flag represents a type of uint8
type Flag uint8

const (
FlagWeb = "web"
FlagTelegram = "telegram"
FlagTwitter = "twitter"
FlagMastodon = "mastodon"
FlagDiscord = "discord"
FlagMatrix = "matrix"
FlagSlack = "slack"
FlagIRC = "irc"

PubBundle = "reduxer-bundle"
FlagWeb Flag = iota // FlagWeb publish from httpd service
FlagTelegram // FlagTelegram publish from telegram service
FlagTwitter // FlagTwitter publish from twitter srvice
FlagMastodon // FlagMastodon publish from mastodon service
FlagDiscord // FlagDiscord publish from discord service
FlagMatrix // FlagMatrix publish from matrix service
FlagSlack // FlagSlack publish from slack service
FlagIRC // FlagIRC publish from relaychat service

PubBundle = "reduxer-bundle" // Publish bundle key in a context with value
)

var maxDelayTime = 10
Expand All @@ -52,7 +56,12 @@ type Publisher interface {
Publish(ctx context.Context, cols []wayback.Collect, args ...string)
}

func process(p Publisher, ctx context.Context, cols []wayback.Collect, args ...string) {
// String returns the flag as a string.
func (f Flag) String() string {
return strconv.Itoa(int(f))
}

func process(ctx context.Context, pub Publisher, cols []wayback.Collect, args ...string) {
// Compose the collects into multiple parts by URI
var parts = make(map[string][]wayback.Collect)
for _, col := range cols {
Expand All @@ -75,7 +84,7 @@ func process(p Publisher, ctx context.Context, cols []wayback.Collect, args ...s
time.Sleep(w)
}

p.Publish(ctx, part, args...)
pub.Publish(ctx, part, args...)
return nil
})
}
Expand All @@ -94,6 +103,7 @@ func from(args ...string) (f string) {
return f
}

// To publish to specific destination services
// nolint:gocyclo
func To(ctx context.Context, cols []wayback.Collect, args ...string) {
f := from(args...)
Expand All @@ -107,15 +117,15 @@ func To(ctx context.Context, cols []wayback.Collect, args ...string) {
if bot == nil {
return
}
t := NewTelegram(bot)
process(t, ctx, cols, args...)
pub := NewTelegram(bot)
process(ctx, pub, cols, args...)
}
}
issue := func(ctx context.Context, cols []wayback.Collect, args ...string) {
if config.Opts.PublishToIssues() {
logger.Debug("[%s] publishing to GitHub issues...", f)
gh := NewGitHub(nil)
process(gh, ctx, cols, args...)
pub := NewGitHub(nil)
process(ctx, pub, cols, args...)
}
}
mastodon := func(ctx context.Context, cols []wayback.Collect, args ...string) {
Expand All @@ -125,8 +135,8 @@ func To(ctx context.Context, cols []wayback.Collect, args ...string) {
if rev, ok := ctx.Value(FlagMastodon).(*mstdn.Client); ok {
client = rev
}
mstdn := NewMastodon(client)
process(mstdn, ctx, cols, args...)
pub := NewMastodon(client)
process(ctx, pub, cols, args...)
}
}
discord := func(ctx context.Context, cols []wayback.Collect, args ...string) {
Expand All @@ -136,8 +146,8 @@ func To(ctx context.Context, cols []wayback.Collect, args ...string) {
if rev, ok := ctx.Value(FlagDiscord).(*discord.Session); ok {
s = rev
}
d := NewDiscord(s)
process(d, ctx, cols, args...)
pub := NewDiscord(s)
process(ctx, pub, cols, args...)
}
}
matrix := func(ctx context.Context, cols []wayback.Collect, args ...string) {
Expand All @@ -147,8 +157,8 @@ func To(ctx context.Context, cols []wayback.Collect, args ...string) {
if rev, ok := ctx.Value(FlagMatrix).(*matrix.Client); ok {
client = rev
}
mat := NewMatrix(client)
process(mat, ctx, cols, args...)
pub := NewMatrix(client)
process(ctx, pub, cols, args...)
}
}
twitter := func(ctx context.Context, cols []wayback.Collect, args ...string) {
Expand All @@ -158,8 +168,8 @@ func To(ctx context.Context, cols []wayback.Collect, args ...string) {
if rev, ok := ctx.Value(FlagTwitter).(*twitter.Client); ok {
client = rev
}
twitter := NewTwitter(client)
process(twitter, ctx, cols, args...)
pub := NewTwitter(client)
process(ctx, pub, cols, args...)
}
}
slack := func(ctx context.Context, cols []wayback.Collect, args ...string) {
Expand All @@ -169,8 +179,8 @@ func To(ctx context.Context, cols []wayback.Collect, args ...string) {
if rev, ok := ctx.Value(FlagTwitter).(*slack.Client); ok {
client = rev
}
slack := NewSlack(client)
process(slack, ctx, cols, args...)
pub := NewSlack(client)
process(ctx, pub, cols, args...)
}
}
irc := func(ctx context.Context, cols []wayback.Collect, args ...string) {
Expand All @@ -180,8 +190,8 @@ func To(ctx context.Context, cols []wayback.Collect, args ...string) {
if rev, ok := ctx.Value(FlagIRC).(*irc.Connection); ok {
conn = rev
}
irc := NewIRC(conn)
process(irc, ctx, cols, args...)
pub := NewIRC(conn)
process(ctx, pub, cols, args...)
}
}
funcs := map[string]func(context.Context, []wayback.Collect, ...string){
Expand Down
6 changes: 3 additions & 3 deletions publish/publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestPublishToChannelFromTelegram(t *testing.T) {

ctx := context.WithValue(context.Background(), FlagTelegram, bot)
ctx = context.WithValue(ctx, PubBundle, bundleExample)
To(ctx, collects, FlagTelegram)
To(ctx, collects, FlagTelegram.String())
}

func TestPublishTootFromMastodon(t *testing.T) {
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestPublishTootFromMastodon(t *testing.T) {
mstdn := NewMastodon(nil)

ctx := context.WithValue(context.Background(), FlagMastodon, mstdn.client)
To(ctx, collects, FlagMastodon)
To(ctx, collects, FlagMastodon.String())
}

func TestPublishTweetFromTwitter(t *testing.T) {
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestPublishTweetFromTwitter(t *testing.T) {

twi := NewTwitter(twitter.NewClient(httpClient))
ctx := context.WithValue(context.Background(), FlagTwitter, twi.client)
To(ctx, collects, FlagTwitter)
To(ctx, collects, FlagTwitter.String())
}

func TestPublishToIRCChannelFromIRC(t *testing.T) {
Expand Down
Loading

0 comments on commit 7c954cb

Please sign in to comment.