Skip to content

Commit 8d00b49

Browse files
committed
feat(migrator): enhance client usage migration by removing unused fiber imports and handling deprecated methods
1 parent 14ad351 commit 8d00b49

File tree

2 files changed

+333
-1
lines changed

2 files changed

+333
-1
lines changed

cmd/internal/migrations/v3/client_usage.go

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
var (
18+
// Multi-line patterns for simple agent usage
1819
clientBytesWithBodyPattern = regexp.MustCompile(`(?m)([ \t]*)(\w+)\s*:=\s*fiber\.(Get|Head|Post|Put|Patch|Delete)\(([^)]*)\)\s*\n([ \t]*)(\w+)\.(Body|BodyString)\(([^)]*)\)\s*\n([ \t]*)(\w+)\s*,\s*(\w+)\s*,\s*errs\s*:=\s*(\w+)\.Bytes\(\)`)
1920
clientBytesPattern = regexp.MustCompile(`(?m)([ \t]*)(\w+)\s*:=\s*fiber\.(Get|Head|Post|Put|Patch|Delete)\(([^)]*)\)\s*\n([ \t]*)(\w+)\s*,\s*(\w+)\s*,\s*errs\s*:=\s*(\w+)\.Bytes\(\)`)
2021
clientStringWithBodyPattern = regexp.MustCompile(`(?m)([ \t]*)(\w+)\s*:=\s*fiber\.(Get|Head|Post|Put|Patch|Delete)\(([^)]*)\)\s*\n([ \t]*)(\w+)\.(Body|BodyString)\(([^)]*)\)\s*\n([ \t]*)(\w+)\s*,\s*(\w+)\s*,\s*errs\s*:=\s*(\w+)\.String\(\)`)
@@ -28,7 +29,9 @@ var (
2829
clientErrVarPattern = regexp.MustCompile(`\berrs\b`)
2930
clientErrsDeclPattern = regexp.MustCompile(`\berrs\s+\[]error\b`)
3031

32+
// AcquireAgent patterns
3133
acquireAgentPattern = regexp.MustCompile(`(?m)^([ \t]*)(\w+)\s*:=\s*fiber\.AcquireAgent\(\)\s*$`)
34+
releaseAgentPattern = regexp.MustCompile(`(?m)^([ \t]*)defer\s+fiber\.ReleaseAgent\((\w+)\)\s*$`)
3235
requestFromAgent = regexp.MustCompile(`^([ \t]*)(\w+)\s*:=\s*(\w+)\.Request\(\)\s*$`)
3336
headerMethodPattern = regexp.MustCompile(`^([ \t]*)(\w+)\.Header\.SetMethod\(([^)]*)\)\s*$`)
3437
headerSetPattern = regexp.MustCompile(`^([ \t]*)(\w+)\.Header\.Set\(([^,]+),\s*([^)]*)\)\s*$`)
@@ -48,6 +51,8 @@ var (
4851
bodyPattern = regexp.MustCompile(`^([ \t]*)(\w+)\.(Body|BodyString)\(([^)]*)\)\s*$`)
4952
basicAuthPattern = regexp.MustCompile(`^([ \t]*)(\w+)\.BasicAuth\(([^,]+),\s*([^)]*)\)\s*$`)
5053
tlsConfigPattern = regexp.MustCompile(`^([ \t]*)(\w+)\.TLSConfig\(([^)]*)\)\s*$`)
54+
debugPattern = regexp.MustCompile(`^([ \t]*)(\w+)\.Debug\(([^)]*)\)\s*$`)
55+
reusePattern = regexp.MustCompile(`^([ \t]*)(\w+)\.Reuse\(\)\s*$`)
5156
agentBytesCallPattern = regexp.MustCompile(`^([ \t]*)([^,]+),\s*([^,]+),\s*errs\s*(=|:=)\s*(\w+)\.Bytes\(\)\s*$`)
5257
agentStringCallPattern = regexp.MustCompile(`^([ \t]*)([^,]+),\s*([^,]+),\s*errs\s*(=|:=)\s*(\w+)\.String\(\)\s*$`)
5358
agentStructCallPattern = regexp.MustCompile(`^([ \t]*)([^,]+),\s*([^,]+),\s*errs\s*(=|:=)\s*(\w+)\.Struct\((.+)\)\s*$`)
@@ -65,6 +70,7 @@ func MigrateClientUsage(cmd *cobra.Command, cwd string, _, _ *semver.Version) er
6570
}
6671

6772
updated = rewriteClientErrorHandling(updated)
73+
updated = removeUnusedFiberImport(updated)
6874
return updated
6975
})
7076
if err != nil {
@@ -82,8 +88,21 @@ func rewriteAcquireAgentBlocks(content string) (string, bool) {
8288
lines := strings.Split(content, "\n")
8389
var out []string
8490
changed := false
91+
skipLines := make(map[int]bool)
92+
93+
// First pass: mark ReleaseAgent defer lines for removal
94+
for i, line := range lines {
95+
if releaseAgentPattern.MatchString(line) {
96+
skipLines[i] = true
97+
}
98+
}
8599

86100
for i := 0; i < len(lines); i++ {
101+
if skipLines[i] {
102+
changed = true
103+
continue
104+
}
105+
87106
line := lines[i]
88107
acquire := acquireAgentPattern.FindStringSubmatch(line)
89108
if acquire == nil {
@@ -97,8 +116,11 @@ func rewriteAcquireAgentBlocks(content string) (string, bool) {
97116
reqLine := -1
98117
var reqMatch []string
99118
for j := i + 1; j < len(lines); j++ {
119+
if skipLines[j] {
120+
continue
121+
}
100122
trimmed := strings.TrimSpace(lines[j])
101-
if trimmed == "" || strings.Contains(lines[j], "ReleaseAgent("+agentVar+")") || strings.HasPrefix(trimmed, "//") {
123+
if trimmed == "" || strings.HasPrefix(trimmed, "//") {
102124
continue
103125
}
104126

@@ -121,6 +143,9 @@ func rewriteAcquireAgentBlocks(content string) (string, bool) {
121143

122144
j := reqLine + 1
123145
for ; j < len(lines); j++ {
146+
if skipLines[j] {
147+
continue
148+
}
124149
l := lines[j]
125150
if m := headerMethodPattern.FindStringSubmatch(l); len(m) > 0 && m[2] == reqVar {
126151
methodExpr = strings.TrimSpace(m[3])
@@ -356,6 +381,7 @@ type simpleAgentConfig struct {
356381
body string
357382
timeout string
358383
tlsConfig string
384+
debug bool
359385
config bool
360386
}
361387

@@ -454,6 +480,15 @@ func rewriteSimpleAgentBlocks(content string) (string, bool) {
454480
cfg.config = true
455481
continue
456482
}
483+
// Handle Debug() - will be removed as v3 uses hooks instead
484+
if m := debugPattern.FindStringSubmatch(l); len(m) > 0 && m[2] == varName {
485+
cfg.debug = true
486+
continue
487+
}
488+
// Handle Reuse() - not needed in v3, just skip
489+
if m := reusePattern.FindStringSubmatch(l); len(m) > 0 && m[2] == varName {
490+
continue
491+
}
457492
if m := agentBytesCallPattern.FindStringSubmatch(l); len(m) > 0 && m[5] == varName {
458493
replacement = buildSimpleAgentReplacement(indent, urlExpr, method, cfg, m[2], m[3], m[4], varName, "bytes", "", m[1])
459494
callFound = true
@@ -746,3 +781,42 @@ func rewriteClientErrorHandling(content string) string {
746781
updated = clientErrVarPattern.ReplaceAllString(updated, "err")
747782
return updated
748783
}
784+
785+
// removeUnusedFiberImport removes unused fiber/v2 or fiber/v3 imports
786+
// when they are no longer needed after migration to the client package
787+
func removeUnusedFiberImport(content string) string {
788+
// Check if fiber is still used somewhere in the code (excluding imports)
789+
fiberUsagePattern := regexp.MustCompile(`\bfiber\.(Get|Head|Post|Put|Patch|Delete|AcquireAgent|ReleaseAgent)\b`)
790+
if fiberUsagePattern.MatchString(content) {
791+
return content
792+
}
793+
794+
// Remove import line for fiber/v2 or fiber/v3 if no longer used
795+
// But only remove if there's no other usage of 'fiber.' in the code
796+
fiberAnyUsage := regexp.MustCompile(`\bfiber\.`)
797+
importContent := extractImportSection(content)
798+
799+
// Check if fiber is used outside of imports
800+
contentWithoutImports := strings.Replace(content, importContent, "", 1)
801+
if fiberAnyUsage.MatchString(contentWithoutImports) {
802+
return content
803+
}
804+
805+
// Remove the fiber import line
806+
fiberImportLine := regexp.MustCompile(`(?m)^\s*"github\.com/gofiber/fiber/v[23]"\s*\n?`)
807+
return fiberImportLine.ReplaceAllString(content, "")
808+
}
809+
810+
func extractImportSection(content string) string {
811+
blockRegex := regexp.MustCompile(`(?ms)^import \([^)]*\)`)
812+
if match := blockRegex.FindString(content); match != "" {
813+
return match
814+
}
815+
816+
singleImport := regexp.MustCompile(`(?m)^import\s+"[^"]+"\s*$`)
817+
if match := singleImport.FindString(content); match != "" {
818+
return match
819+
}
820+
821+
return ""
822+
}

0 commit comments

Comments
 (0)