Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/internal/migrations/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(comment, false)
replacement := fmt.Sprintf("%s%s// TODO: migrate %s: %s", prefix, indent, field, val)
if comment != "" {
replacement = fmt.Sprintf("%s %s", replacement, comment)
Expand Down Expand Up @@ -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(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(comment string, hasMigrationMarker bool) string {
if comment == "" {
return comment
}
if strings.Contains(comment, "TODO: migrate") && hasMigrationMarker {
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.
Expand Down
17 changes: 17 additions & 0 deletions cmd/internal/migrations/v3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
3 changes: 1 addition & 2 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
if err != nil {
return fmt.Errorf("invalid version for \"%s\": %w", opts.TargetVersionS, err)
}

targetVersion := baseVersion
if opts.TargetHash != "" {
pv, err := pseudoVersionFromHash("gofiber/fiber", baseVersion, opts.TargetHash)
Expand Down Expand Up @@ -180,7 +179,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))

Expand Down
31 changes: 31 additions & 0 deletions cmd/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
18 changes: 8 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand Down
Loading