Skip to content

Commit

Permalink
Merge pull request #196 from henrybear327/ci/add_fmt
Browse files Browse the repository at this point in the history
[CI] Introduce fmt and linter CI pipeline
  • Loading branch information
azimjohn authored Apr 5, 2024
2 parents 38f60f3 + e0fcc76 commit 2f53ea3
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 35 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: Static analysis
on: [push, pull_request]
permissions: read-all
jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.22
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.57.1
args: --config tools/.golangci.yaml
- run: |
set -euo pipefail
make verify
- run: |
set -euo pipefail
make fmt
DIFF=$(git status --porcelain)
if [ -n "$DIFF" ]; then
echo "These files were modified:"
echo
echo "$DIFF"
echo
exit 1
fi
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ test:
go vet ./...
@echo Testing
go test ./... -skip TestConfig_Load

6 changes: 3 additions & 3 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func (c *Config) Write() error {
return fmt.Errorf("error getting user config directory: %s", err)
}
dirPath := filepath.Join(configDir, "jprq")
if err := os.MkdirAll(dirPath, 0700); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(dirPath, 0700); err != nil && os.IsNotExist(err) {
return fmt.Errorf("error creating config directory: %s", err)
}
filePath := filepath.Join(dirPath, localConfig)
}
filePath := filepath.Join(dirPath, localConfig)
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("error creating config file: %s", err)
Expand Down
7 changes: 4 additions & 3 deletions cli/jprqc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"encoding/binary"
"fmt"
"github.com/azimjohn/jprq/cli/debugger"
"github.com/azimjohn/jprq/server/events"
"github.com/azimjohn/jprq/server/tunnel"
"log"
"net"
"strings"

"github.com/azimjohn/jprq/cli/debugger"
"github.com/azimjohn/jprq/server/events"
"github.com/azimjohn/jprq/server/tunnel"
)

type jprqClient struct {
Expand Down
18 changes: 6 additions & 12 deletions server/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ func (e *Event[EventType]) encode() ([]byte, error) {
func (e *Event[EventType]) decode(data []byte) error {
buf := bytes.NewBuffer(data)
dec := gob.NewDecoder(buf)
if err := dec.Decode(&e.Data); err != nil {
return err
}
return nil
err := dec.Decode(&e.Data)
return err
}

func (e *Event[EventType]) Read(conn io.Reader) error {
Expand All @@ -84,10 +82,8 @@ func (e *Event[EventType]) Read(conn io.Reader) error {
if _, err := conn.Read(buffer); err != nil {
return err
}
if err := e.decode(buffer); err != nil {
return err
}
return nil
err := e.decode(buffer)
return err
}

func (e *Event[EventType]) Write(conn io.Writer) error {
Expand All @@ -100,8 +96,6 @@ func (e *Event[EventType]) Write(conn io.Writer) error {
if _, err := conn.Write(length); err != nil {
return err
}
if _, err := conn.Write(data); err != nil {
return err
}
return nil
_, err = conn.Write(data)
return err
}
3 changes: 3 additions & 0 deletions server/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (g github) ObtainToken(code string) (string, error) {
"POST",
"https://github.com/login/oauth/access_token",
strings.NewReader(payload.Encode()))
if err != nil {
return "", fmt.Errorf("failed to perform http request: %v", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")

Expand Down
16 changes: 5 additions & 11 deletions server/jprq.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -49,11 +48,8 @@ func (j *Jprq) Init(conf config.Config, oauth github.Authenticator) error {
if err := j.publicServer.Init(conf.PublicServerPort, "jprq_public_server"); err != nil {
return err
}
if err := j.publicServerTLS.InitTLS(conf.PublicServerTLSPort, "jprq_public_server_tls", conf.TLSCertFile,
conf.TLSKeyFile); err != nil {
return err
}
return nil
err := j.publicServerTLS.InitTLS(conf.PublicServerTLSPort, "jprq_public_server_tls", conf.TLSCertFile, conf.TLSKeyFile)
return err
}

func (j *Jprq) Start() {
Expand All @@ -76,10 +72,8 @@ func (j *Jprq) Stop() error {
if err := j.publicServer.Stop(); err != nil {
return err
}
if err := j.publicServerTLS.Stop(); err != nil {
return err
}
return nil
err := j.publicServerTLS.Stop()
return err
}

func (j *Jprq) servePublicConn(conn net.Conn) error {
Expand All @@ -96,7 +90,7 @@ func (j *Jprq) servePublicConn(conn net.Conn) error {
t, found := j.httpTunnels[host]
if !found {
writeResponse(conn, 404, "Not Found", "tunnel not found. create one at jprq.io")
return errors.New(fmt.Sprintf("unknown host requested %s", host))
return fmt.Errorf("unknown host requested %s", host)
}
return t.PublicConnectionHandler(conn, buffer)
}
Expand Down
5 changes: 3 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package main

import (
"github.com/azimjohn/jprq/server/config"
"github.com/azimjohn/jprq/server/github"
"log"
"os"
"os/signal"

"github.com/azimjohn/jprq/server/config"
"github.com/azimjohn/jprq/server/github"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion server/tunnel/tcp_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package tunnel

import (
"fmt"
"github.com/azimjohn/jprq/server/server"
"io"

"github.com/azimjohn/jprq/server/server"
)

type TCPTunnel struct {
Expand Down
7 changes: 4 additions & 3 deletions server/tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/azimjohn/jprq/server/events"
"github.com/azimjohn/jprq/server/server"
"io"
"net"
"sync"
"time"

"github.com/azimjohn/jprq/server/events"
"github.com/azimjohn/jprq/server/server"
)

type Tunnel interface {
Expand Down Expand Up @@ -74,7 +75,7 @@ func (t *tunnel) publicConnectionHandler(publicCon net.Conn) error {
}
publicCon.Close()
event.Write(t.eventWriter)
return errors.New(fmt.Sprintf("[connections-limit-reached]: %s", t.hostname))
return fmt.Errorf("[connections-limit-reached]: %s", t.hostname)
}

event := events.Event[events.ConnectionReceived]{
Expand Down
104 changes: 104 additions & 0 deletions tools/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
run:
timeout: 30m
issues:
max-same-issues: 0
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# exclude ineffassing linter for generated files for conversion
- path: conversion\.go
linters: [ineffassign]
exclude-files:
- ^zz_generated.*
linters:
disable-all: true
enable: # please keep this alphabetized
# Don't use soon to deprecated[1] linters that lead to false
# https://github.com/golangci/golangci-lint/issues/1841
# - deadcode
# - structcheck
# - varcheck
- goimports
- ineffassign
- nakedret
- revive
- staticcheck
- stylecheck
- unconvert # Remove unnecessary type conversions
- unparam
- unused
linters-settings: # please keep this alphabetized
goimports:
local-prefixes: go.etcd.io # Put imports beginning with prefix after 3rd-party packages.
nakedret:
# Align with https://github.com/alexkohler/nakedret/blob/v1.0.2/cmd/nakedret/main.go#L10
max-func-lines: 5
revive:
ignore-generated-header: false
severity: error
confidence: 0.8
enable-all-rules: false
rules:
- name: blank-imports
severity: error
disabled: false
- name: context-as-argument
severity: error
disabled: false
- name: dot-imports
severity: error
disabled: false
- name: error-return
severity: error
disabled: false
- name: error-naming
severity: error
disabled: false
- name: if-return
severity: error
disabled: false
- name: increment-decrement
severity: error
disabled: false
- name: var-declaration
severity: error
disabled: false
- name: package-comments
severity: error
disabled: false
- name: range
severity: error
disabled: false
- name: receiver-naming
severity: error
disabled: false
- name: time-naming
severity: error
disabled: false
- name: indent-error-flow
severity: error
disabled: false
- name: errorf
severity: error
disabled: false
- name: context-keys-type
severity: error
disabled: false
- name: error-strings
severity: error
disabled: false
# TODO: enable the following rules
- name: var-naming
disabled: true
- name: exported
disabled: true
- name: unexported-return
disabled: true
staticcheck:
checks:
- all
- -SA1019 # TODO(fix) Using a deprecated function, variable, constant or field
- -SA2002 # TODO(fix) Called testing.T.FailNow or SkipNow in a goroutine, which isn’t allowed
stylecheck:
checks:
- ST1019 # Importing the same package multiple times.

0 comments on commit 2f53ea3

Please sign in to comment.