|
| 1 | +/* |
| 2 | +Copyright 2021 The KodeRover Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + "github.com/spf13/viper" |
| 24 | + |
| 25 | + "github.com/koderover/zadig/v2/pkg/shared/client/plutusenterprise" |
| 26 | + e "github.com/koderover/zadig/v2/pkg/tool/errors" |
| 27 | + "github.com/koderover/zadig/v2/pkg/tool/log" |
| 28 | +) |
| 29 | + |
| 30 | +func init() { |
| 31 | + rootCmd.AddCommand(checkUpgradeCmd) |
| 32 | + |
| 33 | + checkUpgradeCmd.PersistentFlags().StringP("from-version", "f", oldestVersion, "current version to migrate from") |
| 34 | + checkUpgradeCmd.PersistentFlags().StringP("to-version", "t", "", "target version to migrate to") |
| 35 | + _ = viper.BindPFlag("fromVersion", checkUpgradeCmd.PersistentFlags().Lookup("from-version")) |
| 36 | + _ = viper.BindPFlag("toVersion", checkUpgradeCmd.PersistentFlags().Lookup("to-version")) |
| 37 | +} |
| 38 | + |
| 39 | +var checkUpgradeCmd = &cobra.Command{ |
| 40 | + Use: "check", |
| 41 | + Short: "check if the upgrade is possible", |
| 42 | + Long: `check if the upgrade is possible.`, |
| 43 | + PreRunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + return preRun() |
| 45 | + }, |
| 46 | + Run: func(cmd *cobra.Command, args []string) { |
| 47 | + if err := runCheckUpgrade(); err != nil { |
| 48 | + log.Fatal(err) |
| 49 | + } |
| 50 | + }, |
| 51 | + PostRun: func(cmd *cobra.Command, args []string) { |
| 52 | + if err := postRun(); err != nil { |
| 53 | + fmt.Println(err) |
| 54 | + } |
| 55 | + }, |
| 56 | +} |
| 57 | + |
| 58 | +func runCheckUpgrade() error { |
| 59 | + from := viper.GetString("fromVersion") |
| 60 | + to := viper.GetString("toVersion") |
| 61 | + |
| 62 | + if len(from) == 0 { |
| 63 | + from = oldestVersion |
| 64 | + } |
| 65 | + if len(to) == 0 { |
| 66 | + return fmt.Errorf("target version not assigned") |
| 67 | + } |
| 68 | + |
| 69 | + log.Infof("Checking upgrade from %s to %s", from, to) |
| 70 | + |
| 71 | + resp, err := plutusenterprise.New().CheckUpgrade(from, to) |
| 72 | + if err != nil { |
| 73 | + nerr := e.ErrUpgradeNotAllowed.AddErr(err) |
| 74 | + log.Error(nerr) |
| 75 | + return nerr |
| 76 | + } |
| 77 | + if !resp.AllowUpgrade { |
| 78 | + nerr := e.ErrUpgradeNotAllowed |
| 79 | + log.Error(nerr) |
| 80 | + return nerr |
| 81 | + } |
| 82 | + |
| 83 | + log.Info("Upgrade check passed") |
| 84 | + return nil |
| 85 | +} |
0 commit comments