Skip to content

Commit 2444ead

Browse files
committed
Update CI, refactor code & use newest go version
1 parent 35d2f19 commit 2444ead

File tree

12 files changed

+268
-85
lines changed

12 files changed

+268
-85
lines changed

.github/workflows/build-release.yml

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@ jobs:
1919

2020
steps:
2121

22-
- uses: actions/checkout@v2
23-
24-
- uses: wangyoucao577/go-release-action@master
25-
with:
26-
github_token: ${{ secrets.GITHUB_TOKEN }}
27-
goos: ${{ matrix.goos }}
28-
goarch: ${{ matrix.goarch }}
29-
goversion: "https://golang.org/dl/go1.17.linux-amd64.tar.gz"
30-
ldflags: -X 'main.versionString=${{ github.event.release.tag_name }}'
31-
extra_files: LICENSE README.md
22+
- name: Set up Go
23+
uses: actions/setup-go@v2
24+
with:
25+
go-version: ^1.19
26+
27+
- name: Check out code into the Go module directory
28+
uses: actions/checkout@v2
29+
30+
- name: Build executable
31+
run: go build -v -ldflags="-X 'main.versionString=${{ github.event.release.tag_name }}'" .
32+
env:
33+
GOARCH: ${{ matrix.goarch }}
34+
GOOS: ${{ matrix.goos }}
35+
36+
- name: Create distribution archive
37+
run: go run -v ./scripts/dist
38+
39+
- name: Upload binary to release
40+
uses: svenstaro/upload-release-action@v2
41+
with:
42+
file: dist/dist.zip
43+
asset_name: batch-rename-${{ matrix.goos }}-${{ matrix.goarch }}.zip
44+
overwrite: true

.github/workflows/build-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111

12-
- name: Set up Go 1.x
12+
- name: Set up Go
1313
uses: actions/setup-go@v2
1414
with:
15-
go-version: ^1.17
15+
go-version: ^1.19
1616
id: go
1717

1818
- name: Check out code into the Go module directory

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ $RECYCLE.BIN/
7070

7171
# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)
7272

73+
/dist/

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"cSpell.words": [
33
"Dadido",
4-
"Vogel"
4+
"GOARCH",
5+
"goversion",
6+
"ldflags",
7+
"svenstaro",
8+
"Vogel",
9+
"wangyoucao"
510
]
611
}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 David Vogel
3+
Copyright (c) 2021-2022 David Vogel
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

file-entry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021 David Vogel
1+
// Copyright (c) 2021-2022 David Vogel
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT

file-handling.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2022 David Vogel
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
package main
7+
8+
import (
9+
"bufio"
10+
"fmt"
11+
"log"
12+
"os"
13+
"strconv"
14+
"strings"
15+
)
16+
17+
// createEntriesFile creates a new file with all the fileEntries and returns its file path.
18+
func createEntriesFile(rootDir string, fileEntries []fileEntry) (filePath string, err error) {
19+
// Create temporary file with paths.
20+
file, err := os.CreateTemp(".", "*.batch-rename")
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
decimalWidth := getDecimalWidth(uint(len(fileEntries)))
26+
27+
for i, fileEntry := range fileEntries {
28+
fmt.Fprintf(file, "%0"+fmt.Sprint(decimalWidth)+"d\t%s\n", i+1, fileEntry.originalPath)
29+
}
30+
31+
if err := file.Close(); err != nil {
32+
return "", err
33+
}
34+
35+
return file.Name(), nil
36+
}
37+
38+
// readEntriesFile reads the (edited) temporary file back and modifies the fileEntries with the new file paths.
39+
func readEntriesFile(filePath string, fileEntries []fileEntry) error {
40+
file, err := os.Open(filePath)
41+
if err != nil {
42+
return err
43+
}
44+
defer file.Close()
45+
46+
scanner := bufio.NewScanner(file)
47+
for scanner.Scan() {
48+
substrings := strings.SplitN(scanner.Text(), "\t", 2)
49+
entry, err := strconv.ParseUint(substrings[0], 10, 64)
50+
if err != nil {
51+
return fmt.Errorf("failed to parse element number: %w", err)
52+
}
53+
if entry <= 0 || entry > uint64(len(fileEntries)) {
54+
return fmt.Errorf("element number outside of valid range: Given %v, there are %v entries", entry, len(fileEntries))
55+
}
56+
fileEntries[entry-1].newPath = substrings[1]
57+
}
58+
59+
if err := scanner.Err(); err != nil {
60+
return fmt.Errorf("failed to scan file: %w", err)
61+
}
62+
63+
return nil
64+
}
65+
66+
// moveFiles renames the files according to the list of file entries.
67+
func moveFiles(fileEntries []fileEntry) error {
68+
for _, fileEntry := range fileEntries {
69+
if fileEntry.originalPath != fileEntry.newPath {
70+
if err := os.Rename(fileEntry.originalPath, fileEntry.newPath); err != nil {
71+
return err
72+
}
73+
log.Printf("Renamed %q to %q", fileEntry.originalPath, fileEntry.newPath)
74+
fileEntry.originalPath = fileEntry.newPath
75+
}
76+
}
77+
78+
return nil
79+
}

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
module github.com/Dadido3/batch-rename
22

3-
go 1.17
3+
go 1.19
44

5-
require github.com/coreos/go-semver v0.3.0
5+
require (
6+
github.com/coreos/go-semver v0.3.0
7+
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326
8+
)
69

710
require gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmfM=
22
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
3+
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
4+
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
35
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
46
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
57
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

main.go

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021 David Vogel
1+
// Copyright (c) 2021-2022 David Vogel
22
//
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
@@ -7,79 +7,12 @@ package main
77

88
import (
99
"bufio"
10-
"fmt"
1110
"io/fs"
1211
"log"
1312
"os"
1413
"path/filepath"
15-
"strconv"
16-
"strings"
1714
)
1815

19-
// createEntriesFile creates a new file with all the fileEntries and returns its file path.
20-
func createEntriesFile(rootDir string, fileEntries []fileEntry) (filePath string, err error) {
21-
// Create temporary file with paths.
22-
file, err := os.CreateTemp(".", "*.batch-rename")
23-
if err != nil {
24-
log.Fatal(err)
25-
}
26-
27-
decimalWidth := getDecimalWidth(uint(len(fileEntries)))
28-
29-
for i, fileEntry := range fileEntries {
30-
fmt.Fprintf(file, "%0"+fmt.Sprint(decimalWidth)+"d\t%s\n", i+1, fileEntry.originalPath)
31-
}
32-
33-
if err := file.Close(); err != nil {
34-
return "", err
35-
}
36-
37-
return file.Name(), nil
38-
}
39-
40-
// readEntriesFile reads the (edited) temporary file back and modifies the fileEntries with the new file paths.
41-
func readEntriesFile(filePath string, fileEntries []fileEntry) error {
42-
file, err := os.Open(filePath)
43-
if err != nil {
44-
return err
45-
}
46-
defer file.Close()
47-
48-
scanner := bufio.NewScanner(file)
49-
for scanner.Scan() {
50-
substrings := strings.SplitN(scanner.Text(), "\t", 2)
51-
entry, err := strconv.ParseUint(substrings[0], 10, 64)
52-
if err != nil {
53-
return fmt.Errorf("failed to parse element number: %w", err)
54-
}
55-
if entry <= 0 || entry > uint64(len(fileEntries)) {
56-
return fmt.Errorf("element number outside of valid range: Given %v, there are %v entries", entry, len(fileEntries))
57-
}
58-
fileEntries[entry-1].newPath = substrings[1]
59-
}
60-
61-
if err := scanner.Err(); err != nil {
62-
return fmt.Errorf("failed to scan file: %w", err)
63-
}
64-
65-
return nil
66-
}
67-
68-
// moveFiles renames the files according to the list of file entries.
69-
func moveFiles(fileEntries []fileEntry) error {
70-
for _, fileEntry := range fileEntries {
71-
if fileEntry.originalPath != fileEntry.newPath {
72-
if err := os.Rename(fileEntry.originalPath, fileEntry.newPath); err != nil {
73-
return err
74-
}
75-
log.Printf("Renamed %q to %q", fileEntry.originalPath, fileEntry.newPath)
76-
fileEntry.originalPath = fileEntry.newPath
77-
}
78-
}
79-
80-
return nil
81-
}
82-
8316
func main() {
8417
log.Printf("Started batch-rename v%v", version)
8518

0 commit comments

Comments
 (0)