Skip to content

Commit

Permalink
Tidy repo
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Sep 16, 2023
1 parent 4aba707 commit e3d81cf
Show file tree
Hide file tree
Showing 22 changed files with 307 additions and 79 deletions.
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# CODEOWNERS: https://help.github.com/articles/about-codeowners/

# Primary repo maintainers.
* @gnolang/tech-staff
24 changes: 24 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 2
updates:

# Maintain dependencies for GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
labels:
- "github_actions"

# Maintain dependencies for top level Go modules
- package-ecosystem: gomod
directory: /
target-branch: "main"
schedule:
interval: weekly
labels:
- "dependencies"
open-pull-requests-limit: 10
pull-request-branch-name:
separator: "-"
reviewers:
- "zivkovicmilos"
124 changes: 124 additions & 0 deletions .github/golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
run:
concurrency: 8
timeout: 10m
issue-exit-code: 1
tests: true
skip-dirs-use-default: true
modules-download-mode: readonly
allow-parallel-runners: false
go: ""

output:
uniq-by-line: false
path-prefix: ""
sort-results: true

issues:
max-issues-per-linter: 0
max-same-issues: 0
new: false
fix: false
exclude-rules:
- path: (.+)_test.go
linters:
- nilnil
- gosec

linters:
fast: false
disable-all: true
enable:
- asasalint # Check for pass []any as any in variadic func(...any)
- asciicheck # Detects funky ASCII characters
- bidichk # Checks for dangerous unicode character sequences
- durationcheck # Check for two durations multiplied together
- errcheck # Forces to not skip error check
- exportloopref # Checks for pointers to enclosing loop variables
- gocritic # Bundles different linting checks
- godot # Checks for periods at the end of comments
- gomoddirectives # Allow or ban replace directives in go.mod
- gosimple # Code simplification
- govet # Official Go tool
- ineffassign # Detects when assignments to existing variables are not used
- nakedret # Finds naked/bare returns and requires change them
- nilerr # Requires explicit returns
- nilnil # Requires explicit returns
- promlinter # Lints Prometheus metrics names
- reassign # Checks that package variables are not reassigned
- revive # Drop-in replacement for golint
- tagliatelle # Checks struct tags
- tenv # Detects using os.Setenv instead of t.Setenv
- testableexamples # Checks if examples are testable (have expected output)
- unparam # Finds unused params
- usestdlibvars # Detects the possibility to use variables/constants from stdlib
- wastedassign # Finds wasted assignment statements
- loggercheck # Checks the odd number of key and value pairs for common logger libraries
- nestif # Finds deeply nested if statements
- nonamedreturns # Reports all named returns
- decorder # Check declaration order of types, consts, vars and funcs
- gocheckcompilerdirectives # Checks that compiler directive comments (//go:) are valid
- gochecknoinits # Checks for init methods
- whitespace # Tool for detection of leading and trailing whitespace
- wsl # Forces you to use empty lines
- unconvert # Unnecessary type conversions
- tparallel # Detects inappropriate usage of t.Parallel() method in your Go test codes
- thelper # Detects golang test helpers without t.Helper() call and checks the consistency of test helpers
- stylecheck # Stylecheck is a replacement for golint
- prealloc # Finds slice declarations that could potentially be pre-allocated
- predeclared # Finds code that shadows one of Go's predeclared identifiers
- nolintlint # Ill-formed or insufficient nolint directives
- nlreturn # Checks for a new line before return and branch statements to increase code clarity
- misspell # Misspelled English words in comments
- makezero # Finds slice declarations with non-zero initial length
- lll # Long lines
- importas # Enforces consistent import aliases
- gosec # Security problems
- gofmt # Whether the code was gofmt-ed
- gofumpt # Stricter gofmt
- goimports # Unused imports
- goconst # Repeated strings that could be replaced by a constant
- dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f())
- dupl # Code clone detection
- errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13
- unused # Checks Go code for unused constants, variables, functions and types

linters-settings:
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- hugeParam
- rangeExprCopy
- rangeValCopy
- importShadow
- unnamedResult
errcheck:
check-type-assertions: false
check-blank: true
exclude-functions:
- io/ioutil.ReadFile
- io.Copy(*bytes.Buffer)
- io.Copy(os.Stdout)
nakedret:
max-func-lines: 1
govet:
enable-all: true
gofmt:
simplify: true
goconst:
min-len: 3
min-occurrences: 3
godot:
scope: all
period: false
tagliatelle:
case:
use-field-name: true
rules:
json: goCamel
yaml: goCamel
18 changes: 18 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
workflow_call:
workflow_dispatch:

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 1.20.x

- name: Checkout code
uses: actions/checkout@v4

- name: Lint
uses: golangci/golangci-lint-action@v3
14 changes: 14 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
on:
push:
branches:
- main
pull_request:

jobs:
lint:
name: Go Linter
uses: ./.github/workflows/lint.yaml

test:
name: Go Test
uses: ./.github/workflows/test.yaml
32 changes: 32 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
workflow_call:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 1.20.x

- name: Checkout code
uses: actions/checkout@v4

- name: Go test
run: go test -shuffle=on -coverprofile coverage.out -timeout 5m ./...

test-with-race:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 1.20.x

- name: Checkout code
uses: actions/checkout@v4

- name: Go race test
run: go test -race -shuffle=on -timeout 5m ./...
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# MacOS Leftovers
.DS_Store

# Editor Leftovers
.vscode
.idea

# Log files
*.log
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY: lint
lint:
golangci-lint run --config .github/golangci.yaml

.PHONY: gofumpt
gofumpt:
go install mvdan.cc/gofumpt@latest
gofumpt -l -w .

.PHONY: fixalign
fixalign:
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
fieldalignment -fix $(filter-out $@,$(MAKECMDGOALS)) # the full package name (not path!)
2 changes: 1 addition & 1 deletion cmd/faucet/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package faucet
package main

import (
"context"
Expand Down
14 changes: 6 additions & 8 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ const (
envPrefix = "GNO_FAUCET"
)

var (
remoteRegex = regexp.MustCompile(`^https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)(?![^<]*(?:<\/\w+>|\/?>))$`)
)
var remoteRegex = regexp.MustCompile(`^https?:\/\/(?:w{1,3}\.)?[^\s.]+(?:\.[a-z]+)*(?::\d+)(?![^<]*(?:<\/\w+>|\/?>))$`)

// faucetCfg wraps the faucet
// root command configuration
type faucetCfg struct {
config.Config

corsConfigPath string
remote string

config.Config
}

// New creates the root faucet command
Expand Down Expand Up @@ -167,7 +165,7 @@ func (c *faucetCfg) exec(context.Context, []string) error {

// Validate the remote URL
// validate the remote address
if !remoteRegex.Match([]byte(c.remote)) {
if !remoteRegex.MatchString(c.remote) {
return errors.New("invalid remote address")
}

Expand Down Expand Up @@ -202,8 +200,8 @@ func readCORSConfig(path string) (*config.CORS, error) {

// Parse it
var corsConfig config.CORS
err = toml.Unmarshal(content, &corsConfig)
if err != nil {

if err := toml.Unmarshal(content, &corsConfig); err != nil {
return nil, err
}

Expand Down
25 changes: 13 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const (
DefaultSendAmount = "1000000ugnot"
DefaultGasFee = "1000000ugnot"
DefaultGasWanted = "100000"
DefaultMnemonic = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast"
DefaultNumAccounts = uint64(1)
//nolint:lll // Mnemonic is naturally long
DefaultMnemonic = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast"
DefaultNumAccounts = uint64(1)
)

var (
Expand All @@ -36,6 +37,9 @@ var (

// Config defines the base-level Faucet configuration
type Config struct {
// The associated CORS config, if any
CORSConfig *CORS `toml:"cors_config"`

// The address at which the faucet will be served.
// Format should be: <IP>:<PORT>
ListenAddress string `toml:"listen_address"`
Expand All @@ -46,10 +50,6 @@ type Config struct {
// The mnemonic for the faucet
Mnemonic string `toml:"mnemonic"`

// The number of faucet accounts,
// based on the mnemonic (account 0, index x)
NumAccounts uint64 `toml:"num_accounts"`

// The static send amount (native currency).
// Format should be: <AMOUNT>ugnot
SendAmount string `toml:"send_amount"`
Expand All @@ -62,8 +62,9 @@ type Config struct {
// Format should be: <AMOUNT>
GasWanted string `toml:"gas_wanted"`

// The associated CORS config, if any
CORSConfig *CORS `toml:"cors_config"`
// The number of faucet accounts,
// based on the mnemonic (account 0, index x)
NumAccounts uint64 `toml:"num_accounts"`
}

// DefaultConfig returns the default faucet configuration
Expand All @@ -83,7 +84,7 @@ func DefaultConfig() *Config {
// ValidateConfig validates the faucet configuration
func ValidateConfig(config *Config) error {
// validate the listen address
if !listenAddressRegex.Match([]byte(config.ListenAddress)) {
if !listenAddressRegex.MatchString(config.ListenAddress) {
return ErrInvalidListenAddress
}

Expand All @@ -93,17 +94,17 @@ func ValidateConfig(config *Config) error {
}

// validate the send amount
if !amountRegex.Match([]byte(config.SendAmount)) {
if !amountRegex.MatchString(config.SendAmount) {
return ErrInvalidSendAmount
}

// validate the gas fee
if !amountRegex.Match([]byte(config.GasFee)) {
if !amountRegex.MatchString(config.GasFee) {
return ErrInvalidGasFee
}

// validate the gas wanted
if !numberRegex.Match([]byte(config.GasWanted)) {
if !numberRegex.MatchString(config.GasWanted) {
return ErrInvalidGasWanted
}

Expand Down
3 changes: 2 additions & 1 deletion faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func NewFaucet(
// Set the single default HTTP handler
f.handlers = []Handler{
{
"/",
f.defaultHTTPHandler,
"/",
},
}

Expand All @@ -71,6 +71,7 @@ func NewFaucet(
}

// Set the send amount
//nolint:errcheck // SendAmount is validated beforehand
f.sendAmount, _ = std.ParseCoins(f.config.SendAmount)

// Generate the in-memory keyring
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gnolang/faucet

go 1.21
go 1.20

require (
github.com/gnolang/gno v0.0.0-20230914214026-ef6a55bf9db2
Expand Down
Loading

0 comments on commit e3d81cf

Please sign in to comment.