Skip to content

Commit

Permalink
feat: add github workflows, linters, and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
nohehf committed Sep 23, 2024
1 parent 8fd8c75 commit 4068c31
Show file tree
Hide file tree
Showing 25 changed files with 296 additions and 160 deletions.
19 changes: 19 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Lines starting with '#' are comments.

# Each line is a file pattern followed by one or more owners.

# More details are here: https://help.github.com/articles/about-codeowners/

# The '\*' pattern is global owners.

# Order is important. The last matching pattern has the most precedence.

# The folders are ordered as follows:

# In each subsection folders are ordered first by depth, then alphabetically.

# This should make it easy to add new rules without breaking existing ones.

# Global rule:

* @nohehf
36 changes: 36 additions & 0 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Build Test

on:
pull_request:
paths:
- "**.go"
workflow_dispatch:

jobs:
build:
name: Test Builds
strategy:
matrix:
go-version: [1.20.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Check out code
uses: actions/checkout@v3

- name: Go Mod hygine
run: |
go clean -modcache
go mod tidy
- name: Build
run: go build .
working-directory: cmd/cli

- name: Test
run: go test ./...
39 changes: 39 additions & 0 deletions .github/workflows/lint-report.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Lint report

on:
pull_request:
paths:
- "**.go"
workflow_dispatch:

jobs:
report:
name: Report
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.20"
- name: Install report card
run: |
git clone https://github.com/gojp/goreportcard.git
cd goreportcard
make install
go install ./cmd/goreportcard-cli
cd ..
rm -rf goreportcard
- name: Report
run: goreportcard-cli -v -t 100

golangci-lint:
name: Lint (golangci-lint)
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
with:
go-version: "1.22"
cache: false
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
27 changes: 27 additions & 0 deletions .github/workflows/release-binaries.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release Binaries

on:
push:
tags:
- "*"
workflow_dispatch:

jobs:
goreleaser:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-go@v3
with:
go-version: 1.20.x

- uses: goreleaser/goreleaser-action@v4
with:
args: "release --clean"
version: latest
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
17 changes: 17 additions & 0 deletions .github/workflows/report-card.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# update report card on push on main
name: 🧚‍♀️ Update Report Card

on:
push:
branches:
- main
workflow_dispatch:

jobs:
report-card:
name: Update Report Card
runs-on: ubuntu-latest
steps:
- name: Request Report Update
run: |
curl -X POST -F "repo=github.com/$GITHUB_REPOSITORY" https://goreportcard.com/checks
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ go.work.sum

# env file
.env

# local binaries
bin/*
42 changes: 42 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
linters:
disable-all: true
enable:
# # default: https://golangci-lint.run/usage/linters/#enabled-by-default
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
# # extras: https://golangci-lint.run/usage/linters/#disabled-by-default
- gocyclo # Computes and checks the cyclomatic complexity of functions.
- gocritic # Provides diagnostics that check for bugs, performance and style issues.
# Extensible without recompilation through dynamic rules.
# Dynamic rules are written declaratively with AST patterns, filters, report message and optional suggestion.
- goconst # Finds repeated strings that could be replaced by a constant.
- mnd # An analyzer to detect magic numbers.
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
- bodyclose # Checks whether HTTP response body is closed successfully.
- durationcheck
- gochecknoinits
- containedctx
- perfsprint
- protogetter
- spancheck
- sloglint
- noctx
- exhaustive
- contextcheck
- whitespace
fast: true
linters-settings:
exhaustive:
check:
- switch
- map
default-signifies-exhaustive: true
severity:
default-severity: error
run:
timeout: 5m
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["golang.go"]
}
16 changes: 16 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Go settings
"go.lintTool": "golangci-lint",
"go.useLanguageServer": true,
"go.lintFlags": ["--fast", "--fix"],
// Show logs even on test success
"go.testFlags": ["-v"],
"[go]": {
"editor.defaultFormatter": "golang.go",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
}
}
}
6 changes: 6 additions & 0 deletions DEVELOPING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Developing

## Dependencies

- Go version >= 1.22.x
- [golangci-lint](https://golangci-lint.run/welcome/install/#local-installation)
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/[email protected] run --fast -- $(go list -f '{{.Dir}}/...' -m)

.PHONY: pre-build
pre-build:
go run cmd/pre-build/pre-build.go

.PHONY: build
build: pre-build
go build -o ./bin/cli ./cmd/cli/*.go

.PHONY: test
test:

22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ func main() {
if err != nil {
// do something with your error
}
p := r.GetProviderForIP(ip)
switch p {
case provider.Aws:
println("Yay got AWS as expected")
case provider.Unknown:
println("Seems like we could not find the provider... Thats weird.")
default:
println("Mmmmh seems like the provider is wrong...")
if len(ip) == 0 {
// do something with your error
}
for _, i := range ip {
p := r.GetProviderForIP(i)
switch p {
case provider.Aws:
println("Yay got AWS as expected")
case provider.Unknown:
println("Seems like we could not find the provider... Thats weird.")
default:
println("Mmmmh seems like the provider is wrong...")
}
}
}

```

## pre build
Expand Down
Binary file removed cmd/cli/cli
Binary file not shown.
20 changes: 10 additions & 10 deletions cmd/cli/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ import (
"strings"
)

func parseHostname(URL string) (string, error) {
func parseHostname(urlStr string) (string, error) {
// If there is no scheme, net/url parsing will fail, parsing the host as path.
// In that case you add a leading //
if !(strings.Contains(URL, "//")) {
URL = "//" + URL
if !(strings.Contains(urlStr, "//")) {
urlStr = "//" + urlStr
}

u, err := url.Parse(URL)
u, err := url.Parse(urlStr)
if err != nil {
return "", fmt.Errorf("failed to parse url \"%s\": %w", URL, err)
return "", fmt.Errorf("failed to parse url \"%s\": %w", urlStr, err)
}

if u.Host == "" {
return "", fmt.Errorf("failed to get host from url \"%s\"", URL)
return "", fmt.Errorf("failed to get host from url \"%s\"", urlStr)
}

return u.Hostname(), nil
}

func getIPsForURL(ctx context.Context, URL string) ([]net.IP, error) {
hostname, err := parseHostname(URL)
func getIPsForURL(ctx context.Context, urlStr string) ([]net.IP, error) {
hostname, err := parseHostname(urlStr)
if err != nil {
return nil, fmt.Errorf("could not get ips for url \"%s\": %w", URL, err)
return nil, fmt.Errorf("could not get ips for url \"%s\": %w", urlStr, err)
}
// If we already have an ip return it (no need to check the DNS)
ip := net.ParseIP(hostname)
Expand All @@ -39,7 +39,7 @@ func getIPsForURL(ctx context.Context, URL string) ([]net.IP, error) {
}
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", hostname)
if err != nil {
return nil, fmt.Errorf("could not get ips for url \"%s\": %w", URL, err)
return nil, fmt.Errorf("could not get ips for url \"%s\": %w", urlStr, err)
}
if len(ips) == 0 {
return nil, fmt.Errorf("DNS lookup did not find any ips for host \"%s\"", hostname)
Expand Down
30 changes: 30 additions & 0 deletions examples/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"net"

"escape.tech/cloudfinder/pkg/cloud"
"escape.tech/cloudfinder/pkg/provider"
)

func main() {
r := cloud.NewResolver()
ip, err := net.LookupIP("escape.tech")
if err != nil {
// do something with your error
}
if len(ip) == 0 {
// do something with your error
}
for _, i := range ip {
p := r.GetProviderForIP(i)
switch p {
case provider.Aws:
println("Yay got AWS as expected")
case provider.Unknown:
println("Seems like we could not find the provider... Thats weird.")
default:
println("Mmmmh seems like the provider is wrong...")
}
}
}
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
module escape.tech/cloudfinder

go 1.22.5

require (
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/tools v0.25.0 // indirect
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
2 changes: 1 addition & 1 deletion internal/static/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Ŷ ��6|���������6�u;d֨�JoN��
o�T��]���O��!1{�W|�}�91��h
Binary file modified internal/static/ipv4.gob
Binary file not shown.
Binary file modified internal/static/ipv6.gob
Binary file not shown.
Loading

0 comments on commit 4068c31

Please sign in to comment.