|
| 1 | +/* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | +/* Copyright Contributors to the cpackget project. */ |
| 3 | + |
| 4 | +package commands |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + errs "github.com/open-cmsis-pack/cpackget/cmd/errors" |
| 12 | + "github.com/open-cmsis-pack/cpackget/cmd/installer" |
| 13 | + "github.com/open-cmsis-pack/cpackget/cmd/utils" |
| 14 | + log "github.com/sirupsen/logrus" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +var updateCmdFlags struct { |
| 19 | + // noRequirements skips installing package requirements |
| 20 | + noRequirements bool |
| 21 | + |
| 22 | + // packsListFileName is the file name where a list of pack urls is present |
| 23 | + packsListFileName string |
| 24 | + |
| 25 | + // skipEula tells whether pack's license should be presented to the user or not for a yay-or-nay acceptance |
| 26 | + skipEula bool |
| 27 | + |
| 28 | + // skipTouch does not touch pack.idx after update |
| 29 | + skipTouch bool |
| 30 | + |
| 31 | + // Reports encoded progress for files and download when used by other tools |
| 32 | + encodedProgress bool |
| 33 | +} |
| 34 | + |
| 35 | +var UpdateCmd = &cobra.Command{ |
| 36 | + Use: "update [<pack> | -f <packs list>]", |
| 37 | + Short: "Update Open-CMSIS-Pack packages to latest", |
| 38 | + Long: ` |
| 39 | +Update a pack using the following "<pack>" specification or using packs provided by "-f <packs list>": |
| 40 | +
|
| 41 | + $ cpackget update Vendor.Pack |
| 42 | +
|
| 43 | + The pack will be updated to the latest version |
| 44 | +
|
| 45 | + $ cpackget update |
| 46 | +
|
| 47 | + Use this to update all installed packs to the latest version |
| 48 | +
|
| 49 | + The pack can be local file or hosted somewhere else on the Internet. |
| 50 | + If it's hosted somewhere, cpackget will first download it then extract all pack files into "CMSIS_PACK_ROOT/<vendor>/<packName>/<x.y.z>/" |
| 51 | + If "-f" is used, cpackget will call "cpackget update pack" on each URL specified in the <packs list> file.`, |
| 52 | + Args: cobra.MinimumNArgs(0), |
| 53 | + PersistentPreRunE: configureInstaller, |
| 54 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | + |
| 56 | + utils.SetEncodedProgress(updateCmdFlags.encodedProgress) |
| 57 | + utils.SetSkipTouch(updateCmdFlags.skipTouch) |
| 58 | + |
| 59 | + if updateCmdFlags.packsListFileName != "" { |
| 60 | + log.Infof("Parsing packs urls via file %v", updateCmdFlags.packsListFileName) |
| 61 | + |
| 62 | + file, err := os.Open(updateCmdFlags.packsListFileName) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + defer file.Close() |
| 67 | + |
| 68 | + scanner := bufio.NewScanner(file) |
| 69 | + for scanner.Scan() { |
| 70 | + tmpEntry := strings.TrimSpace(scanner.Text()) |
| 71 | + if len(tmpEntry) == 0 { |
| 72 | + continue |
| 73 | + } |
| 74 | + args = append(args, tmpEntry) |
| 75 | + } |
| 76 | + |
| 77 | + if err := scanner.Err(); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + var lastErr error |
| 83 | + |
| 84 | + if len(args) == 0 { |
| 85 | + if updateCmdFlags.packsListFileName != "" { |
| 86 | + return nil // nothing to do |
| 87 | + } |
| 88 | + installer.UnlockPackRoot() |
| 89 | + err := installer.UpdatePack("", !updateCmdFlags.skipEula, updateCmdFlags.noRequirements, viper.GetInt("timeout")) |
| 90 | + if err != nil { |
| 91 | + lastErr = err |
| 92 | + if !errs.AlreadyLogged(err) { |
| 93 | + log.Error(err) |
| 94 | + } |
| 95 | + } |
| 96 | + installer.LockPackRoot() |
| 97 | + return lastErr |
| 98 | + } |
| 99 | + |
| 100 | + log.Debugf("Specified packs %v", args) |
| 101 | + installer.UnlockPackRoot() |
| 102 | + for _, packPath := range args { |
| 103 | + err := installer.UpdatePack(packPath, !updateCmdFlags.skipEula, updateCmdFlags.noRequirements, viper.GetInt("timeout")) |
| 104 | + if err != nil { |
| 105 | + lastErr = err |
| 106 | + if !errs.AlreadyLogged(err) { |
| 107 | + log.Error(err) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + installer.LockPackRoot() |
| 112 | + return lastErr |
| 113 | + }, |
| 114 | +} |
| 115 | + |
| 116 | +func init() { |
| 117 | + UpdateCmd.Flags().BoolVarP(&updateCmdFlags.skipEula, "agree-embedded-license", "a", false, "agrees with the embedded license of the pack") |
| 118 | + UpdateCmd.Flags().BoolVarP(&updateCmdFlags.noRequirements, "no-dependencies", "n", false, "do not install package dependencies") |
| 119 | + UpdateCmd.Flags().StringVarP(&updateCmdFlags.packsListFileName, "packs-list-filename", "f", "", "specifies a file listing packs urls, one per line") |
| 120 | + UpdateCmd.Flags().BoolVar(&updateCmdFlags.skipTouch, "skip-touch", false, "do not touch pack.idx") |
| 121 | + UpdateCmd.Flags().BoolVarP(&updateCmdFlags.encodedProgress, "encoded-progress", "E", false, "Reports encoded progress for files and download when used by other tools") |
| 122 | + |
| 123 | + UpdateCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { |
| 124 | + // Small workaround to keep the linter happy, not |
| 125 | + // really necessary to test this |
| 126 | + err := command.Flags().MarkHidden("concurrent-downloads") |
| 127 | + log.Debug(err) |
| 128 | + command.Parent().HelpFunc()(command, strings) |
| 129 | + }) |
| 130 | +} |
0 commit comments