Skip to content

Commit b6d5cca

Browse files
Merge pull request #54 from goark/develop
Refactor error handling and code structure for improved readability and maintainability
2 parents 0cee53e + 2ed60a2 commit b6d5cca

29 files changed

Lines changed: 742 additions & 573 deletions

‎.github/workflows/build.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ jobs:
1414
steps:
1515
-
1616
name: Checkout
17-
uses: actions/checkout@v3
17+
uses: actions/checkout@v6
1818
with:
1919
fetch-depth: 0
2020
-
2121
name: Set up Go
22-
uses: actions/setup-go@v3
22+
uses: actions/setup-go@v6
2323
with:
2424
go-version-file: 'go.mod'
2525
-
2626
name: Run GoReleaser
27-
uses: goreleaser/goreleaser-action@v4
27+
uses: goreleaser/goreleaser-action@v7
2828
with:
2929
# either 'goreleaser' (default) or 'goreleaser-pro'
3030
distribution: goreleaser
31-
version: latest
31+
version: '~> v2'
3232
args: release --clean
3333
env:
3434
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

‎.github/workflows/lint.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ jobs:
1414
name: lint
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v3
18-
- uses: actions/setup-go@v3
17+
- uses: actions/checkout@v6
18+
- uses: actions/setup-go@v6
1919
with:
2020
go-version-file: 'go.mod'
2121
- name: golangci-lint
22-
uses: golangci/golangci-lint-action@v3
22+
uses: golangci/golangci-lint-action@v9
2323
with:
2424
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
2525
version: latest

‎.golangci.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: "2"
2+
linters:
3+
settings:
4+
gosec:
5+
# excludes rules G117
6+
excludes:
7+
- G117

‎.goreleaser.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ builds:
2828

2929
archives:
3030
-
31-
format: tar.gz
31+
formats: ["tar.gz"]
3232
format_overrides:
3333
- goos: windows
34-
format: zip
34+
formats: ["zip"]
3535
name_template: >-
3636
{{ .Binary }}_
3737
{{- .Version }}_

‎README.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# [toolbox] -- A collection of miscellaneous commands
22

3-
[![check vulns](https://github.com/goark/toolbox/workflows/vulns/badge.svg)](https://github.com/goark/toolbox/actions)
43
[![lint status](https://github.com/goark/toolbox/workflows/lint/badge.svg)](https://github.com/goark/toolbox/actions)
54
[![lint status](https://github.com/goark/toolbox/workflows/build/badge.svg)](https://github.com/goark/toolbox/actions)
65
[![GitHub license](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/goark/toolbox/master/LICENSE)
76
[![GitHub release](http://img.shields.io/github/release/goark/toolbox.svg)](https://github.com/goark/toolbox/releases/latest)
87

9-
This package is required Go 1.20 or later.
8+
This package is required Go 1.26 or later.
109

1110
## Build and Install
1211

‎Taskfile.yml‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ tasks:
55
cmds:
66
- task: prepare
77
- task: test
8-
- task: nancy
8+
- task: govulncheck
9+
# - task: nancy
910
- task: graph
1011

1112
build-all:
@@ -19,7 +20,15 @@ tasks:
1920
- go mod verify
2021
- go test -shuffle on ./...
2122
- govulncheck ./...
22-
- golangci-lint run --enable gosec --timeout 10m0s ./...
23+
- golangci-lint-v2 run --enable gosec --timeout 10m0s ./...
24+
sources:
25+
- ./go.mod
26+
- '**/*.go'
27+
28+
govulncheck:
29+
desc: Check reachable vulnerabilities with latest govulncheck.
30+
cmds:
31+
- go run golang.org/x/vuln/cmd/govulncheck@latest ./...
2332
sources:
2433
- ./go.mod
2534
- '**/*.go'
@@ -33,7 +42,7 @@ tasks:
3342
- '**/*.go'
3443

3544
prepare:
36-
- go mod tidy -v -go=1.23
45+
- go mod tidy -v -go=1.26
3746

3847
clean:
3948
desc: Initialize module and build cache, and remake go.sum file.

‎apod/apod.go‎

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"os"
7+
"path/filepath"
78

89
"github.com/goark/errs"
910
"github.com/goark/toolbox/db"
@@ -26,33 +27,39 @@ type APOD struct {
2627
}
2728

2829
// New functions creates new APOD instance from file.
29-
func New(ctx context.Context, path, cacheDir string, logger *log.ZapEventLogger) (*APOD, error) {
30+
func New(ctx context.Context, path, cacheDir string, logger *log.ZapEventLogger) (cfg *APOD, err error) {
3031
// open database
31-
repos, err := db.Open(ctx, cacheDir, logger)
32-
if err != nil {
33-
return nil, errs.Wrap(err, errs.WithContext("cache_dir", cacheDir))
32+
repos, ferr := db.Open(ctx, cacheDir, logger)
33+
if ferr != nil {
34+
err = errs.Wrap(ferr, errs.WithContext("cache_dir", cacheDir))
35+
return
3436
}
3537

3638
// read configuration file
3739
if len(path) == 0 {
38-
return fallthroughCfg(repos, logger), nil
40+
cfg = fallthroughCfg(repos, logger)
41+
return
3942
}
40-
file, err := os.Open(path)
41-
if err != nil {
42-
return fallthroughCfg(repos, logger), nil
43+
file, ferr := os.Open(filepath.Clean(path))
44+
if ferr != nil {
45+
cfg = fallthroughCfg(repos, logger)
46+
return
4347
}
44-
defer file.Close()
45-
var cfg APOD
46-
if err := json.NewDecoder(file).Decode(&cfg); err != nil {
47-
return nil, errs.Wrap(err, errs.WithContext("path", path))
48+
defer func() {
49+
err = errs.Join(err, file.Close())
50+
}()
51+
var vcfg APOD
52+
if jerr := json.NewDecoder(file).Decode(&vcfg); jerr != nil {
53+
err = errs.Wrap(jerr, errs.WithContext("path", path))
54+
return
4855
}
49-
cfg.logger = logger
50-
cfg.cacheDir = cacheDir
51-
cfg.repos = repos
52-
cfg.cache = map[string]*nasaapod.Response{}
53-
cfg.saveData = []*nasaapod.Response{}
54-
55-
return &cfg, nil
56+
vcfg.logger = logger
57+
vcfg.cacheDir = cacheDir
58+
vcfg.repos = repos
59+
vcfg.cache = map[string]*nasaapod.Response{}
60+
vcfg.saveData = []*nasaapod.Response{}
61+
cfg = &vcfg
62+
return
5663
}
5764

5865
func fallthroughCfg(repos *db.Repository, logger *log.ZapEventLogger) *APOD {
@@ -74,23 +81,28 @@ func (cfg *APOD) Logger() *zap.Logger {
7481
}
7582

7683
// Export methods exports configuration to config file.
77-
func (cfg *APOD) Export(path string) error {
84+
func (cfg *APOD) Export(path string) (err error) {
7885
if cfg == nil {
79-
return errs.Wrap(ecode.ErrNullPointer)
86+
err = errs.Wrap(ecode.ErrNullPointer)
87+
return
8088
}
81-
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
82-
if err != nil {
83-
return errs.Wrap(err, errs.WithContext("path", path))
89+
file, ferr := os.OpenFile(filepath.Clean(path), os.O_RDWR|os.O_CREATE, 0600)
90+
if ferr != nil {
91+
err = errs.Wrap(ferr, errs.WithContext("path", path))
92+
return
8493
}
85-
defer file.Close()
94+
defer func() {
95+
err = errs.Join(err, file.Close())
96+
}()
8697

87-
if err := json.NewEncoder(file).Encode(cfg); err != nil {
88-
return errs.Wrap(err, errs.WithContext("path", path))
98+
if jerr := json.NewEncoder(file).Encode(cfg); jerr != nil {
99+
err = errs.Wrap(jerr, errs.WithContext("path", path))
100+
return
89101
}
90-
return nil
102+
return
91103
}
92104

93-
/* Copyright 2023-2024 Spiegel
105+
/* Copyright 2023-2026 Spiegel
94106
*
95107
* Licensed under the Apache License, Version 2.0 (the "License");
96108
* you may not use this file except in compliance with the License.

‎apod/lookup.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,27 @@ func MakeMessage(data *nasaapod.Response) string {
108108
// hash tag
109109
bld.WriteString("#apod " + data.Date.String())
110110
if len(data.MediaType) > 0 && data.MediaType != "image" {
111-
bld.WriteString(fmt.Sprintf(" (%s)", data.MediaType))
111+
fmt.Fprintf(&bld, " (%s)", data.MediaType)
112112
}
113113
bld.WriteString("\n")
114114
//title
115115
if len(data.Title) > 0 {
116-
bld.WriteString(fmt.Sprintln(data.Title))
116+
fmt.Fprintln(&bld, data.Title)
117117
}
118118
// credit
119119
if len(data.Copyright) > 0 {
120-
bld.WriteString(fmt.Sprintln("Image Credit:", data.Copyright))
120+
fmt.Fprintln(&bld, "Image Credit:", data.Copyright)
121121
}
122122
// Web page
123-
bld.WriteString(fmt.Sprintln("Web page:", data.WebPage()))
123+
fmt.Fprintln(&bld, "Web page:", data.WebPage())
124124
// content URL
125125
if data.MediaType != nasaapod.MediaImage && len(data.Url) > 0 {
126-
bld.WriteString(fmt.Sprintln("Content:", data.Url))
126+
fmt.Fprintln(&bld, "Content:", data.Url)
127127
}
128128
return bld.String()
129129
}
130130

131-
/* Copyright 2023 Spiegel
131+
/* Copyright 2023-2026 Spiegel
132132
*
133133
* Licensed under the Apache License, Version 2.0 (the "License");
134134
* you may not use this file except in compliance with the License.

‎bluesky/authfile.go‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,43 @@ func (cfg *Bluesky) authPath() string {
1818
return filepath.Join(cfg.BaseDir(), "bluesky.auth")
1919
}
2020

21-
func (cfg *Bluesky) readAuth() (*xrpc.AuthInfo, error) {
22-
file, err := os.Open(cfg.authPath())
23-
if err != nil {
24-
return nil, errs.Wrap(err, errs.WithContext("authfile", cfg.authPath()))
21+
func (cfg *Bluesky) readAuth() (auth *xrpc.AuthInfo, err error) {
22+
file, ferr := os.Open(cfg.authPath())
23+
if ferr != nil {
24+
err = errs.Wrap(ferr, errs.WithContext("authfile", cfg.authPath()))
25+
return
2526
}
26-
defer file.Close()
27+
defer func() {
28+
err = errs.Join(err, file.Close())
29+
}()
2730

28-
var auth xrpc.AuthInfo
29-
if err := json.NewDecoder(file).Decode(&auth); err != nil {
30-
return nil, errs.Wrap(err, errs.WithContext("authfile", cfg.authPath()))
31+
var vauth xrpc.AuthInfo
32+
if jerr := json.NewDecoder(file).Decode(&vauth); jerr != nil {
33+
err = errs.Wrap(jerr, errs.WithContext("authfile", cfg.authPath()))
34+
return
3135
}
32-
return &auth, nil
36+
auth = &vauth
37+
return
3338
}
3439

35-
func (cfg *Bluesky) writeAuth(auth *xrpc.AuthInfo) error {
36-
file, err := os.OpenFile(cfg.authPath(), os.O_RDWR|os.O_CREATE, 0600)
37-
if err != nil {
38-
return errs.Wrap(err, errs.WithContext("authfile", cfg.authPath()))
40+
func (cfg *Bluesky) writeAuth(auth *xrpc.AuthInfo) (err error) {
41+
file, ferr := os.OpenFile(cfg.authPath(), os.O_RDWR|os.O_CREATE, 0600)
42+
if ferr != nil {
43+
err = errs.Wrap(ferr, errs.WithContext("authfile", cfg.authPath()))
44+
return
3945
}
40-
defer file.Close()
46+
defer func() {
47+
err = errs.Join(err, file.Close())
48+
}()
4149

42-
if err := json.NewEncoder(file).Encode(auth); err != nil {
43-
return errs.Wrap(err, errs.WithContext("authfile", cfg.authPath()))
50+
if jerr := json.NewEncoder(file).Encode(auth); jerr != nil {
51+
err = errs.Wrap(jerr, errs.WithContext("authfile", cfg.authPath()))
52+
return
4453
}
45-
return nil
54+
return
4655
}
4756

48-
/* Copyright 2023 Spiegel
57+
/* Copyright 2023-2026 Spiegel
4958
*
5059
* Licensed under the Apache License, Version 2.0 (the "License");
5160
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)