github.com/tilotech/tilores-cli@v0.28.0/cmd/upgrade.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/cobra" 7 "github.com/tilotech/tilores-cli/internal/pkg" 8 "github.com/tilotech/tilores-cli/internal/pkg/step" 9 ) 10 11 // upgradeCmd represents the upgrade command 12 var upgradeCmd = &cobra.Command{ 13 Use: "upgrade", 14 Short: "Upgrades to the latest TiloRes version", 15 Long: "Upgrades to the latest TiloRes version.", 16 Run: func(_ *cobra.Command, _ []string) { 17 err := upgrade() 18 cobra.CheckErr(err) 19 }, 20 } 21 22 func init() { 23 rootCmd.AddCommand(upgradeCmd) 24 } 25 26 func upgrade() error { 27 finalModulePath, err := pkg.GetModulePath() 28 if err != nil { 29 return err 30 } 31 variables := map[string]interface{}{ 32 "ApplicationName": applicationName, 33 "GeneratedMsg": generatedMsg, 34 "ModulePath": finalModulePath, 35 } 36 config, err := pkg.LoadConfig() 37 if err != nil { 38 return err 39 } 40 upgrades, err := pkg.ListUpgrades(config.Version) 41 if err != nil { 42 return err 43 } 44 if len(upgrades) == 0 { 45 fmt.Println("Upgrade not required. Already at the latest version.") 46 return nil 47 } 48 for _, upgradeVersion := range upgrades { 49 steps, err := pkg.CreateUpgradeSteps(upgradeVersion, variables) 50 if err != nil { 51 return fmt.Errorf("error while preparing the upgrade for %v: %v", upgradeVersion, err) 52 } 53 steps = append( 54 steps, 55 step.ModTidy, 56 step.ModVendor, 57 step.Generate, 58 step.BuildVerify, 59 ) 60 err = step.Execute(steps) 61 if err != nil { 62 return fmt.Errorf("error while running the upgrade for %v: %v", upgradeVersion, err) 63 } 64 config.Version = upgradeVersion 65 err = pkg.SaveConfig(config) 66 if err != nil { 67 return err 68 } 69 } 70 fmt.Println("Successfully upgraded to the latest version.") 71 return nil 72 }