From fea3e368bc72cde6b5abcb9605a2263b47b7cbb9 Mon Sep 17 00:00:00 2001 From: Ryan Slade Date: Fri, 18 Oct 2024 13:10:00 +0200 Subject: [PATCH 1/2] Use filepath.Glob in bootstrap It does most of the work for us. --- cmd/bootstrap.go | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/cmd/bootstrap.go b/cmd/bootstrap.go index 8666190a..919c0d98 100644 --- a/cmd/bootstrap.go +++ b/cmd/bootstrap.go @@ -4,9 +4,7 @@ package cmd import ( "fmt" - "os" "path/filepath" - "slices" "github.com/spf13/cobra" ) @@ -27,22 +25,14 @@ in lexicographical order. All migrations are completed.`, defer m.Close() // open folder and read all json files - files, err := os.ReadDir(migrationsDir) + files, err := filepath.Glob(filepath.Join(migrationsDir, "*.json")) if err != nil { return fmt.Errorf("reading migration directory: %w", err) } - migrationFiles := []string{} - for _, file := range files { - if file.IsDir() || filepath.Ext(file.Name()) != ".json" { - continue - } - migrationFiles = append(migrationFiles, filepath.Join(migrationsDir, file.Name())) - } - slices.Sort(migrationFiles) - for _, fileName := range migrationFiles { - if err := runMigrationFromFile(cmd.Context(), m, fileName, true); err != nil { - return fmt.Errorf("running migration file '%s': %w", fileName, err) + for _, file := range files { + if err := runMigrationFromFile(cmd.Context(), m, file, true); err != nil { + return fmt.Errorf("running migration file '%s': %w", file, err) } } From 86ba3ce86ad3d1ab4d6ebcaa023ac22922e5e15f Mon Sep 17 00:00:00 2001 From: Ryan Slade Date: Fri, 18 Oct 2024 13:39:03 +0200 Subject: [PATCH 2/2] Use %q --- cmd/bootstrap.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/bootstrap.go b/cmd/bootstrap.go index 919c0d98..fc95a350 100644 --- a/cmd/bootstrap.go +++ b/cmd/bootstrap.go @@ -32,7 +32,7 @@ in lexicographical order. All migrations are completed.`, for _, file := range files { if err := runMigrationFromFile(cmd.Context(), m, file, true); err != nil { - return fmt.Errorf("running migration file '%s': %w", file, err) + return fmt.Errorf("running migration file %q: %w", file, err) } }