-
Notifications
You must be signed in to change notification settings - Fork 15
Update migration for moved config fields #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -364,17 +364,98 @@ func MigrateTrustedProxyConfig(cmd *cobra.Command, cwd string, _, _ *semver.Vers | |
// in Fiber v3. It renames Prefork and Network fields and adapts them to the new | ||
// listener configuration fields. | ||
func MigrateConfigListenerFields(cmd *cobra.Command, cwd string, _, _ *semver.Version) error { | ||
var disableStartup string | ||
var enablePrint string | ||
|
||
err := internal.ChangeFileContent(cwd, func(content string) string { | ||
replacer := strings.NewReplacer( | ||
"Prefork:", "EnablePrefork:", | ||
"Network:", "ListenerNetwork:", | ||
) | ||
return replacer.Replace(content) | ||
content = replacer.Replace(content) | ||
|
||
reStartup := regexp.MustCompile(`(?m)^\s*DisableStartupMessage:\s*([^,]+),?\n`) | ||
content = reStartup.ReplaceAllStringFunc(content, func(s string) string { | ||
if disableStartup == "" { | ||
sub := reStartup.FindStringSubmatch(s) | ||
if len(sub) > 1 { | ||
disableStartup = strings.TrimSpace(sub[1]) | ||
} | ||
} | ||
return "" | ||
}) | ||
|
||
rePrint := regexp.MustCompile(`(?m)^\s*EnablePrintRoutes:\s*([^,]+),?\n`) | ||
content = rePrint.ReplaceAllStringFunc(content, func(s string) string { | ||
if enablePrint == "" { | ||
sub := rePrint.FindStringSubmatch(s) | ||
if len(sub) > 1 { | ||
enablePrint = strings.TrimSpace(sub[1]) | ||
} | ||
} | ||
return "" | ||
}) | ||
|
||
return content | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to migrate listener related config fields: %w", err) | ||
} | ||
|
||
err = internal.ChangeFileContent(cwd, func(content string) string { | ||
if disableStartup == "" && enablePrint == "" { | ||
return content | ||
} | ||
|
||
reWithCfg := regexp.MustCompile(`\.Listen\(([^,]+),\s*fiber.ListenConfig\{([^}]*)\}\)`) | ||
content = reWithCfg.ReplaceAllStringFunc(content, func(s string) string { | ||
sub := reWithCfg.FindStringSubmatch(s) | ||
addr := sub[1] | ||
cfg := strings.TrimSpace(sub[2]) | ||
|
||
if disableStartup != "" && !strings.Contains(cfg, "DisableStartupMessage:") { | ||
if len(cfg) > 0 && !strings.HasSuffix(cfg, ",") { | ||
cfg += "," | ||
} | ||
cfg += " DisableStartupMessage: " + disableStartup + "," | ||
} | ||
if enablePrint != "" && !strings.Contains(cfg, "EnablePrintRoutes:") { | ||
if len(cfg) > 0 && !strings.HasSuffix(cfg, ",") { | ||
cfg += "," | ||
} | ||
cfg += " EnablePrintRoutes: " + enablePrint + "," | ||
} | ||
|
||
cfg = strings.TrimSuffix(cfg, ",") | ||
return fmt.Sprintf(".Listen(%s, fiber.ListenConfig{%s})", addr, cfg) | ||
}) | ||
|
||
rePlain := regexp.MustCompile(`\.Listen\(([^,\n)]+)\)`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This regular expression can be made more specific to only match rePlain := regexp.MustCompile(`\.Listen\(([^,\n)]+)\s*\)`) |
||
content = rePlain.ReplaceAllStringFunc(content, func(s string) string { | ||
if strings.Contains(s, "fiber.ListenConfig") { | ||
return s | ||
} | ||
Comment on lines
+435
to
+437
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
addr := rePlain.FindStringSubmatch(s)[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
fields := "" | ||
if disableStartup != "" { | ||
fields += "DisableStartupMessage: " + disableStartup + ", " | ||
} | ||
if enablePrint != "" { | ||
fields += "EnablePrintRoutes: " + enablePrint + ", " | ||
} | ||
fields = strings.TrimSpace(fields) | ||
fields = strings.TrimSuffix(fields, ",") | ||
|
||
return fmt.Sprintf(".Listen(%s, fiber.ListenConfig{%s})", addr, fields) | ||
}) | ||
|
||
return content | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to migrate listener related listen calls: %w", err) | ||
} | ||
|
||
cmd.Println("Migrating listener related config fields") | ||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing slice elements from
FindStringSubmatch
without checking the slice length can lead to a panic if the regex matches but fails to capture the expected groups. It's safer to check the length ofsub
before accessing its elements.