From 03dc6aeec89a1eff9932e4c18923138d5bfde7a8 Mon Sep 17 00:00:00 2001 From: RW Date: Mon, 2 Feb 2026 20:39:33 +0100 Subject: [PATCH 1/2] Fix migration output formatting --- cmd/internal/migrations/v3/common.go | 12 +++++++++ cmd/internal/migrations/v3/common_test.go | 17 +++++++++++++ cmd/migrate.go | 3 ++- cmd/migrate_test.go | 31 +++++++++++++++++++++++ go.mod | 8 +++--- go.sum | 18 ++++++------- 6 files changed, 74 insertions(+), 15 deletions(-) diff --git a/cmd/internal/migrations/v3/common.go b/cmd/internal/migrations/v3/common.go index 00594b3..b2e35a1 100644 --- a/cmd/internal/migrations/v3/common.go +++ b/cmd/internal/migrations/v3/common.go @@ -265,6 +265,7 @@ func replaceFieldImpl(src, field string, unquote bool, fn func(indent, val, comm if unquote { uq, err := strconv.Unquote(val) if err != nil { + comment = normalizeMigrationComment(field, comment) replacement := fmt.Sprintf("%s%s// TODO: migrate %s: %s", prefix, indent, field, val) if comment != "" { replacement = fmt.Sprintf("%s %s", replacement, comment) @@ -605,12 +606,23 @@ func ExtractCommentAndValue(line string) (value, comment string) { // FormatFieldWithComment formats a field assignment with consistent spacing // for indentation, value, comma, comment, and newline. func FormatFieldWithComment(indent, fieldName, value, comma, comment, newline string) string { + comment = normalizeMigrationComment(fieldName, comment) if comment != "" { comment = " " + comment } return fmt.Sprintf("%s%s: %s%s%s%s", indent, fieldName, value, comma, comment, newline) } +func normalizeMigrationComment(fieldName, comment string) string { + if comment == "" { + return comment + } + if strings.Contains(comment, "TODO: migrate") && strings.Contains(fieldName, "TODO: migrate") { + return "" + } + return comment +} + // IterateConfigBlocks finds all occurrences matching the given regex pattern, // extracts their config blocks using braces, processes each block with the // provided function, and reconstructs the content. diff --git a/cmd/internal/migrations/v3/common_test.go b/cmd/internal/migrations/v3/common_test.go index 3984e8b..10e2c10 100644 --- a/cmd/internal/migrations/v3/common_test.go +++ b/cmd/internal/migrations/v3/common_test.go @@ -8,6 +8,8 @@ import ( "github.com/spf13/cobra" "github.com/stretchr/testify/require" + + "github.com/gofiber/cli/cmd/internal/migrations/v3" ) func writeTempFile(t *testing.T, dir, content string) string { @@ -31,3 +33,18 @@ func newCmd(buf *bytes.Buffer) *cobra.Command { cmd.SetErr(buf) return cmd } + +func TestFormatFieldWithComment_SkipsDuplicateMigrationComment(t *testing.T) { + t.Parallel() + + formatted := v3.FormatFieldWithComment( + " ", + "// TODO: migrate KeyLookup", + `strings.Join([]string{"cookie", "session_id"}, ":")`, + ",", + "// TODO: migrate KeyLookup: strings.Join([]string{\"cookie\", \"session_id\"}, \":\")", + "\n", + ) + + require.Equal(t, " // TODO: migrate KeyLookup: strings.Join([]string{\"cookie\", \"session_id\"}, \":\"),\n", formatted) +} diff --git a/cmd/migrate.go b/cmd/migrate.go index 49bc1de..2e3dedd 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -111,6 +111,7 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error { if err != nil { return fmt.Errorf("invalid version for \"%s\": %w", opts.TargetVersionS, err) } + opts.TargetVersionS = baseVersion.String() targetVersion := baseVersion if opts.TargetHash != "" { @@ -180,7 +181,7 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error { } } - msg := fmt.Sprintf("Migration from Fiber %s to %s", migrateFromS, opts.TargetVersionS) + msg := fmt.Sprintf("Migration from Fiber %s to %s", migrateFromS, targetVersion.String()) cmd.Println(termenv.String(msg). Foreground(termenv.ANSIBrightBlue)) diff --git a/cmd/migrate_test.go b/cmd/migrate_test.go index a589ae9..bc1a1f8 100644 --- a/cmd/migrate_test.go +++ b/cmd/migrate_test.go @@ -200,6 +200,37 @@ func main() { assert.Contains(t, content, "github.com/gofiber/fiber/v3 v3.0.0") } +func Test_Migrate_TargetVersionShort(t *testing.T) { + dir, err := os.MkdirTemp("", "migrate_short_version") + require.NoError(t, err) + defer func() { require.NoError(t, os.RemoveAll(dir)) }() + + require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goModV2), 0o600)) + + main := `package main +import "github.com/gofiber/fiber/v2" +func main() { + _ = fiber.New() +}` + require.NoError(t, os.WriteFile(filepath.Join(dir, "main.go"), []byte(main), 0o600)) + + cwd, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, os.Chdir(dir)) + defer func() { require.NoError(t, os.Chdir(cwd)) }() + + cmd := newMigrateCmd() + setupCmd() + defer teardownCmd() + out, err := runCobraCmd(cmd, "-t=3") + require.NoError(t, err) + + assert.Contains(t, out, "Migration from Fiber 2.0.6 to 3.0.0") + + content := readFileTB(t, filepath.Join(dir, "go.mod")) + assert.Contains(t, content, "github.com/gofiber/fiber/v3 v3.0.0") +} + func Test_RunGoMod(t *testing.T) { dir := t.TempDir() diff --git a/go.mod b/go.mod index 3483716..f3ca1c0 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/charmbracelet/bubbletea v1.3.10 github.com/containerd/console v1.0.5 github.com/fsnotify/fsnotify v1.9.0 - github.com/gofiber/fiber/v3 v3.0.0-rc.3 + github.com/gofiber/fiber/v3 v3.0.0 github.com/gofrs/flock v0.13.0 github.com/jarcoal/httpmock v1.4.1 github.com/muesli/termenv v0.16.0 @@ -35,7 +35,7 @@ require ( github.com/gofiber/utils/v2 v2.0.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect - github.com/klauspost/compress v1.18.1 // indirect + github.com/klauspost/compress v1.18.3 // indirect github.com/kr/text v0.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-colorable v0.1.14 // indirect @@ -48,9 +48,9 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/rogpeppe/go-internal v1.14.1 // indirect - github.com/tinylib/msgp v1.5.0 // indirect + github.com/tinylib/msgp v1.6.3 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.68.0 // indirect + github.com/valyala/fasthttp v1.69.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect golang.org/x/crypto v0.47.0 // indirect golang.org/x/net v0.49.0 // indirect diff --git a/go.sum b/go.sum index feb7cea..37215b2 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,8 @@ github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM= github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ= -github.com/gofiber/fiber/v3 v3.0.0-rc.3 h1:h0KXuRHbivSslIpoHD1R/XjUsjcGwt+2vK0avFiYonA= -github.com/gofiber/fiber/v3 v3.0.0-rc.3/go.mod h1:LNBPuS/rGoUFlOyy03fXsWAeWfdGoT1QytwjRVNSVWo= +github.com/gofiber/fiber/v3 v3.0.0 h1:GPeCG8X60L42wLKrzgeewDHBr6pE6veAvwaXsqD3Xjk= +github.com/gofiber/fiber/v3 v3.0.0/go.mod h1:kVZiO/AwyT5Pq6PgC8qRCJ+j/BHrMy5jNw1O9yH38aY= github.com/gofiber/schema v1.6.0 h1:rAgVDFwhndtC+hgV7Vu5ItQCn7eC2mBA4Eu1/ZTiEYY= github.com/gofiber/schema v1.6.0/go.mod h1:WNZWpQx8LlPSK7ZaX0OqOh+nQo/eW2OevsXs1VZfs/s= github.com/gofiber/utils/v2 v2.0.0 h1:SCC3rpsEDWupFSHtc0RKxg/BKgV0s1qKfZg9Jv6D0sM= @@ -48,8 +48,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jarcoal/httpmock v1.4.1 h1:0Ju+VCFuARfFlhVXFc2HxlcQkfB+Xq12/EotHko+x2A= github.com/jarcoal/httpmock v1.4.1/go.mod h1:ftW1xULwo+j0R0JJkJIIi7UKigZUXCLLanykgjwBXL0= -github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co= -github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0= +github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw= +github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -82,8 +82,6 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/shamaton/msgpack/v2 v2.4.0 h1:O5Z08MRmbo0lA9o2xnQ4TXx6teJbPqEurqcCOQ8Oi/4= -github.com/shamaton/msgpack/v2 v2.4.0/go.mod h1:6khjYnkx73f7VQU7wjcFS9DFjs+59naVWJv1TB7qdOI= github.com/shamaton/msgpack/v3 v3.0.0 h1:xl40uxWkSpwBCSTvS5wyXvJRsC6AcVcYeox9PspKiZg= github.com/shamaton/msgpack/v3 v3.0.0/go.mod h1:DcQG8jrdrQCIxr3HlMYkiXdMhK+KfN2CitkyzsQV4uc= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= @@ -93,12 +91,12 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= -github.com/tinylib/msgp v1.5.0 h1:GWnqAE54wmnlFazjq2+vgr736Akg58iiHImh+kPY2pc= -github.com/tinylib/msgp v1.5.0/go.mod h1:cvjFkb4RiC8qSBOPMGPSzSAx47nAsfhLVTCZZNuHv5o= +github.com/tinylib/msgp v1.6.3 h1:bCSxiTz386UTgyT1i0MSCvdbWjVW+8sG3PjkGsZQt4s= +github.com/tinylib/msgp v1.6.3/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.68.0 h1:v12Nx16iepr8r9ySOwqI+5RBJ/DqTxhOy1HrHoDFnok= -github.com/valyala/fasthttp v1.68.0/go.mod h1:5EXiRfYQAoiO/khu4oU9VISC/eVY6JqmSpPJoHCKsz4= +github.com/valyala/fasthttp v1.69.0 h1:fNLLESD2SooWeh2cidsuFtOcrEi4uB4m1mPrkJMZyVI= +github.com/valyala/fasthttp v1.69.0/go.mod h1:4wA4PfAraPlAsJ5jMSqCE2ug5tqUPwKXxVj8oNECGcw= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= From c18048d5e0cee866e06a264061ea49b3634140b1 Mon Sep 17 00:00:00 2001 From: RW Date: Mon, 2 Feb 2026 21:07:42 +0100 Subject: [PATCH 2/2] Refine migration comment handling --- cmd/internal/migrations/v3/common.go | 8 ++++---- cmd/migrate.go | 2 -- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/internal/migrations/v3/common.go b/cmd/internal/migrations/v3/common.go index b2e35a1..ea18610 100644 --- a/cmd/internal/migrations/v3/common.go +++ b/cmd/internal/migrations/v3/common.go @@ -265,7 +265,7 @@ func replaceFieldImpl(src, field string, unquote bool, fn func(indent, val, comm if unquote { uq, err := strconv.Unquote(val) if err != nil { - comment = normalizeMigrationComment(field, comment) + comment = normalizeMigrationComment(comment, false) replacement := fmt.Sprintf("%s%s// TODO: migrate %s: %s", prefix, indent, field, val) if comment != "" { replacement = fmt.Sprintf("%s %s", replacement, comment) @@ -606,18 +606,18 @@ func ExtractCommentAndValue(line string) (value, comment string) { // FormatFieldWithComment formats a field assignment with consistent spacing // for indentation, value, comma, comment, and newline. func FormatFieldWithComment(indent, fieldName, value, comma, comment, newline string) string { - comment = normalizeMigrationComment(fieldName, comment) + comment = normalizeMigrationComment(comment, strings.Contains(fieldName, "TODO: migrate")) if comment != "" { comment = " " + comment } return fmt.Sprintf("%s%s: %s%s%s%s", indent, fieldName, value, comma, comment, newline) } -func normalizeMigrationComment(fieldName, comment string) string { +func normalizeMigrationComment(comment string, hasMigrationMarker bool) string { if comment == "" { return comment } - if strings.Contains(comment, "TODO: migrate") && strings.Contains(fieldName, "TODO: migrate") { + if strings.Contains(comment, "TODO: migrate") && hasMigrationMarker { return "" } return comment diff --git a/cmd/migrate.go b/cmd/migrate.go index 2e3dedd..1ab98b3 100644 --- a/cmd/migrate.go +++ b/cmd/migrate.go @@ -111,8 +111,6 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error { if err != nil { return fmt.Errorf("invalid version for \"%s\": %w", opts.TargetVersionS, err) } - opts.TargetVersionS = baseVersion.String() - targetVersion := baseVersion if opts.TargetHash != "" { pv, err := pseudoVersionFromHash("gofiber/fiber", baseVersion, opts.TargetHash)