mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2026-01-12 08:30:45 +01:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba82003b53 | ||
|
|
5f7e7de644 | ||
|
|
5b183cca29 | ||
|
|
d901be14fa | ||
|
|
a80997835a | ||
|
|
37bb12fd61 | ||
|
|
c19afb9f40 | ||
|
|
911e1041ff | ||
|
|
a10d589b67 | ||
|
|
da508fcb3f | ||
|
|
57f5282032 | ||
|
|
d4f8d8b5ff | ||
|
|
1b026b9dbd | ||
|
|
12cc8d4e25 | ||
|
|
3442b2a845 | ||
|
|
b9a1fb213a | ||
|
|
278ee130e3 | ||
|
|
5357ece2b7 |
8
.github/workflows/test.yml
vendored
8
.github/workflows/test.yml
vendored
@@ -32,12 +32,16 @@ jobs:
|
|||||||
set -o pipefail
|
set -o pipefail
|
||||||
go test -v 2>&1 ./... -coverprofile=coverage.txt | tee test-results.txt
|
go test -v 2>&1 ./... -coverprofile=coverage.txt | tee test-results.txt
|
||||||
- name: Analyse test results
|
- name: Analyse test results
|
||||||
run: go-junit-report < test-results.txt > report.xml
|
if: ${{ !cancelled() }}
|
||||||
|
run: go-junit-report < test-results.txt > junit.xml
|
||||||
- name: Upload test result artifact
|
- name: Upload test result artifact
|
||||||
|
if: ${{ !cancelled() }}
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: test-results
|
name: test-results
|
||||||
path: test-results.txt
|
path: |
|
||||||
|
test-results.txt
|
||||||
|
junit.xml
|
||||||
retention-days: 7
|
retention-days: 7
|
||||||
- name: Upload results to Codecov
|
- name: Upload results to Codecov
|
||||||
uses: codecov/codecov-action@v4
|
uses: codecov/codecov-action@v4
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"archive/zip"
|
"archive/zip"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/belphemur/CBZOptimizer/manga"
|
"github.com/belphemur/CBZOptimizer/manga"
|
||||||
|
"github.com/belphemur/CBZOptimizer/utils/errs"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -14,15 +15,14 @@ func WriteChapterToCBZ(chapter *manga.Chapter, outputFilePath string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create .cbz file: %w", err)
|
return fmt.Errorf("failed to create .cbz file: %w", err)
|
||||||
}
|
}
|
||||||
defer zipFile.Close()
|
defer errs.Capture(&err, zipFile.Close, "failed to close .cbz file")
|
||||||
|
|
||||||
// Create a new ZIP writer
|
// Create a new ZIP writer
|
||||||
zipWriter := zip.NewWriter(zipFile)
|
zipWriter := zip.NewWriter(zipFile)
|
||||||
err = zipWriter.SetComment("Created by CBZOptimizer")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer zipWriter.Close()
|
defer errs.Capture(&err, zipWriter.Close, "failed to close .cbz writer")
|
||||||
|
|
||||||
// Write each page to the ZIP archive
|
// Write each page to the ZIP archive
|
||||||
for _, page := range chapter.Pages {
|
for _, page := range chapter.Pages {
|
||||||
@@ -71,18 +71,11 @@ func WriteChapterToCBZ(chapter *manga.Chapter, outputFilePath string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if chapter.IsConverted {
|
if chapter.IsConverted {
|
||||||
convertedWriter, err := zipWriter.CreateHeader(&zip.FileHeader{
|
|
||||||
Name: "Converted.txt",
|
|
||||||
Method: zip.Deflate,
|
|
||||||
Modified: time.Now(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create Converted.txt in .cbz: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = convertedWriter.Write([]byte(fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", chapter.ConvertedTime)))
|
convertedString := fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", chapter.ConvertedTime)
|
||||||
|
err = zipWriter.SetComment(convertedString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write Converted.txt contents: %w", err)
|
return fmt.Errorf("failed to write comment: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package cbz
|
|||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"github.com/belphemur/CBZOptimizer/manga"
|
"github.com/belphemur/CBZOptimizer/manga"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -10,11 +11,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestWriteChapterToCBZ(t *testing.T) {
|
func TestWriteChapterToCBZ(t *testing.T) {
|
||||||
|
currentTime := time.Now()
|
||||||
|
|
||||||
// Define test cases
|
// Define test cases
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
chapter *manga.Chapter
|
chapter *manga.Chapter
|
||||||
expectedFiles []string
|
expectedFiles []string
|
||||||
|
expectedComment string
|
||||||
}{
|
}{
|
||||||
//test case where there is only one page and ComicInfo and the chapter is converted
|
//test case where there is only one page and ComicInfo and the chapter is converted
|
||||||
{
|
{
|
||||||
@@ -29,9 +33,10 @@ func TestWriteChapterToCBZ(t *testing.T) {
|
|||||||
},
|
},
|
||||||
ComicInfoXml: "<Series>Boundless Necromancer</Series>",
|
ComicInfoXml: "<Series>Boundless Necromancer</Series>",
|
||||||
IsConverted: true,
|
IsConverted: true,
|
||||||
ConvertedTime: time.Now(),
|
ConvertedTime: currentTime,
|
||||||
},
|
},
|
||||||
expectedFiles: []string{"0000.jpg", "ComicInfo.xml", "Converted.txt"},
|
expectedFiles: []string{"0000.jpg", "ComicInfo.xml"},
|
||||||
|
expectedComment: fmt.Sprintf("%s\nThis chapter has been converted by CBZOptimizer.", currentTime),
|
||||||
},
|
},
|
||||||
//test case where there is only one page and no
|
//test case where there is only one page and no
|
||||||
{
|
{
|
||||||
@@ -125,6 +130,10 @@ func TestWriteChapterToCBZ(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tc.expectedComment != "" && r.Comment != tc.expectedComment {
|
||||||
|
t.Errorf("Expected comment %s, but found %s", tc.expectedComment, r.Comment)
|
||||||
|
}
|
||||||
|
|
||||||
// Check if there are no unexpected files
|
// Check if there are no unexpected files
|
||||||
if len(filesInArchive) != len(tc.expectedFiles) {
|
if len(filesInArchive) != len(tc.expectedFiles) {
|
||||||
t.Errorf("Expected %d files, but found %d", len(tc.expectedFiles), len(filesInArchive))
|
t.Errorf("Expected %d files, but found %d", len(tc.expectedFiles), len(filesInArchive))
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"github.com/belphemur/CBZOptimizer/manga"
|
"github.com/belphemur/CBZOptimizer/manga"
|
||||||
|
"github.com/belphemur/CBZOptimizer/utils/errs"
|
||||||
"io"
|
"io"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -18,22 +19,36 @@ func LoadChapter(filePath string) (*manga.Chapter, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open .cbz file: %w", err)
|
return nil, fmt.Errorf("failed to open .cbz file: %w", err)
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer errs.Capture(&err, r.Close, "failed to close opened .cbz file")
|
||||||
|
|
||||||
chapter := &manga.Chapter{
|
chapter := &manga.Chapter{
|
||||||
FilePath: filePath,
|
FilePath: filePath,
|
||||||
}
|
}
|
||||||
|
// Check for comment
|
||||||
|
if r.Comment != "" {
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(r.Comment))
|
||||||
|
if scanner.Scan() {
|
||||||
|
convertedTime := scanner.Text()
|
||||||
|
chapter.ConvertedTime, err = dateparse.ParseAny(convertedTime)
|
||||||
|
if err == nil {
|
||||||
|
chapter.IsConverted = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, f := range r.File {
|
for _, f := range r.File {
|
||||||
if f.FileInfo().IsDir() {
|
if f.FileInfo().IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
err := func() error {
|
||||||
// Open the file inside the zip
|
// Open the file inside the zip
|
||||||
rc, err := f.Open()
|
rc, err := f.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open file inside .cbz: %w", err)
|
return fmt.Errorf("failed to open file inside .cbz: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer errs.Capture(&err, rc.Close, "failed to close file inside .cbz")
|
||||||
|
|
||||||
// Determine the file extension
|
// Determine the file extension
|
||||||
ext := strings.ToLower(filepath.Ext(f.Name))
|
ext := strings.ToLower(filepath.Ext(f.Name))
|
||||||
|
|
||||||
@@ -41,23 +56,20 @@ func LoadChapter(filePath string) (*manga.Chapter, error) {
|
|||||||
// Read the ComicInfo.xml file content
|
// Read the ComicInfo.xml file content
|
||||||
xmlContent, err := io.ReadAll(rc)
|
xmlContent, err := io.ReadAll(rc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rc.Close()
|
return fmt.Errorf("failed to read ComicInfo.xml content: %w", err)
|
||||||
return nil, fmt.Errorf("failed to read ComicInfo.xml content: %w", err)
|
|
||||||
}
|
}
|
||||||
chapter.ComicInfoXml = string(xmlContent)
|
chapter.ComicInfoXml = string(xmlContent)
|
||||||
} else if ext == ".txt" && strings.ToLower(filepath.Base(f.Name)) == "converted.txt" {
|
} else if !chapter.IsConverted && ext == ".txt" && strings.ToLower(filepath.Base(f.Name)) == "converted.txt" {
|
||||||
textContent, err := io.ReadAll(rc)
|
textContent, err := io.ReadAll(rc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rc.Close()
|
return fmt.Errorf("failed to read Converted.xml content: %w", err)
|
||||||
return nil, fmt.Errorf("failed to read Converted.xml content: %w", err)
|
|
||||||
}
|
}
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(textContent))
|
scanner := bufio.NewScanner(bytes.NewReader(textContent))
|
||||||
if scanner.Scan() {
|
if scanner.Scan() {
|
||||||
convertedTime := scanner.Text()
|
convertedTime := scanner.Text()
|
||||||
chapter.ConvertedTime, err = dateparse.ParseAny(convertedTime)
|
chapter.ConvertedTime, err = dateparse.ParseAny(convertedTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rc.Close()
|
return fmt.Errorf("failed to parse converted time: %w", err)
|
||||||
return nil, fmt.Errorf("failed to parse converted time: %w", err)
|
|
||||||
}
|
}
|
||||||
chapter.IsConverted = true
|
chapter.IsConverted = true
|
||||||
}
|
}
|
||||||
@@ -66,8 +78,7 @@ func LoadChapter(filePath string) (*manga.Chapter, error) {
|
|||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
_, err = io.Copy(buf, rc)
|
_, err = io.Copy(buf, rc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rc.Close()
|
return fmt.Errorf("failed to read file contents: %w", err)
|
||||||
return nil, fmt.Errorf("failed to read file contents: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new Page object
|
// Create a new Page object
|
||||||
@@ -82,8 +93,11 @@ func LoadChapter(filePath string) (*manga.Chapter, error) {
|
|||||||
// Add the page to the chapter
|
// Add the page to the chapter
|
||||||
chapter.Pages = append(chapter.Pages, page)
|
chapter.Pages = append(chapter.Pages, page)
|
||||||
}
|
}
|
||||||
rc.Close()
|
return nil
|
||||||
|
}()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return chapter, nil
|
return chapter, nil
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func TestLoadChapter(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Converted Chapter",
|
name: "Converted Chapter",
|
||||||
filePath: "../testdata/Chapter 1_converted.cbz",
|
filePath: "../testdata/Chapter 10_converted.cbz",
|
||||||
expectedPages: 107,
|
expectedPages: 107,
|
||||||
expectedSeries: "<Series>Boundless Necromancer</Series>",
|
expectedSeries: "<Series>Boundless Necromancer</Series>",
|
||||||
expectedConversion: true,
|
expectedConversion: true,
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func WatchCommand(_ *cobra.Command, args []string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to prepare converter: %v", err)
|
return fmt.Errorf("failed to prepare converter: %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("Watching [%s] with [override: %t, quality: %d, format: %s]", path, override, quality, converterType.String())
|
log.Printf("Watching [%s] with [override: %t, quality: %d, format: %s, split: %t]", path, override, quality, converterType.String(), split)
|
||||||
|
|
||||||
events := make(chan inotifywaitgo.FileEvent)
|
events := make(chan inotifywaitgo.FileEvent)
|
||||||
errors := make(chan error)
|
errors := make(chan error)
|
||||||
|
|||||||
6
go.mod
6
go.mod
@@ -11,8 +11,8 @@ require (
|
|||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
github.com/spf13/viper v1.19.0
|
github.com/spf13/viper v1.19.0
|
||||||
github.com/thediveo/enumflag/v2 v2.0.5
|
github.com/thediveo/enumflag/v2 v2.0.5
|
||||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
|
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
|
||||||
golang.org/x/image v0.19.0
|
golang.org/x/image v0.20.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -43,7 +43,7 @@ require (
|
|||||||
go.uber.org/atomic v1.9.0 // indirect
|
go.uber.org/atomic v1.9.0 // indirect
|
||||||
go.uber.org/multierr v1.9.0 // indirect
|
go.uber.org/multierr v1.9.0 // indirect
|
||||||
golang.org/x/sys v0.18.0 // indirect
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
golang.org/x/text v0.17.0 // indirect
|
golang.org/x/text v0.18.0 // indirect
|
||||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
16
go.sum
16
go.sum
@@ -118,18 +118,18 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
|||||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
|
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
|
||||||
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
|
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
|
||||||
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
|
golang.org/x/image v0.20.0 h1:7cVCUjQwfL18gyBJOmYvptfSHS8Fb3YUDtfLIZ7Nbpw=
|
||||||
golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys=
|
golang.org/x/image v0.20.0/go.mod h1:0a88To4CYVBAHp5FXJm8o7QbUl37Vd85ply1vyD8auM=
|
||||||
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
||||||
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
|
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
|
||||||
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
|
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
|
|||||||
@@ -2,5 +2,16 @@
|
|||||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
"extends": [
|
"extends": [
|
||||||
"config:recommended"
|
"config:recommended"
|
||||||
|
],
|
||||||
|
"packageRules": [
|
||||||
|
{
|
||||||
|
"matchUpdateTypes": [
|
||||||
|
"minor",
|
||||||
|
"patch",
|
||||||
|
"digest"
|
||||||
|
],
|
||||||
|
"matchCurrentVersion": "!/^0/",
|
||||||
|
"automerge": true
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
16
utils/errs/errors_defer.go
Normal file
16
utils/errs/errors_defer.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package errs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Capture runs errFunc and assigns the error, if any, to *errPtr. Preserves the
|
||||||
|
// original error by wrapping with errors.Join if the errFunc err is non-nil.
|
||||||
|
func Capture(errPtr *error, errFunc func() error, msg string) {
|
||||||
|
err := errFunc()
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*errPtr = errors.Join(*errPtr, fmt.Errorf("%s: %w", msg, err))
|
||||||
|
}
|
||||||
58
utils/errs/errors_defer_test.go
Normal file
58
utils/errs/errors_defer_test.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package errs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCapture(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
initial error
|
||||||
|
errFunc func() error
|
||||||
|
msg string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "No error from errFunc",
|
||||||
|
initial: nil,
|
||||||
|
errFunc: func() error { return nil },
|
||||||
|
msg: "test message",
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Error from errFunc with no initial error",
|
||||||
|
initial: nil,
|
||||||
|
errFunc: func() error { return errors.New("error from func") },
|
||||||
|
msg: "test message",
|
||||||
|
expected: "test message: error from func",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Error from errFunc with initial error",
|
||||||
|
initial: errors.New("initial error"),
|
||||||
|
errFunc: func() error { return errors.New("error from func") },
|
||||||
|
msg: "test message",
|
||||||
|
expected: "initial error\ntest message: error from func",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Error from errFunc with initial wrapped error",
|
||||||
|
initial: fmt.Errorf("wrapped error: %w", errors.New("initial error")),
|
||||||
|
errFunc: func() error { return errors.New("error from func") },
|
||||||
|
msg: "test message",
|
||||||
|
expected: "wrapped error: initial error\ntest message: error from func",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var err error = tt.initial
|
||||||
|
Capture(&err, tt.errFunc, tt.msg)
|
||||||
|
if err != nil && err.Error() != tt.expected {
|
||||||
|
t.Errorf("expected %q, got %q", tt.expected, err.Error())
|
||||||
|
} else if err == nil && tt.expected != "" {
|
||||||
|
t.Errorf("expected %q, got nil", tt.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user