-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Masterminds/semver/v3" | ||
Check failure on line 5 in cmd/internal/migrations/common.go GitHub Actions / golangci[golangci] cmd/internal/migrations/common.go#L5
Raw output
|
||
"github.com/spf13/cobra" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gofiber/cli/cmd/internal" | ||
) | ||
|
||
var ( | ||
pkgRegex = regexp.MustCompile(`(github\.com\/gofiber\/fiber\/)(v\d+)( *?)(v[\w.-]+)`) | ||
) | ||
|
||
func MigrateGoPkgs(cmd *cobra.Command, cwd string, curr *semver.Version, target *semver.Version) error { | ||
Check failure on line 19 in cmd/internal/migrations/common.go GitHub Actions / golangci[golangci] cmd/internal/migrations/common.go#L19
Raw output
|
||
pkgReplacer := strings.NewReplacer( | ||
"github.com/gofiber/fiber/v"+strconv.FormatUint(curr.Major(), 10), | ||
"github.com/gofiber/fiber/v"+strconv.FormatUint(target.Major(), 10), | ||
) | ||
|
||
err := internal.ChangeFileContent(cwd, func(content string) string { | ||
return pkgReplacer.Replace(content) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to migrate Go packages: %v", err) | ||
} | ||
|
||
// get go.mod file | ||
modFile := "go.mod" | ||
fileContent, err := os.ReadFile(modFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// replace old version with new version in go.mod file | ||
fileContentStr := pkgRegex.ReplaceAllString( | ||
string(fileContent), | ||
"${1}v"+strconv.FormatUint(target.Major(), 10)+"${3}v"+target.String(), | ||
) | ||
|
||
// update go.mod file | ||
if err := os.WriteFile(modFile, []byte(fileContentStr), 0644); err != nil { | ||
return err | ||
} | ||
|
||
cmd.Println("Migrating Go packages") | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Masterminds/semver/v3" | ||
Check failure on line 5 in cmd/internal/migrations/lists.go GitHub Actions / golangci[golangci] cmd/internal/migrations/lists.go#L5
Raw output
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/gofiber/cli/cmd/internal/migrations/v3" | ||
Check failure on line 8 in cmd/internal/migrations/lists.go GitHub Actions / golangci[golangci] cmd/internal/migrations/lists.go#L8
Raw output
|
||
) | ||
|
||
// MigrationFn is a function that will be called during migration | ||
type MigrationFn func(cmd *cobra.Command, cwd string, curr *semver.Version, target *semver.Version) error | ||
Check failure on line 12 in cmd/internal/migrations/lists.go GitHub Actions / golangci[golangci] cmd/internal/migrations/lists.go#L12
Raw output
|
||
|
||
// Migration is a single migration | ||
type Migration struct { | ||
From string | ||
To string | ||
Functions []MigrationFn | ||
} | ||
|
||
// Migrations is a list of all migrations | ||
// Example structure: | ||
// {"from": ">=2.0.0", "to": "<=3.*.*", "fn": [MigrateFN, MigrateFN]} | ||
var Migrations = []Migration{ | ||
{From: ">=2.0.0", To: "<4.0.0-0", Functions: []MigrationFn{v3.MigrateHandlerSignatures}}, | ||
{From: ">=1.0.0", To: ">=0.0.0-0", Functions: []MigrationFn{MigrateGoPkgs}}, | ||
} | ||
|
||
// DoMigration runs all migrations | ||
// It will run all migrations that match the current and target version | ||
func DoMigration(cmd *cobra.Command, cwd string, curr *semver.Version, target *semver.Version) error { | ||
for _, m := range Migrations { | ||
toC, err := semver.NewConstraint(m.To) | ||
if err != nil { | ||
return err | ||
} | ||
fromC, err := semver.NewConstraint(m.From) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if fromC.Check(curr) && toC.Check(target) { | ||
for _, fn := range m.Functions { | ||
if err := fn(cmd, cwd, curr, target); err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
fmt.Printf("Skipping migration from %s to %s\n", m.From, m.To) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package v3 | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Masterminds/semver/v3" | ||
Check failure on line 5 in cmd/internal/migrations/v3/common.go GitHub Actions / golangci[golangci] cmd/internal/migrations/v3/common.go#L5
Raw output
|
||
"github.com/spf13/cobra" | ||
"strings" | ||
|
||
"github.com/gofiber/cli/cmd/internal" | ||
) | ||
|
||
func MigrateHandlerSignatures(cmd *cobra.Command, cwd string, _ *semver.Version, _ *semver.Version) error { | ||
Check failure on line 12 in cmd/internal/migrations/v3/common.go GitHub Actions / golangci[golangci] cmd/internal/migrations/v3/common.go#L12
Raw output
|
||
sigReplacer := strings.NewReplacer("*fiber.Ctx", "fiber.Ctx") | ||
|
||
err := internal.ChangeFileContent(cwd, func(content string) string { | ||
return sigReplacer.Replace(content) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to migrate handler signatures: %v", err) | ||
} | ||
|
||
cmd.Println("Migrating handler signatures") | ||
|
||
return nil | ||
} |