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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fiber migrate --to 3.0.0
-f, --force Force migration even if already on the version
-s, --skip_go_mod Skip running go mod tidy, download and vendor
--hash string Commit hash for Fiber version
--third-party strings Refresh third-party modules (contrib,storage,template). Provide a comma-separated list and optionally append @<commit> to pin a commit
-v, --verbose Enable verbose output
-h, --help help for migrate
```
Expand Down
55 changes: 52 additions & 3 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/spf13/cobra"
"golang.org/x/mod/module"

"github.com/gofiber/cli/cmd/internal"
"github.com/gofiber/cli/cmd/internal/migrations"
)

Expand All @@ -25,6 +26,7 @@ func newMigrateCmd() *cobra.Command {
var force bool
var skipGoMod bool
var verbose bool
var thirdParty []string

cmd := &cobra.Command{
Use: "migrate",
Expand All @@ -36,15 +38,29 @@ func newMigrateCmd() *cobra.Command {
cmd.Flags().BoolVarP(&skipGoMod, "skip_go_mod", "s", false, "Skip running go mod tidy, download and vendor")
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")
cmd.Flags().StringVar(&targetHash, "hash", "", "Commit hash for Fiber version")
cmd.Flags().StringSliceVar(&thirdParty, "third-party", nil, "Refresh third-party modules (contrib,storage,template). Use a comma-separated list like --third-party=contrib,storage and append @<commit> to pin a commit")

cmd.RunE = func(cmd *cobra.Command, _ []string) error {
tps := make([]ThirdPartyParam, 0, len(thirdParty))
for _, tp := range thirdParty {
parts := strings.SplitN(tp, "@", 2)
p := ThirdPartyParam{Name: parts[0]}
if len(parts) == 2 {
p.Hash = parts[1]
}
if p.Name != "" {
tps = append(tps, p)
}
}

return migrateRunE(cmd, MigrateOptions{
CurrentVersionFile: currentVersionFile,
TargetVersionS: targetVersionS,
TargetHash: targetHash,
Force: force,
SkipGoMod: skipGoMod,
Verbose: verbose,
ThirdParty: tps,
})
}

Expand All @@ -57,11 +73,17 @@ type MigrateOptions struct {
CurrentVersionFile string
TargetVersionS string
TargetHash string
ThirdParty []ThirdPartyParam
Force bool
SkipGoMod bool
Verbose bool
}

type ThirdPartyParam struct {
Name string
Hash string
}

func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
currentVersionS, err := currentVersionFromFile(opts.CurrentVersionFile)
if err != nil {
Expand All @@ -84,7 +106,7 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {

targetVersion := baseVersion
if opts.TargetHash != "" {
pv, err := pseudoVersionFromHash(baseVersion, opts.TargetHash)
pv, err := pseudoVersionFromHash("gofiber/fiber", baseVersion, opts.TargetHash)
if err != nil {
return fmt.Errorf("pseudo version: %w", err)
}
Expand Down Expand Up @@ -121,15 +143,42 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
return fmt.Errorf("migration failed %w", err)
}

tpChanged := false
for _, tp := range opts.ThirdParty {
var (
changed bool
err error
)
switch tp.Name {
case "contrib":
changed, err = refreshContrib(cmd, wd, tp.Hash)
case "storage":
changed, err = refreshStorage(cmd, wd, tp.Hash)
case "template", "templates":
changed, err = refreshTemplates(cmd, wd, tp.Hash)
}
if err != nil {
return fmt.Errorf("refresh %s packages: %w", tp.Name, err)
}
if changed {
tpChanged = true
}
}
if tpChanged && !opts.SkipGoMod {
if err := internal.RunGoMod(wd); err != nil {
return fmt.Errorf("go mod: %w", err)
}
}

msg := fmt.Sprintf("Migration from Fiber %s to %s", migrateFromS, opts.TargetVersionS)
cmd.Println(termenv.String(msg).
Foreground(termenv.ANSIBrightBlue))

return nil
}

func pseudoVersionFromHash(base *semver.Version, hash string) (string, error) {
url := "https://api.github.com/repos/gofiber/fiber/commits/" + hash
func pseudoVersionFromHash(repo string, base *semver.Version, hash string) (string, error) {
url := "https://api.github.com/repos/" + repo + "/commits/" + hash
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand Down
Loading
Loading