Skip to content

Commit dcf32bd

Browse files
authored
Merge pull request #163 from gofiber/codex/2025-08-22-19-10-42
2 parents 7ee1e42 + 68ce790 commit dcf32bd

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

cmd/internal/migrations/common.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@ import (
55
"os"
66
"regexp"
77
"strconv"
8-
"strings"
98

109
semver "github.com/Masterminds/semver/v3"
1110
"github.com/spf13/cobra"
1211

1312
"github.com/gofiber/cli/cmd/internal"
1413
)
1514

16-
var pkgRegex = regexp.MustCompile(`(github\.com\/gofiber\/fiber\/)(v\d+)( *?)(v[\w.-]+)`)
17-
18-
func MigrateGoPkgs(cmd *cobra.Command, cwd string, curr, target *semver.Version) error {
19-
pkgReplacer := strings.NewReplacer(
20-
"github.com/gofiber/fiber/v"+strconv.FormatUint(curr.Major(), 10),
21-
"github.com/gofiber/fiber/v"+strconv.FormatUint(target.Major(), 10),
22-
)
15+
var (
16+
pkgRegex = regexp.MustCompile(`(github\.com/gofiber/fiber/)(v\d+)( *?)(v[\w.-]+)`)
17+
pkgImportRegex = regexp.MustCompile(`(?m)^(\s*(?:[\w.]+\s+)?")github\.com/gofiber/fiber/v\d+("$)`)
18+
)
2319

20+
func MigrateGoPkgs(cmd *cobra.Command, cwd string, _, target *semver.Version) error {
2421
err := internal.ChangeFileContent(cwd, func(content string) string {
25-
return pkgReplacer.Replace(content)
22+
replacement := fmt.Sprintf("${1}github.com/gofiber/fiber/v%d${2}", target.Major())
23+
return pkgImportRegex.ReplaceAllString(content, replacement)
2624
})
2725
if err != nil {
2826
return fmt.Errorf("failed to migrate Go packages: %w", err)

cmd/migrate.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,18 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
7878
return fmt.Errorf("cannot get current working directory: %w", err)
7979
}
8080

81-
err = migrations.DoMigration(cmd, wd, currentVersion, targetVersion)
81+
migrateFrom := currentVersion
82+
migrateFromS := currentVersionS
83+
if opts.Force && !targetVersion.GreaterThan(currentVersion) {
84+
prevMajor := targetVersion.Major() - 1
85+
migrateFrom, err = semver.NewVersion(fmt.Sprintf("%d.0.0", prevMajor))
86+
if err != nil {
87+
return fmt.Errorf("invalid previous major version %d: %w", prevMajor, err)
88+
}
89+
migrateFromS = migrateFrom.String()
90+
}
91+
92+
err = migrations.DoMigration(cmd, wd, migrateFrom, targetVersion)
8293
if err != nil {
8394
return fmt.Errorf("migration failed %w", err)
8495
}
@@ -89,7 +100,7 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
89100
}
90101
}
91102

92-
msg := fmt.Sprintf("Migration from Fiber %s to %s", currentVersionS, opts.TargetVersionS)
103+
msg := fmt.Sprintf("Migration from Fiber %s to %s", migrateFromS, opts.TargetVersionS)
93104
cmd.Println(termenv.String(msg).
94105
Foreground(termenv.ANSIBrightBlue))
95106

cmd/migrate_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ require github.com/gofiber/fiber/v3 v3.0.0
177177
cmd := newMigrateCmd()
178178
out, err := runCobraCmd(cmd, "-t=3.0.0", "-f")
179179
require.NoError(t, err)
180-
assert.Contains(t, out, "Migration from Fiber 3.0.0 to 3.0.0")
180+
assert.Contains(t, out, "Migration from Fiber 2.0.0 to 3.0.0")
181+
assert.Contains(t, out, "Migrating Go packages")
181182
assert.Len(t, cmds, 3)
182183
})
183184

@@ -196,7 +197,42 @@ require github.com/gofiber/fiber/v3 v3.0.0
196197
cmd := newMigrateCmd()
197198
out, err := runCobraCmd(cmd, "-t=3.0.0", "-f", "-s")
198199
require.NoError(t, err)
199-
assert.Contains(t, out, "Migration from Fiber 3.0.0 to 3.0.0")
200+
assert.Contains(t, out, "Migration from Fiber 2.0.0 to 3.0.0")
201+
assert.Contains(t, out, "Migrating Go packages")
200202
assert.Empty(t, cmds)
201203
})
202204
}
205+
206+
func Test_Migrate_ForcePartialV3(t *testing.T) {
207+
dir := t.TempDir()
208+
209+
gomod := `module example
210+
211+
go 1.20
212+
213+
require github.com/gofiber/fiber/v3 v3.0.0
214+
`
215+
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(gomod), 0o600))
216+
217+
main := `package main
218+
import "github.com/gofiber/fiber/v2"
219+
func main() {}`
220+
require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte(main), 0o600))
221+
222+
cwd, err := os.Getwd()
223+
require.NoError(t, err)
224+
require.NoError(t, os.Chdir(dir))
225+
defer func() { require.NoError(t, os.Chdir(cwd)) }()
226+
227+
cmd := newMigrateCmd()
228+
setupCmd()
229+
defer teardownCmd()
230+
out, err := runCobraCmd(cmd, "-t=3.0.0", "-f")
231+
require.NoError(t, err)
232+
assert.Contains(t, out, "Migration from Fiber 2.0.0 to 3.0.0")
233+
assert.Contains(t, out, "Migrating Go packages")
234+
235+
content := readFileTB(t, filepath.Join(dir, "main.go"))
236+
assert.Contains(t, content, "github.com/gofiber/fiber/v3")
237+
assert.NotContains(t, content, "github.com/gofiber/fiber/v2")
238+
}

0 commit comments

Comments
 (0)