@@ -12,8 +12,10 @@ import (
1212 "github.com/gofiber/cli/cmd/internal/migrations"
1313)
1414
15- func newMigrateCmd (currentVersionFile string ) * cobra.Command {
15+ func newMigrateCmd () * cobra.Command {
1616 var targetVersionS string
17+ var force bool
18+ var skipGoMod bool
1719
1820 cmd := & cobra.Command {
1921 Use : "migrate" ,
@@ -29,32 +31,46 @@ func newMigrateCmd(currentVersionFile string) *cobra.Command {
2931 if err := cmd .MarkFlagRequired ("to" ); err != nil {
3032 panic (err )
3133 }
34+ cmd .Flags ().BoolVarP (& force , "force" , "f" , false , "Force migration even if already on version" )
35+ cmd .Flags ().BoolVarP (& skipGoMod , "skip_go_mod" , "s" , false , "Skip running go mod tidy, download and vendor" )
3236
3337 cmd .RunE = func (cmd * cobra.Command , _ []string ) error {
34- return migrateRunE (cmd , currentVersionFile , targetVersionS )
38+ return migrateRunE (cmd , MigrateOptions {
39+ CurrentVersionFile : currentVersionFile ,
40+ TargetVersionS : targetVersionS ,
41+ Force : force ,
42+ SkipGoMod : skipGoMod ,
43+ })
3544 }
3645
3746 return cmd
3847}
3948
40- var migrateCmd = newMigrateCmd ("go.mod" )
49+ var migrateCmd = newMigrateCmd ()
4150
42- func migrateRunE (cmd * cobra.Command , currentVersionFile , targetVersionS string ) error {
43- currentVersionS , err := currentVersionFromFile (currentVersionFile )
51+ type MigrateOptions struct {
52+ CurrentVersionFile string
53+ TargetVersionS string
54+ Force bool
55+ SkipGoMod bool
56+ }
57+
58+ func migrateRunE (cmd * cobra.Command , opts MigrateOptions ) error {
59+ currentVersionS , err := currentVersionFromFile (opts .CurrentVersionFile )
4460 if err != nil {
4561 return fmt .Errorf ("current fiber project version not found: %w" , err )
4662 }
4763 currentVersionS = strings .TrimPrefix (currentVersionS , "v" )
4864 currentVersion := semver .MustParse (currentVersionS )
4965
50- targetVersionS = strings .TrimPrefix (targetVersionS , "v" )
51- targetVersion , err := semver .NewVersion (targetVersionS )
66+ opts . TargetVersionS = strings .TrimPrefix (opts . TargetVersionS , "v" )
67+ targetVersion , err := semver .NewVersion (opts . TargetVersionS )
5268 if err != nil {
53- return fmt .Errorf ("invalid version for \" %s\" : %w" , targetVersionS , err )
69+ return fmt .Errorf ("invalid version for \" %s\" : %w" , opts . TargetVersionS , err )
5470 }
5571
56- if ! targetVersion .GreaterThan (currentVersion ) {
57- return fmt .Errorf ("target version v%s is not greater than current version v%s" , targetVersionS , currentVersionS )
72+ if ! targetVersion .GreaterThan (currentVersion ) && ! ( opts . Force && targetVersion . Equal ( currentVersion )) {
73+ return fmt .Errorf ("target version v%s is not greater than current version v%s" , opts . TargetVersionS , currentVersionS )
5874 }
5975
6076 wd , err := os .Getwd ()
@@ -67,11 +83,13 @@ func migrateRunE(cmd *cobra.Command, currentVersionFile, targetVersionS string)
6783 return fmt .Errorf ("migration failed %w" , err )
6884 }
6985
70- if err := runGoMod (wd ); err != nil {
71- return fmt .Errorf ("go mod: %w" , err )
86+ if ! opts .SkipGoMod {
87+ if err := runGoMod (wd ); err != nil {
88+ return fmt .Errorf ("go mod: %w" , err )
89+ }
7290 }
7391
74- msg := fmt .Sprintf ("Migration from Fiber %s to %s" , currentVersionS , targetVersionS )
92+ msg := fmt .Sprintf ("Migration from Fiber %s to %s" , currentVersionS , opts . TargetVersionS )
7593 cmd .Println (termenv .String (msg ).
7694 Foreground (termenv .ANSIBrightBlue ))
7795
0 commit comments