diff --git a/connstring.go b/connstring.go index 2251d00..5b4e703 100644 --- a/connstring.go +++ b/connstring.go @@ -27,6 +27,7 @@ package main import ( "fmt" + "maps" "net/url" "sort" "strings" @@ -85,10 +86,7 @@ func (c *ConnInfo) Copy() *ConnInfo { Kind: c.Kind, Infos: make(map[string]string, len(c.Infos)), } - - for k, v := range c.Infos { - newC.Infos[k] = v - } + maps.Copy(newC.Infos, c.Infos) return &newC } diff --git a/go.mod b/go.mod index a3aa5b4..38172fc 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/orgrim/pg_back -go 1.23.0 +go 1.24.0 toolchain go1.24.1 diff --git a/hash_test.go b/hash_test.go index be86a25..f164c67 100644 --- a/hash_test.go +++ b/hash_test.go @@ -28,7 +28,7 @@ package main import ( "errors" "fmt" - "io/ioutil" + "io" "os" "os/exec" "path/filepath" @@ -50,7 +50,7 @@ func TestChecksumFile(t *testing.T) { // create a temporary directory to store a test file to // checksum with the different algorithm relatively - dir, err := ioutil.TempDir("", "test_checksum_file") + dir, err := os.MkdirTemp("", "test_checksum_file") if err != nil { t.Fatal("could not create tempdir:", err) } @@ -110,7 +110,7 @@ func TestChecksumFile(t *testing.T) { // bad files var e *os.PathError - l.logger.SetOutput(ioutil.Discard) + l.logger.SetOutput(io.Discard) if _, err := checksumFile("", 0o700, "sha1"); !errors.As(err, &e) { t.Errorf("expected an *os.PathError, got %q\n", err) } diff --git a/hook.go b/hook.go index 444f333..e776782 100644 --- a/hook.go +++ b/hook.go @@ -27,10 +27,11 @@ package main import ( "fmt" - "github.com/anmitsu/go-shlex" "os" "os/exec" "strings" + + "github.com/anmitsu/go-shlex" ) func hookCommand(cmd string, logPrefix string) error { @@ -51,7 +52,7 @@ func hookCommand(cmd string, logPrefix string) error { c := exec.Command(prog, args...) stdoutStderr, err := c.CombinedOutput() if err != nil { - for _, line := range strings.Split(string(stdoutStderr), "\n") { + for line := range strings.SplitSeq(string(stdoutStderr), "\n") { if line != "" { l.Errorln(logPrefix, line) } @@ -59,7 +60,7 @@ func hookCommand(cmd string, logPrefix string) error { return err } if len(stdoutStderr) > 0 { - for _, line := range strings.Split(string(stdoutStderr), "\n") { + for line := range strings.SplitSeq(string(stdoutStderr), "\n") { if line != "" { l.Infoln(logPrefix, line) } diff --git a/legacy.go b/legacy.go index 20ab0f6..a2b34a5 100644 --- a/legacy.go +++ b/legacy.go @@ -27,11 +27,11 @@ package main import ( "fmt" - "github.com/anmitsu/go-shlex" "io" - "io/ioutil" "os" "strings" + + "github.com/anmitsu/go-shlex" ) // Read the input file and return all lines that look like legacy configuration @@ -39,7 +39,7 @@ import ( func readLegacyConf(f io.Reader) ([]string, error) { var lines []string - data, err := ioutil.ReadAll(f) + data, err := io.ReadAll(f) if err != nil { return lines, fmt.Errorf("could not read file: %w", err) } @@ -239,7 +239,7 @@ func convertLegacyConf(oldConf []string) string { case "PGBK_DBLIST", "PGBK_EXCLUDE": // The separator for lists of databases now the comma dbs := make([]string, 0) - for _, d := range strings.Split(strings.Trim(value, "'\""), " ") { + for d := range strings.SplitSeq(strings.Trim(value, "'\""), " ") { if len(d) > 0 { dbs = append(dbs, d) } diff --git a/legacy_test.go b/legacy_test.go index 0b7a0ad..051b462 100644 --- a/legacy_test.go +++ b/legacy_test.go @@ -28,9 +28,10 @@ package main import ( "bytes" "fmt" + "testing" + "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "testing" ) func TestReadLegacyConf(t *testing.T) { diff --git a/lock.go b/lock.go index dff0624..0d8d526 100644 --- a/lock.go +++ b/lock.go @@ -23,6 +23,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//go:build !windows // +build !windows package main diff --git a/lock_test.go b/lock_test.go index 9bdfcaf..5167a54 100644 --- a/lock_test.go +++ b/lock_test.go @@ -27,7 +27,6 @@ package main import ( "errors" - "io/ioutil" "os" "path/filepath" "runtime" @@ -36,7 +35,7 @@ import ( func TestLockPath(t *testing.T) { // Work from a tempdir - dir, err := ioutil.TempDir("", "test_lockpath") + dir, err := os.MkdirTemp("", "test_lockpath") if err != nil { t.Fatal("could not create tempdir:", err) } @@ -83,7 +82,7 @@ func TestLockPath(t *testing.T) { } func TestUnlockPath(t *testing.T) { - f, err := ioutil.TempFile("", "test_unlockpath") + f, err := os.CreateTemp("", "test_unlockpath") if err != nil { t.Fatal("could not create tempfile") } diff --git a/lock_win.go b/lock_win.go index 3d8117d..4e27847 100644 --- a/lock_win.go +++ b/lock_win.go @@ -23,6 +23,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//go:build windows // +build windows package main diff --git a/main.go b/main.go index 9a97da0..fb3518d 100644 --- a/main.go +++ b/main.go @@ -706,7 +706,7 @@ func (d *dump) dump(fc chan<- sumFileJob) error { l.Verboseln("running:", pgDumpCmd) stdoutStderr, err := pgDumpCmd.CombinedOutput() if err != nil { - for _, line := range strings.Split(string(stdoutStderr), "\n") { + for line := range strings.SplitSeq(string(stdoutStderr), "\n") { if line != "" { l.Errorf("[%s] %s\n", dbname, line) } @@ -718,7 +718,7 @@ func (d *dump) dump(fc chan<- sumFileJob) error { return err } if len(stdoutStderr) > 0 { - for _, line := range strings.Split(string(stdoutStderr), "\n") { + for line := range strings.SplitSeq(string(stdoutStderr), "\n") { if line != "" { l.Infof("[%s] %s\n", dbname, line) } @@ -979,7 +979,7 @@ func dumpGlobals(dir string, mode int, timeFormat string, withRolePasswords bool l.Verboseln("running:", pgDumpallCmd) stdoutStderr, err := pgDumpallCmd.CombinedOutput() if err != nil { - for _, line := range strings.Split(string(stdoutStderr), "\n") { + for line := range strings.SplitSeq(string(stdoutStderr), "\n") { if line != "" { l.Errorln(line) } @@ -987,7 +987,7 @@ func dumpGlobals(dir string, mode int, timeFormat string, withRolePasswords bool return err } if len(stdoutStderr) > 0 { - for _, line := range strings.Split(string(stdoutStderr), "\n") { + for line := range strings.SplitSeq(string(stdoutStderr), "\n") { if line != "" { l.Infoln(line) } diff --git a/purge_test.go b/purge_test.go index c8040ee..2c35e00 100644 --- a/purge_test.go +++ b/purge_test.go @@ -27,7 +27,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "runtime" @@ -38,7 +37,7 @@ import ( // func purgeDumps(directory string, dbname string, keep int, limit time.Time) error func TestPurgeDumps(t *testing.T) { // work in a tempdir - dir, err := ioutil.TempDir("", "test_purge_dumps") + dir, err := os.MkdirTemp("", "test_purge_dumps") if err != nil { t.Fatal("could not create tempdir:", err) } @@ -81,7 +80,7 @@ func TestPurgeDumps(t *testing.T) { // file without write perms if runtime.GOOS != "windows" { tf = formatDumpPath(wd, time.RFC3339, "dump", "db", time.Now().Add(-time.Hour), 0) - ioutil.WriteFile(tf, []byte("truc\n"), 0644) + os.WriteFile(tf, []byte("truc\n"), 0644) os.Chmod(filepath.Dir(tf), 0555) err = purgeDumps(wd, "db", 0, time.Now()) @@ -139,7 +138,7 @@ func TestPurgeDumps(t *testing.T) { for i := 1; i <= 3; i++ { when := time.Now().Add(-time.Hour * time.Duration(i)) tf = formatDumpPath(wd, st.format, "dump", "db", when, 0) - ioutil.WriteFile(tf, []byte("truc\n"), 0644) + os.WriteFile(tf, []byte("truc\n"), 0644) os.Chtimes(tf, when, when) } diff --git a/sql.go b/sql.go index 8c6d885..71df37c 100644 --- a/sql.go +++ b/sql.go @@ -29,10 +29,11 @@ import ( "database/sql" "errors" "fmt" - "github.com/jackc/pgtype" - _ "github.com/jackc/pgx/v4/stdlib" "strings" "time" + + "github.com/jackc/pgtype" + _ "github.com/jackc/pgx/v4/stdlib" ) type pg struct { diff --git a/sql_test.go b/sql_test.go index b5b31a8..6e55ffb 100644 --- a/sql_test.go +++ b/sql_test.go @@ -27,12 +27,13 @@ package main import ( "fmt" - "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "os" "regexp" "strings" "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" ) var (