Skip to content

Commit 0f463cd

Browse files
authored
Implemented cpackget update, corrected list --updates (#345)
1 parent 73bb159 commit 0f463cd

File tree

8 files changed

+476
-94
lines changed

8 files changed

+476
-94
lines changed

cmd/commands/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var AllCommands = []*cobra.Command{
2525
RmCmd,
2626
ListCmd,
2727
UpdateIndexCmd,
28+
UpdateCmd,
2829
ChecksumCreateCmd,
2930
ChecksumVerifyCmd,
3031
SignatureCreateCmd,

cmd/commands/update.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

cmd/commands/update_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* SPDX-License-Identifier: Apache-2.0 */
2+
/* Copyright Contributors to the cpackget project. */
3+
4+
package commands_test
5+
6+
import (
7+
"testing"
8+
)
9+
10+
var (
11+
// packFilePath1 = filepath.Join(testingDir, "TheVendor.PublicLocalPack.pack")
12+
// fileWithPacksListed1 = "file_with_listed_packs.txt"
13+
// fileWithNoPacksListed1 = "file_with_no_listed_packs.txt"
14+
)
15+
16+
var updateCmdTests = []TestCase{
17+
{
18+
name: "test help command",
19+
args: []string{"help", "update"},
20+
expectedErr: nil,
21+
},
22+
/*{
23+
name: "test updating pack file no args",
24+
args: []string{"update"},
25+
createPackRoot: true,
26+
expectedStdout: []string{"Missing a pack-path or list with pack urls specified via -f/--packs-list-filename"},
27+
expectedErr: errs.ErrIncorrectCmdArgs,
28+
},*/
29+
/*{
30+
name: "test updating pack file default mode",
31+
args: []string{"update", packFilePath1},
32+
createPackRoot: true,
33+
defaultMode: true,
34+
expectedStdout: []string{"updating pack", filepath.Base(packFilePath1)},
35+
},*/
36+
/*{
37+
name: "test updating pack file default mode no preexisting index",
38+
args: []string{"update", packFilePath1},
39+
createPackRoot: false,
40+
defaultMode: true,
41+
expectedStdout: []string{"updating pack", filepath.Base(packFilePath1)},
42+
},*/
43+
{
44+
name: "test updating pack missing file",
45+
args: []string{"update", "DoesNotExist.Pack"},
46+
createPackRoot: true,
47+
// expectedStdout: []string{"cannot be determined"},
48+
// expectedErr: errs.ErrPackURLCannotBeFound,
49+
},
50+
/* {
51+
name: "test updating pack file",
52+
args: []string{"update", packFilePath},
53+
createPackRoot: true,
54+
expectedStdout: []string{"updating pack", filepath.Base(packFilePath)},
55+
},
56+
{
57+
name: "test updating packs listed in file",
58+
args: []string{"update", "-f", fileWithPacksListed},
59+
createPackRoot: true,
60+
expectedStdout: []string{"Parsing packs urls via file " + fileWithPacksListed,
61+
"Updating pack", filepath.Base(packFilePath)},
62+
setUpFunc: func(t *TestCase) {
63+
f, _ := os.Create(fileWithPacksListed)
64+
_, _ = f.WriteString(packFilePath)
65+
f.Close()
66+
},
67+
tearDownFunc: func() {
68+
os.Remove(fileWithPacksListed)
69+
},
70+
},
71+
{
72+
name: "test updating empty packs list file",
73+
args: []string{"update", "-f", fileWithNoPacksListed},
74+
createPackRoot: true,
75+
expectedStdout: []string{"Parsing packs urls via file " + fileWithNoPacksListed},
76+
expectedErr: nil,
77+
setUpFunc: func(t *TestCase) {
78+
f, _ := os.Create(fileWithNoPacksListed)
79+
_, _ = f.WriteString("")
80+
f.Close()
81+
},
82+
tearDownFunc: func() {
83+
os.Remove(fileWithNoPacksListed)
84+
},
85+
},
86+
{
87+
name: "test updating empty packs list file (but whitespace characters)",
88+
args: []string{"update", "-f", fileWithNoPacksListed},
89+
createPackRoot: true,
90+
expectedStdout: []string{"Parsing packs urls via file " + fileWithNoPacksListed},
91+
expectedErr: nil,
92+
setUpFunc: func(t *TestCase) {
93+
f, _ := os.Create(fileWithNoPacksListed)
94+
_, _ = f.WriteString(" \n \t \n")
95+
f.Close()
96+
},
97+
tearDownFunc: func() {
98+
os.Remove(fileWithNoPacksListed)
99+
},
100+
},*/
101+
}
102+
103+
func TestUpdateCmd(t *testing.T) {
104+
runTests(t, updateCmdTests)
105+
}

cmd/installer/pack.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type PackType struct {
7575

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

105+
if forceLatest {
106+
info.Version = "latest"
107+
info.VersionModifier = utils.LatestVersion
108+
}
105109
pack.URL = info.Location
106110
pack.Name = info.Pack
107111
pack.Vendor = info.Vendor
@@ -117,7 +121,7 @@ func preparePack(packPath string, toBeRemoved bool, timeout int) (*PackType, err
117121
return pack, err
118122
}
119123

120-
pack.isInstalled = Installation.PackIsInstalled(pack)
124+
pack.isInstalled = Installation.PackIsInstalled(pack, noLocal)
121125

122126
return pack, nil
123127
}
@@ -586,12 +590,12 @@ func (p *PackType) loadDependencies() error {
586590
var pack *PackType
587591
var err error
588592
if version == "" {
589-
pack, err = preparePack(deps[i][1]+"."+deps[i][0], false, 0)
593+
pack, err = preparePack(deps[i][1]+"."+deps[i][0], false, false, false, 0)
590594
if err != nil {
591595
return err
592596
}
593597
} else {
594-
pack, err = preparePack(deps[i][1]+"."+deps[i][0]+"."+deps[i][2], false, 0)
598+
pack, err = preparePack(deps[i][1]+"."+deps[i][0]+"."+deps[i][2], false, false, false, 0)
595599
if err != nil {
596600
return err
597601
}
@@ -611,7 +615,7 @@ func (p *PackType) loadDependencies() error {
611615
} else {
612616
pack.versionModifier = utils.LatestVersion
613617
}
614-
if Installation.PackIsInstalled(pack) {
618+
if Installation.PackIsInstalled(pack, false) {
615619
p.Requirements.packages = append(p.Requirements.packages, struct {
616620
info []string
617621
installed bool

0 commit comments

Comments
 (0)