From 424af5dfe0d1d7316d79db4dbc10f18d44750c1e Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Mon, 28 Apr 2025 16:04:50 +0100 Subject: [PATCH] style: Apply `go fmt` to go code (#2046) ~I will add pre-commit hook later if I find a good one~ Found a nice repo, but it is no longer maintained: https://github.com/dnephin/pre-commit-golang So, I implemented the check as a local hook. --- .pre-commit-config.yaml | 9 ++++ pre-commit-hooks/run-go-fmt.sh | 14 +++++ .../internal/ammo_provider/ammo_provider.go | 16 +++--- .../internal/parse_args/parse_args.go | 4 +- .../internal/request_maker/request_maker.go | 54 +++++++++---------- tools/requests_gun/requests_gun.go | 30 +++++------ 6 files changed, 75 insertions(+), 52 deletions(-) create mode 100755 pre-commit-hooks/run-go-fmt.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3d23370e..b1fafbb9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,3 +30,12 @@ repos: hooks: - id: prettier exclude: ^docs/doxygen-awesome-theme/ + + - repo: local + hooks: + - id: gofmt + name: Go Format + entry: pre-commit-hooks/run-go-fmt.sh + types: [go] + language: golang + description: "Runs `gofmt`, requires golang" diff --git a/pre-commit-hooks/run-go-fmt.sh b/pre-commit-hooks/run-go-fmt.sh new file mode 100755 index 00000000..6f73c894 --- /dev/null +++ b/pre-commit-hooks/run-go-fmt.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# +# Capture and print stdout, since gofmt doesn't use proper exit codes +# +set -e -o pipefail + +if ! command -v gofmt &> /dev/null ; then + echo "gofmt not installed or available in the PATH" >&2 + exit 1 +fi + +output="$(gofmt -l -w "$@")" +echo "$output" +[[ -z "$output" ]] diff --git a/tools/requests_gun/internal/ammo_provider/ammo_provider.go b/tools/requests_gun/internal/ammo_provider/ammo_provider.go index ad664cec..25e7c97e 100644 --- a/tools/requests_gun/internal/ammo_provider/ammo_provider.go +++ b/tools/requests_gun/internal/ammo_provider/ammo_provider.go @@ -8,22 +8,22 @@ import ( ) type AmmoProvider struct { - ammo []string + ammo []string currentBullet atomic.Uint64 } func (ap *AmmoProvider) getIndex() uint64 { - result := ap.currentBullet.Add(1) + result := ap.currentBullet.Add(1) return result % uint64(len(ap.ammo)) } func (ap *AmmoProvider) GetBullet() string { - for { - res := ap.ammo[ap.getIndex()] - if !strings.HasPrefix(res, "#") { - return res - } - } + for { + res := ap.ammo[ap.getIndex()] + if !strings.HasPrefix(res, "#") { + return res + } + } } func New(reader io.Reader) *AmmoProvider { diff --git a/tools/requests_gun/internal/parse_args/parse_args.go b/tools/requests_gun/internal/parse_args/parse_args.go index 7b937589..ef73abfb 100644 --- a/tools/requests_gun/internal/parse_args/parse_args.go +++ b/tools/requests_gun/internal/parse_args/parse_args.go @@ -7,7 +7,7 @@ import ( ) type CliArgs struct { - Host string + Host string Port uint TargetLoad uint Ammo string @@ -23,7 +23,7 @@ func Parse() (*CliArgs, error) { target_load := flag.UintP("load", "l", 100, "Target requests per second load") print_errors := flag.BoolP("print-errors", "e", false, "Print errors") help := flag.BoolP("help", "h", false, "Print help message") - ws := flag.BoolP("ws", "w", false, "Use websocket") + ws := flag.BoolP("ws", "w", false, "Use websocket") flag.Parse() diff --git a/tools/requests_gun/internal/request_maker/request_maker.go b/tools/requests_gun/internal/request_maker/request_maker.go index 670bc1fa..f5a03d05 100644 --- a/tools/requests_gun/internal/request_maker/request_maker.go +++ b/tools/requests_gun/internal/request_maker/request_maker.go @@ -4,12 +4,12 @@ import ( "encoding/json" "errors" "fmt" + "github.com/gorilla/websocket" "io" "net/http" - "net/url" + "net/url" "strings" "time" - "github.com/gorilla/websocket" ) type RequestMaker interface { @@ -17,11 +17,11 @@ type RequestMaker interface { } type WebSocketClient struct { - conn *websocket.Conn + conn *websocket.Conn } type HttpRequestMaker struct { - host string + host string transport *http.Transport client *http.Client } @@ -38,10 +38,10 @@ type ResponseData struct { func (h *HttpRequestMaker) MakeRequest(request string) (*ResponseData, error) { startTime := time.Now() - req, err := http.NewRequest("POST", h.host, strings.NewReader(request)) - if err != nil { - return nil, errors.New("Error creating request: " + err.Error()) - } + req, err := http.NewRequest("POST", h.host, strings.NewReader(request)) + if err != nil { + return nil, errors.New("Error creating request: " + err.Error()) + } response, err := h.client.Do(req) requestDuration := time.Since(startTime) @@ -80,12 +80,12 @@ func NewHttp(host string, port uint) *HttpRequestMaker { } func NewWebSocketClient(host string, port uint) (*WebSocketClient, error) { - var u url.URL - if !strings.HasPrefix(host, "ws://") && !strings.HasPrefix(host, "wss://") { - u = url.URL{Scheme: "ws", Host: host + ":" + fmt.Sprintf("%d", port), Path: "/"} - } else { - u = url.URL{Host: host + ":" + fmt.Sprintf("%d", port), Path: "/"} - } + var u url.URL + if !strings.HasPrefix(host, "ws://") && !strings.HasPrefix(host, "wss://") { + u = url.URL{Scheme: "ws", Host: host + ":" + fmt.Sprintf("%d", port), Path: "/"} + } else { + u = url.URL{Host: host + ":" + fmt.Sprintf("%d", port), Path: "/"} + } conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) if err != nil { return nil, errors.New("Error connecting to WebSocket: " + err.Error()) @@ -95,24 +95,24 @@ func NewWebSocketClient(host string, port uint) (*WebSocketClient, error) { // SendMessage sends a message to the WebSocket server func (ws *WebSocketClient) SendMessage(message string) (*ResponseData, error) { - defer ws.conn.Close() - start := time.Now() - err := ws.conn.WriteMessage(websocket.TextMessage, []byte(message)) - if err != nil { - return nil, errors.New("Error sending ws message: " + err.Error()) - } + defer ws.conn.Close() + start := time.Now() + err := ws.conn.WriteMessage(websocket.TextMessage, []byte(message)) + if err != nil { + return nil, errors.New("Error sending ws message: " + err.Error()) + } - var msg []byte - err = ws.conn.SetReadDeadline(time.Now().Add(5 * time.Second)) - if err != nil { - return nil, errors.New("Error setting timeout: " + err.Error()) - } + var msg []byte + err = ws.conn.SetReadDeadline(time.Now().Add(5 * time.Second)) + if err != nil { + return nil, errors.New("Error setting timeout: " + err.Error()) + } _, msg, err = ws.conn.ReadMessage() if err != nil { return nil, errors.New("Error reading message: " + err.Error()) } - requestDuration := time.Since(start) - ws.conn.Close() + requestDuration := time.Since(start) + ws.conn.Close() var response JsonMap err = json.Unmarshal(msg, &response) diff --git a/tools/requests_gun/requests_gun.go b/tools/requests_gun/requests_gun.go index 04b895d7..37647b42 100644 --- a/tools/requests_gun/requests_gun.go +++ b/tools/requests_gun/requests_gun.go @@ -10,25 +10,25 @@ import ( func main() { args, err := parse_args.Parse() - if err != nil { - fmt.Fprintln(os.Stderr, "Error: ", err) - parse_args.PrintUsage() - os.Exit(1) - } + if err != nil { + fmt.Fprintln(os.Stderr, "Error: ", err) + parse_args.PrintUsage() + os.Exit(1) + } - if args.Help { - parse_args.PrintUsage() - os.Exit(0) - } + if args.Help { + parse_args.PrintUsage() + os.Exit(0) + } fmt.Print("Loading ammo... ") - f, err := os.Open(args.Ammo) - if err != nil { - fmt.Println("Error opening file '", args.Ammo, "': ", err) - os.Exit(1) - } + f, err := os.Open(args.Ammo) + if err != nil { + fmt.Println("Error opening file '", args.Ammo, "': ", err) + os.Exit(1) + } ammoProvider := ammo_provider.New(f) - f.Close() + f.Close() fmt.Println("Done") fmt.Println("Firing requests...")