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
5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Repository Guidelines

- Use the Makefile commands for common tasks (e.g., `make lint`, `make test`)
- Always run `make lint`
- Always run `make test`
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
TOOLCHAIN ?= go1.25.0

## help: 💡 Display available commands
.PHONY: help
help:
Expand Down Expand Up @@ -37,7 +39,7 @@ markdown:
## lint: 🚨 Run lint checks
.PHONY: lint
lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./...
GOTOOLCHAIN=$(TOOLCHAIN) go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.0 run ./...

## test: 🚦 Execute all tests
.PHONY: test
Expand Down
4 changes: 2 additions & 2 deletions cmd/internal/migrations/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var Migrations = []Migration{

// DoMigration runs all migrations
// It will run all migrations that match the current and target version
func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, skipGoMod bool) error {
func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, skipGoMod, verbose bool) error {
for _, m := range Migrations {
toC, err := semver.NewConstraint(m.To)
if err != nil {
Expand All @@ -90,7 +90,7 @@ func DoMigration(cmd *cobra.Command, cwd string, curr, target *semver.Version, s
return err
}
}
} else {
} else if verbose {
cmd.Printf("Skipping migration from %s to %s\n", m.From, m.To)
}
}
Expand Down
42 changes: 42 additions & 0 deletions cmd/internal/migrations/lists_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package migrations_test

import (
"bytes"
"testing"

semver "github.com/Masterminds/semver/v3"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func Test_DoMigration_Verbose(t *testing.T) {
t.Parallel()
dir := t.TempDir()
curr := semver.MustParse("0.0.0")
target := semver.MustParse("0.1.0")

t.Run("silent", func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOut(&buf)
require.NoError(t, migrations.DoMigration(cmd, dir, curr, target, true, false))
assert.Equal(t, "", buf.String())
})

t.Run("verbose", func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
cmd := &cobra.Command{}
cmd.SetOut(&buf)
require.NoError(t, migrations.DoMigration(cmd, dir, curr, target, true, true))
out := buf.String()
assert.Contains(t, out, "Skipping migration from >=1.0.0 to >=0.0.0-0")
assert.Contains(t, out, "Skipping migration from >=2.0.0 to <4.0.0-0")
})
}
22 changes: 12 additions & 10 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,25 @@ func newMigrateCmd() *cobra.Command {
var targetVersionS string
var force bool
var skipGoMod bool
var verbose bool

cmd := &cobra.Command{
Use: "migrate",
Short: "Migrate Fiber project version to a newer version",
}

latestFiberVersion, err := LatestFiberVersion()
if err != nil {
latestFiberVersion = ""
}

cmd.Flags().StringVarP(&targetVersionS, "to", "t", "", "Migrate to a specific version e.g:"+latestFiberVersion+" Format: X.Y.Z")
if err := cmd.MarkFlagRequired("to"); err != nil {
panic(err)
}
cmd.Flags().StringVarP(&targetVersionS, "to", "t", "", "Migrate to a specific version. Default: latest")
cmd.Flags().BoolVarP(&force, "force", "f", false, "Force migration even if already on version")
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.RunE = func(cmd *cobra.Command, _ []string) error {
return migrateRunE(cmd, MigrateOptions{
CurrentVersionFile: currentVersionFile,
TargetVersionS: targetVersionS,
Force: force,
SkipGoMod: skipGoMod,
Verbose: verbose,
})
}

Expand All @@ -53,6 +48,7 @@ type MigrateOptions struct {
TargetVersionS string
Force bool
SkipGoMod bool
Verbose bool
}

func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
Expand All @@ -63,6 +59,12 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
currentVersionS = strings.TrimPrefix(currentVersionS, "v")
currentVersion := semver.MustParse(currentVersionS)

if opts.TargetVersionS == "" {
opts.TargetVersionS, err = LatestFiberVersion()
if err != nil {
return fmt.Errorf("failed to determine latest fiber version: %w", err)
}
}
opts.TargetVersionS = strings.TrimPrefix(opts.TargetVersionS, "v")
targetVersion, err := semver.NewVersion(opts.TargetVersionS)
if err != nil {
Expand All @@ -89,7 +91,7 @@ func migrateRunE(cmd *cobra.Command, opts MigrateOptions) error {
migrateFromS = migrateFrom.String()
}

err = migrations.DoMigration(cmd, wd, migrateFrom, targetVersion, opts.SkipGoMod)
err = migrations.DoMigration(cmd, wd, migrateFrom, targetVersion, opts.SkipGoMod, opts.Verbose)
if err != nil {
return fmt.Errorf("migration failed %w", err)
}
Expand Down
43 changes: 43 additions & 0 deletions cmd/migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd

import (
"net/http"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -111,6 +113,47 @@ func main() {
assert.Equal(t, content, content2)
}

func Test_Migrate_DefaultTarget(t *testing.T) {
dir, err := os.MkdirTemp("", "migrate_default")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

gomod := `module example.com/demo

go 1.20

require github.com/gofiber/fiber/v2 v2.0.6
`
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(gomod), 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)) }()

setupCmd()
defer teardownCmd()

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(http.MethodGet, "https://api.github.com/repos/gofiber/fiber/releases/latest", httpmock.NewBytesResponder(200, []byte(`{"name":"v3.0.0"}`)))

cmd := newMigrateCmd()
out, err := runCobraCmd(cmd)
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
6 changes: 5 additions & 1 deletion cmd/tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ func runCobraCmd(cmd *cobra.Command, args ...string) (string, error) {
parent.SilenceErrors = origSilence
}()
} else {
cmd.SetArgs(args)
if len(args) == 0 {
cmd.SetArgs([]string{})
} else {
cmd.SetArgs(args)
}
}

err := cmd.Execute()
Expand Down
Loading