Skip to content

Commit

Permalink
Implemented cpackget update, corrected list --updates (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgn42 authored Sep 13, 2024
1 parent 73bb159 commit 0f463cd
Show file tree
Hide file tree
Showing 8 changed files with 476 additions and 94 deletions.
1 change: 1 addition & 0 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var AllCommands = []*cobra.Command{
RmCmd,
ListCmd,
UpdateIndexCmd,
UpdateCmd,
ChecksumCreateCmd,
ChecksumVerifyCmd,
SignatureCreateCmd,
Expand Down
130 changes: 130 additions & 0 deletions cmd/commands/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */

package commands

import (
"bufio"
"os"
"strings"

errs "github.com/open-cmsis-pack/cpackget/cmd/errors"
"github.com/open-cmsis-pack/cpackget/cmd/installer"
"github.com/open-cmsis-pack/cpackget/cmd/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var updateCmdFlags struct {
// noRequirements skips installing package requirements
noRequirements bool

// packsListFileName is the file name where a list of pack urls is present
packsListFileName string

// skipEula tells whether pack's license should be presented to the user or not for a yay-or-nay acceptance
skipEula bool

// skipTouch does not touch pack.idx after update
skipTouch bool

// Reports encoded progress for files and download when used by other tools
encodedProgress bool
}

var UpdateCmd = &cobra.Command{
Use: "update [<pack> | -f <packs list>]",
Short: "Update Open-CMSIS-Pack packages to latest",
Long: `
Update a pack using the following "<pack>" specification or using packs provided by "-f <packs list>":
$ cpackget update Vendor.Pack
The pack will be updated to the latest version
$ cpackget update
Use this to update all installed packs to the latest version
The pack can be local file or hosted somewhere else on the Internet.
If it's hosted somewhere, cpackget will first download it then extract all pack files into "CMSIS_PACK_ROOT/<vendor>/<packName>/<x.y.z>/"
If "-f" is used, cpackget will call "cpackget update pack" on each URL specified in the <packs list> file.`,
Args: cobra.MinimumNArgs(0),
PersistentPreRunE: configureInstaller,
RunE: func(cmd *cobra.Command, args []string) error {

utils.SetEncodedProgress(updateCmdFlags.encodedProgress)
utils.SetSkipTouch(updateCmdFlags.skipTouch)

if updateCmdFlags.packsListFileName != "" {
log.Infof("Parsing packs urls via file %v", updateCmdFlags.packsListFileName)

file, err := os.Open(updateCmdFlags.packsListFileName)
if err != nil {
return err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
tmpEntry := strings.TrimSpace(scanner.Text())
if len(tmpEntry) == 0 {
continue
}
args = append(args, tmpEntry)
}

if err := scanner.Err(); err != nil {
return err
}
}

var lastErr error

if len(args) == 0 {
if updateCmdFlags.packsListFileName != "" {
return nil // nothing to do
}
installer.UnlockPackRoot()
err := installer.UpdatePack("", !updateCmdFlags.skipEula, updateCmdFlags.noRequirements, viper.GetInt("timeout"))
if err != nil {
lastErr = err
if !errs.AlreadyLogged(err) {
log.Error(err)
}
}
installer.LockPackRoot()
return lastErr
}

log.Debugf("Specified packs %v", args)
installer.UnlockPackRoot()
for _, packPath := range args {
err := installer.UpdatePack(packPath, !updateCmdFlags.skipEula, updateCmdFlags.noRequirements, viper.GetInt("timeout"))
if err != nil {
lastErr = err
if !errs.AlreadyLogged(err) {
log.Error(err)
}
}
}
installer.LockPackRoot()
return lastErr
},
}

func init() {
UpdateCmd.Flags().BoolVarP(&updateCmdFlags.skipEula, "agree-embedded-license", "a", false, "agrees with the embedded license of the pack")
UpdateCmd.Flags().BoolVarP(&updateCmdFlags.noRequirements, "no-dependencies", "n", false, "do not install package dependencies")
UpdateCmd.Flags().StringVarP(&updateCmdFlags.packsListFileName, "packs-list-filename", "f", "", "specifies a file listing packs urls, one per line")
UpdateCmd.Flags().BoolVar(&updateCmdFlags.skipTouch, "skip-touch", false, "do not touch pack.idx")
UpdateCmd.Flags().BoolVarP(&updateCmdFlags.encodedProgress, "encoded-progress", "E", false, "Reports encoded progress for files and download when used by other tools")

UpdateCmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
// Small workaround to keep the linter happy, not
// really necessary to test this
err := command.Flags().MarkHidden("concurrent-downloads")
log.Debug(err)
command.Parent().HelpFunc()(command, strings)
})
}
105 changes: 105 additions & 0 deletions cmd/commands/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */

package commands_test

import (
"testing"
)

var (
// packFilePath1 = filepath.Join(testingDir, "TheVendor.PublicLocalPack.pack")
// fileWithPacksListed1 = "file_with_listed_packs.txt"
// fileWithNoPacksListed1 = "file_with_no_listed_packs.txt"
)

var updateCmdTests = []TestCase{
{
name: "test help command",
args: []string{"help", "update"},
expectedErr: nil,
},
/*{
name: "test updating pack file no args",
args: []string{"update"},
createPackRoot: true,
expectedStdout: []string{"Missing a pack-path or list with pack urls specified via -f/--packs-list-filename"},
expectedErr: errs.ErrIncorrectCmdArgs,
},*/
/*{
name: "test updating pack file default mode",
args: []string{"update", packFilePath1},
createPackRoot: true,
defaultMode: true,
expectedStdout: []string{"updating pack", filepath.Base(packFilePath1)},
},*/
/*{
name: "test updating pack file default mode no preexisting index",
args: []string{"update", packFilePath1},
createPackRoot: false,
defaultMode: true,
expectedStdout: []string{"updating pack", filepath.Base(packFilePath1)},
},*/
{
name: "test updating pack missing file",
args: []string{"update", "DoesNotExist.Pack"},
createPackRoot: true,
// expectedStdout: []string{"cannot be determined"},
// expectedErr: errs.ErrPackURLCannotBeFound,
},
/* {
name: "test updating pack file",
args: []string{"update", packFilePath},
createPackRoot: true,
expectedStdout: []string{"updating pack", filepath.Base(packFilePath)},
},
{
name: "test updating packs listed in file",
args: []string{"update", "-f", fileWithPacksListed},
createPackRoot: true,
expectedStdout: []string{"Parsing packs urls via file " + fileWithPacksListed,
"Updating pack", filepath.Base(packFilePath)},
setUpFunc: func(t *TestCase) {
f, _ := os.Create(fileWithPacksListed)
_, _ = f.WriteString(packFilePath)
f.Close()
},
tearDownFunc: func() {
os.Remove(fileWithPacksListed)
},
},
{
name: "test updating empty packs list file",
args: []string{"update", "-f", fileWithNoPacksListed},
createPackRoot: true,
expectedStdout: []string{"Parsing packs urls via file " + fileWithNoPacksListed},
expectedErr: nil,
setUpFunc: func(t *TestCase) {
f, _ := os.Create(fileWithNoPacksListed)
_, _ = f.WriteString("")
f.Close()
},
tearDownFunc: func() {
os.Remove(fileWithNoPacksListed)
},
},
{
name: "test updating empty packs list file (but whitespace characters)",
args: []string{"update", "-f", fileWithNoPacksListed},
createPackRoot: true,
expectedStdout: []string{"Parsing packs urls via file " + fileWithNoPacksListed},
expectedErr: nil,
setUpFunc: func(t *TestCase) {
f, _ := os.Create(fileWithNoPacksListed)
_, _ = f.WriteString(" \n \t \n")
f.Close()
},
tearDownFunc: func() {
os.Remove(fileWithNoPacksListed)
},
},*/
}

func TestUpdateCmd(t *testing.T) {
runTests(t, updateCmdTests)
}
14 changes: 9 additions & 5 deletions cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type PackType struct {

// preparePack does some sanity validation regarding pack name
// and check if it's public and if it's installed or not
func preparePack(packPath string, toBeRemoved bool, timeout int) (*PackType, error) {
func preparePack(packPath string, toBeRemoved, forceLatest, noLocal bool, timeout int) (*PackType, error) {
pack := &PackType{
path: packPath,
toBeRemoved: toBeRemoved,
Expand All @@ -102,6 +102,10 @@ func preparePack(packPath string, toBeRemoved bool, timeout int) (*PackType, err
return pack, err
}

if forceLatest {
info.Version = "latest"
info.VersionModifier = utils.LatestVersion
}
pack.URL = info.Location
pack.Name = info.Pack
pack.Vendor = info.Vendor
Expand All @@ -117,7 +121,7 @@ func preparePack(packPath string, toBeRemoved bool, timeout int) (*PackType, err
return pack, err
}

pack.isInstalled = Installation.PackIsInstalled(pack)
pack.isInstalled = Installation.PackIsInstalled(pack, noLocal)

return pack, nil
}
Expand Down Expand Up @@ -586,12 +590,12 @@ func (p *PackType) loadDependencies() error {
var pack *PackType
var err error
if version == "" {
pack, err = preparePack(deps[i][1]+"."+deps[i][0], false, 0)
pack, err = preparePack(deps[i][1]+"."+deps[i][0], false, false, false, 0)
if err != nil {
return err
}
} else {
pack, err = preparePack(deps[i][1]+"."+deps[i][0]+"."+deps[i][2], false, 0)
pack, err = preparePack(deps[i][1]+"."+deps[i][0]+"."+deps[i][2], false, false, false, 0)
if err != nil {
return err
}
Expand All @@ -611,7 +615,7 @@ func (p *PackType) loadDependencies() error {
} else {
pack.versionModifier = utils.LatestVersion
}
if Installation.PackIsInstalled(pack) {
if Installation.PackIsInstalled(pack, false) {
p.Requirements.packages = append(p.Requirements.packages, struct {
info []string
installed bool
Expand Down
Loading

0 comments on commit 0f463cd

Please sign in to comment.