Skip to content

Commit

Permalink
Implemented automagically loading of index.pidx #355, #357, #358
Browse files Browse the repository at this point in the history
  • Loading branch information
bgn42 committed Dec 3, 2024
1 parent 818916a commit 5c47968
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 131 deletions.
9 changes: 7 additions & 2 deletions cmd/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ Add a pack using the following "<pack>" specification or using packs provided by
The file can be a local file or a file 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 pack add" on each URL specified in the <packs list> file.`,
Args: cobra.MinimumNArgs(0),
PersistentPreRunE: configureInstaller,
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {

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

createPackRoot = true
err := configureInstaller(cmd, args)
if err != nil {
return err
}

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

Expand Down
15 changes: 6 additions & 9 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ var AllCommands = []*cobra.Command{
// createPackRoot is a flag that determines if the pack root should be created or not
var createPackRoot bool

// defaultPublicIndex is the public index to use in "default mode"
const defaultPublicIndex = "https://www.keil.com/pack/index.pidx"

var viper *viperType.Viper

func configureInstallerGlobalCmd(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -70,37 +67,37 @@ func configureInstaller(cmd *cobra.Command, args []string) error {
}

targetPackRoot := viper.GetString("pack-root")
checkConnection := viper.GetBool("check-connection")
checkConnection := viper.GetBool("check-connection") // TODO: never set

if targetPackRoot == installer.GetDefaultCmsisPackRoot() {
// If using the default pack root path and the public index is not found,
// initialize it
if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) {
err := installer.SetPackRoot(targetPackRoot, true)
err := installer.SetPackRoot(targetPackRoot, true, true)
if err != nil {
return err
}
// Exclude index updating commands to not double update
if cmd.Name() != "init" && cmd.Name() != "index" && cmd.Name() != "update-index" {
installer.UnlockPackRoot()
err = installer.UpdatePublicIndex(defaultPublicIndex, true, true, false, false, 0, 0)
err = installer.UpdatePublicIndex(installer.DefaultPublicIndex, true, true, false, false, 0, 0)
if err != nil {
return err
}
err = installer.SetPackRoot(targetPackRoot, false)
err = installer.SetPackRoot(targetPackRoot, false, true)
if err != nil {
return err
}
installer.LockPackRoot()
}
} else {
err := installer.SetPackRoot(targetPackRoot, createPackRoot)
err := installer.SetPackRoot(targetPackRoot, createPackRoot, true)
if err != nil {
return err
}
}
} else {
err := installer.SetPackRoot(targetPackRoot, createPackRoot)
err := installer.SetPackRoot(targetPackRoot, createPackRoot, true)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func runTests(t *testing.T, tests []TestCase) {

os.Setenv("CMSIS_PACK_ROOT", localTestingDir)
if test.createPackRoot {
assert.Nil(installer.SetPackRoot(localTestingDir, test.createPackRoot))
assert.Nil(installer.SetPackRoot(localTestingDir, test.createPackRoot, false))
installer.UnlockPackRoot()
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
ErrFailedInflatingFile = errors.New("fail to inflate file")
ErrFailedCreatingDirectory = errors.New("fail to create directory")
ErrFileNotFound = errors.New("file not found")
ErrFileNotFoundUseInit = errors.New("index.pdix file not found; use cpackget init command to retrieve it")
ErrDirectoryNotFound = errors.New("directory not found")
ErrPathAlreadyExists = errors.New("path already exists")
ErrCopyingEqualPaths = errors.New("failed copying files: source is the same as destination")
Expand Down Expand Up @@ -95,4 +96,7 @@ var (

// Error/Flag to detect when a user has requested early termination
ErrTerminatedByUser = errors.New("terminated by user request")

ErrIndexTooOld = errors.New("public index too old")
ErrOffline = errors.New("remote server is offline or cannot be reached")
)
39 changes: 36 additions & 3 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"golang.org/x/sync/semaphore"
)

// DefaultPublicIndex is the public index to use in "default mode"
const DefaultPublicIndex = "https://www.keil.com/pack/index.pidx"

const KeilDefaultPackRoot = "https://www.keil.com/pack/"

// GetDefaultCmsisPackRoot provides a default location
Expand Down Expand Up @@ -613,7 +616,7 @@ func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPd
} else {
if indexPath != "" {
if !utils.FileExists(indexPath) && !utils.DirExists(indexPath) {
return errs.ErrFileNotFound
return errs.ErrFileNotFoundUseInit
}
fileInfo, err := os.Stat(indexPath)
if err != nil {
Expand Down Expand Up @@ -1065,7 +1068,7 @@ var Installation *PacksInstallationType

// SetPackRoot sets the working directory of the packs installation
// if create == true, cpackget will try to create needed resources
func SetPackRoot(packRoot string, create bool) error {
func SetPackRoot(packRoot string, create, download bool) error {
if len(packRoot) == 0 {
return errs.ErrPackRootNotFound
}
Expand All @@ -1075,7 +1078,7 @@ func SetPackRoot(packRoot string, create bool) error {
return errs.ErrPackRootDoesNotExist
}

checkConnection := viper.GetBool("check-connection")
checkConnection := viper.GetBool("check-connection") // TODO: never set
if checkConnection && !utils.GetEncodedProgress() {
if packRoot == GetDefaultCmsisPackRoot() {
log.Infof("Using pack root: \"%v\" (default mode - no specific CMSIS_PACK_ROOT chosen)", packRoot)
Expand Down Expand Up @@ -1118,6 +1121,36 @@ func SetPackRoot(packRoot string, create bool) error {
// Make sure utils.DownloadFile always downloads files to .Download/
utils.CacheDir = Installation.DownloadDir

// If public index already exists then first check if online, then its timestamp
// if we are online and it is too old then download a current version
if download && utils.FileExists(Installation.PublicIndex) {
err := utils.CheckConnection(DefaultPublicIndex, 0)
if err != nil && err != errs.ErrOffline {
return err
}
if err != errs.ErrOffline {
err = Installation.PublicIndexXML.CheckTime()
if err != nil && err != errs.ErrIndexTooOld {
return err
}
if err == errs.ErrIndexTooOld {
UnlockPackRoot()
err := UpdatePublicIndex(DefaultPublicIndex, true, true, false, false, 0, 0)
if err != nil {
return err
}
}
}
}
// if public index does not or not yet exist then download without check
if download && !utils.FileExists(Installation.PublicIndex) {
UnlockPackRoot()
err := UpdatePublicIndex(DefaultPublicIndex, true, true, false, false, 0, 0)
if err != nil {
return err
}
}

err := Installation.PublicIndexXML.Read()
if err != nil {
return err
Expand Down
Loading

0 comments on commit 5c47968

Please sign in to comment.