Skip to content

Commit

Permalink
Linter and Drone config fixes, code linting.
Browse files Browse the repository at this point in the history
  • Loading branch information
pztrn committed Nov 20, 2021
1 parent 9c045e9 commit d5fcc5c
Show file tree
Hide file tree
Showing 22 changed files with 152 additions and 146 deletions.
3 changes: 1 addition & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ steps:
- name: docker
image: plugins/docker
when:
branch:
master
branch: ["master"]
settings:
username:
from_secret: dockerhub_user
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*DS_Store*
data/*
.idea
.vscode
12 changes: 9 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ linters:
linters-settings:
lll:
line-length: 420
gocyclo:
min-complexity: 40

cyclop:
max-complexity: 25

issues:
exclude-rules:
# There will be some ToDos.
- linters:
- godox
text: "TODO"
7 changes: 2 additions & 5 deletions cmd/giredorectl/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

import (
// stdlib
"os"

// local
"github.com/teris-io/cli"
clientv1 "go.dev.pztrn.name/giredore/domains/client/v1"
"go.dev.pztrn.name/giredore/internal/logger"

// other
"github.com/teris-io/cli"
)

func main() {
Expand All @@ -34,6 +30,7 @@ func main() {
logger.Initialize()
clientv1.Initialize()
clientv1.SetAllowedIPs(args, options)

return 0
}),
),
Expand Down
2 changes: 0 additions & 2 deletions cmd/giredored/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
// stdlib
"os"
"os/signal"
"syscall"

// local
serverv1 "go.dev.pztrn.name/giredore/domains/server/v1"
"go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/httpserver"
Expand Down
2 changes: 0 additions & 2 deletions domains/client/v1/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package clientv1

import (
// stdlib
"strings"

// local
"go.dev.pztrn.name/giredore/internal/requester"
"go.dev.pztrn.name/giredore/internal/structs"
)
Expand Down
9 changes: 2 additions & 7 deletions domains/client/v1/exported.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package clientv1

import (
// local
"github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/logger"
"go.dev.pztrn.name/giredore/internal/requester"

// other
"github.com/rs/zerolog"
)

var (
log zerolog.Logger
)
var log zerolog.Logger

func Initialize() {
log = logger.Logger.With().Str("type", "domain").Str("package", "client").Int("version", 1).Logger()
Expand Down
3 changes: 1 addition & 2 deletions domains/client/v1/packages.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package clientv1

import (
// stdlib
"strings"

// local
"go.dev.pztrn.name/giredore/internal/requester"
"go.dev.pztrn.name/giredore/internal/structs"
)
Expand All @@ -29,6 +27,7 @@ func DeletePackage(args []string, options map[string]string) {
func GetPackages(args []string, options map[string]string) {
pkgs := strings.Split(args[0], ",")

// nolint:exhaustivestruct
req := &structs.PackageGetRequest{}
if pkgs[0] == "all" {
req.All = true
Expand Down
18 changes: 9 additions & 9 deletions domains/server/v1/configapi.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package serverv1

import (
// stdlib
"net/http"

// local
"github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs"

// other
"github.com/labstack/echo"
)

// This function responsible for getting runtime configuration.
func configurationGET(ec echo.Context) error {
// nolint:wrapcheck
return ec.JSON(http.StatusOK, configuration.Cfg)
}

func configurationAllowedIPsSET(ec echo.Context) error {
func configurationAllowedIPsSET(ectx echo.Context) error {
// nolint:exhaustivestruct
req := &structs.AllowedIPsSetRequest{}
if err := ec.Bind(req); err != nil {
if err := ectx.Bind(req); err != nil {
log.Error().Err(err).Msg("Failed to parse allowed IPs set request")
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingAllowedIPsSetRequest}})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingAllowedIPsSetRequest}})
}

log.Debug().Msgf("Got set allowed IPs request: %+v", req)

configuration.Cfg.SetAllowedIPs(req.AllowedIPs)

return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
}
9 changes: 2 additions & 7 deletions domains/server/v1/exported.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package serverv1

import (
// local
"github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/httpserver"
"go.dev.pztrn.name/giredore/internal/logger"

// other
"github.com/rs/zerolog"
)

var (
log zerolog.Logger
)
var log zerolog.Logger

func Initialize() {
log = logger.Logger.With().Str("type", "domain").Str("package", "server").Int("version", 1).Logger()
Expand Down
25 changes: 13 additions & 12 deletions domains/server/v1/goimports.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
package serverv1

import (
// stdlib
"net/http"
"strings"

// local
"github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs"

// other
"github.com/labstack/echo"
)

func throwGoImports(ec echo.Context) error {
func throwGoImports(ectx echo.Context) error {
// Getting real path. This might be the package itself, or namespace
// to list available packages.
// For now only package itself is supported, all other features in ToDo.
packageNameRaw := ec.Request().URL.Path
packageNameRaw := ectx.Request().URL.Path

pkgs, errs := configuration.Cfg.GetPackagesInfo([]string{packageNameRaw})

if errs != nil {
log.Error().Str("package", packageNameRaw).Msgf("Failed to get package information: %+v", errs)
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})

// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
}

if len(pkgs) == 0 {
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
}

pkg, found := pkgs[packageNameRaw]
if !found {
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
}

// We should compose package name using our domain under which giredore
// is working.
domain := ec.Request().Host
domain := ectx.Request().Host
packageName := domain + packageNameRaw

tmpl := singlePackageTemplate
tmpl = strings.Replace(tmpl, "{PKGNAME}", packageName, -1)
tmpl = strings.Replace(tmpl, "{VCS}", pkg.VCS, 1)
tmpl = strings.Replace(tmpl, "{REPOPATH}", pkg.RealPath, 1)

return ec.HTML(http.StatusOK, tmpl)
// nolint:wrapcheck
return ectx.HTML(http.StatusOK, tmpl)
}
51 changes: 31 additions & 20 deletions domains/server/v1/packagesapi.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package serverv1

import (
// stdlib
"net/http"
"strings"

// local
"github.com/labstack/echo"
"go.dev.pztrn.name/giredore/internal/configuration"
"go.dev.pztrn.name/giredore/internal/structs"

// other
"github.com/labstack/echo"
)

// This function responsible for getting packages configuration.
func packagesGET(ec echo.Context) error {
func packagesGET(ectx echo.Context) error {
// nolint:exhaustivestruct
req := &structs.PackageGetRequest{}
if err := ec.Bind(req); err != nil {
if err := ectx.Bind(req); err != nil {
log.Error().Err(err).Msg("Failed to parse package get request")
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}})

// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}})
}

log.Info().Msgf("Received package(s) info get request: %+v", req)
Expand All @@ -34,47 +33,59 @@ func packagesGET(ec echo.Context) error {
}

if len(errors) > 0 {
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs})
// nolint:wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs})
}

return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs})
}

// This function responsible for deleting package.
func packagesDELETE(ec echo.Context) error {
func packagesDELETE(ectx echo.Context) error {
// nolint:exhaustivestruct
req := &structs.PackageDeleteRequest{}
if err := ec.Bind(req); err != nil {
if err := ectx.Bind(req); err != nil {
log.Error().Err(err).Msg("Failed to parse package delete request")
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}})

// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}})
}

log.Info().Msgf("Received package delete request: %+v", req)

errs := configuration.Cfg.DeletePackage(req)

if len(errs) > 0 {
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
}

return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
}

// This function responsible for setting or updating packages.
func packagesSET(ec echo.Context) error {
func packagesSET(ectx echo.Context) error {
// nolint:exhaustivestruct
req := &structs.Package{}
if err := ec.Bind(req); err != nil {
if err := ectx.Bind(req); err != nil {
log.Error().Err(err).Msg("Failed to parse package data")
return ec.JSON(http.StatusBadRequest, nil)

// nolint:wrapcheck
return ectx.JSON(http.StatusBadRequest, nil)
}

log.Info().Msgf("Received package set/update request: %+v", req)

// Validate passed package data.
if !strings.HasPrefix(req.OriginalPath, "/") {
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}})
}

configuration.Cfg.AddOrUpdatePackage(req)

return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
// nolint:exhaustivestruct,wrapcheck
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
}
1 change: 0 additions & 1 deletion internal/configuration/envconfig.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package configuration

import (
// other
"github.com/vrischmann/envconfig"
)

Expand Down
7 changes: 3 additions & 4 deletions internal/configuration/exported.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package configuration

import (
// local
"go.dev.pztrn.name/giredore/internal/logger"

// other
"github.com/rs/zerolog"
"go.dev.pztrn.name/giredore/internal/logger"
)

var (
Expand All @@ -19,9 +16,11 @@ func Initialize() {
log = logger.Logger.With().Str("type", "internal").Str("package", "configuration").Logger()
log.Info().Msg("Initializing...")

// nolint:exhaustivestruct
envCfg = &envConfig{}
envCfg.Initialize()

// nolint:exhaustivestruct
Cfg = &fileConfig{}
Cfg.Initialize()

Expand Down

0 comments on commit d5fcc5c

Please sign in to comment.