Skip to content

Commit

Permalink
validate github client input
Browse files Browse the repository at this point in the history
  • Loading branch information
theleeeo committed Jan 27, 2024
1 parent f72a538 commit 1fb9f23
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
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/theleeeo/docs-server
go 1.21.3

require (
github.com/fatih/color v1.16.0
github.com/gofiber/fiber/v2 v2.52.0
github.com/gofiber/template/html/v2 v2.1.0
github.com/google/go-github/v58 v58.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
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/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/gofiber/fiber/v2 v2.52.0 h1:S+qXi7y+/Pgvqq4DrSmREGiFwtB7Bu6+QFLuIHYw/UE=
github.com/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk=
Expand Down
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"
"time"

"github.com/fatih/color"
"github.com/gofiber/fiber/v2"
"github.com/theleeeo/docs-server/app"
"github.com/theleeeo/docs-server/provider"
Expand All @@ -35,24 +36,31 @@ func main() {

cfg, err := loadConfig()
if err != nil {
panic(err)
color.Red("failed to load config: %s", err)
return
}

ghClient := provider.NewGithub(cfg.Provider.Github.Owner, cfg.Provider.Github.Repo, cfg.Provider.Github.Token)
ghClient, err := provider.NewGithub(cfg.Provider.Github.Owner, cfg.Provider.Github.Repo, cfg.Provider.Github.Token)
if err != nil {
color.Red("failed to create github provider: %s", err)
return
}

serverConfig := &server.Config{
PathPrefix: cfg.Server.PathPrefix,
FileSuffix: cfg.Server.FileSuffix,
}
interval, err := parseInterval(cfg.Server.PollInterval)
if err != nil {
panic(err)
color.Red("failed to parse poll interval: %s", err)
return
}
serverConfig.PollInterval = interval

s, err := server.New(serverConfig, ghClient)
if err != nil {
panic(err)
color.Red("failed to create server: %s", err)
return
}

app := app.New(&app.Config{
Expand Down
13 changes: 11 additions & 2 deletions provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"fmt"
"strings"

"github.com/google/go-github/v58/github"
Expand All @@ -14,18 +15,26 @@ type githubProvider struct {
client *github.Client
}

func NewGithub(owner, repo, token string) *githubProvider {
func NewGithub(owner, repo, token string) (*githubProvider, error) {
cl := github.NewClient(nil)
if token != "" {
cl = cl.WithAuthToken(token)
}

if owner == "" {
return nil, fmt.Errorf("owner cannot be empty")
}

if repo == "" {
return nil, fmt.Errorf("repo cannot be empty")
}

return &githubProvider{
owner: owner,
repo: repo,
token: token,
client: cl,
}
}, nil
}

// Get the names of all tags in the repository
Expand Down

0 comments on commit 1fb9f23

Please sign in to comment.