Skip to content

Commit

Permalink
Onlinecheck and other things (#401)
Browse files Browse the repository at this point in the history
## Fixes
- Error message if a pack is not known
- Do not download index multiple times
- Better connection response

## Changes
Some Sources were changed-

## Checklist
<!-- Put an `x` in the boxes. All tasks must be completed and boxes
checked before merging. -->

- [x] 🤖 This change is covered by unit tests as required.
- [x] 🤹 All required manual testing has been performed.
- [x] 🛡️ Security impacts have been considered.
- [ ] 📖 All documentation updates are complete.
- [ ] 🧠 This change does not change third-party dependencies

---------

Co-authored-by: Joachim Krech <[email protected]>
  • Loading branch information
bgn42 and jkrech authored Dec 12, 2024
1 parent 97fca47 commit 2786cc5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
7 changes: 4 additions & 3 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func configureInstaller(cmd *cobra.Command, args []string) error {
targetPackRoot := viper.GetString("pack-root")
checkConnection := viper.GetBool("check-connection") // TODO: never set

download := cmd.Name() != "init" && cmd.Name() != "update-index" && cmd.Name() != "connection"

if targetPackRoot == installer.GetDefaultCmsisPackRoot() {
// If using the default pack root path and the public index is not found,
// initialize it
Expand All @@ -84,20 +86,19 @@ func configureInstaller(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
err = installer.SetPackRoot(targetPackRoot, false, true)
err = installer.SetPackRoot(targetPackRoot, false, false)
if err != nil {
return err
}
installer.LockPackRoot()
}
} else {
err := installer.SetPackRoot(targetPackRoot, createPackRoot, true)
err := installer.SetPackRoot(targetPackRoot, createPackRoot, download)
if err != nil {
return err
}
}
} else {
download := cmd.Name() != "init" && cmd.Name() != "connection"
err := installer.SetPackRoot(targetPackRoot, createPackRoot, download)
if err != nil {
return err
Expand Down
2 changes: 0 additions & 2 deletions cmd/commands/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"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"
)

Expand All @@ -34,7 +33,6 @@ var UpdateIndexCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
utils.SetEncodedProgress(updateIndexCmdFlags.encodedProgress)
utils.SetSkipTouch(updateIndexCmdFlags.skipTouch)
log.Infof("Updating public index")
installer.UnlockPackRoot()
err := installer.UpdatePublicIndex("", true, updateIndexCmdFlags.sparse, false, updateIndexCmdFlags.downloadUpdatePdscFiles, viper.GetInt("concurrent-downloads"), viper.GetInt("timeout"))
installer.LockPackRoot()
Expand Down
17 changes: 15 additions & 2 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ func UpdatePack(packPath string, checkEula, noRequirements bool, timeout int) er
}

if !pack.IsPublic || pack.isInstalled {
if !pack.isInstalled {
log.Infof("Pack \"%s\" is not installed", packPath)
}
return nil
}

Expand Down Expand Up @@ -601,10 +604,20 @@ func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPd
indexPath = strings.TrimSuffix(Installation.PublicIndexXML.URL, "/") + "/" + PublicIndex
}

log.Debugf("Updating public index with \"%v\"", indexPath)

var err error

if strings.HasPrefix(indexPath, "http://") || strings.HasPrefix(indexPath, "https://") {
if !strings.HasPrefix(indexPath, "https://127.0.0.1") {
err = utils.CheckConnection(indexPath, 0)
if err != nil && errors.Unwrap(err) == errs.ErrOffline {
return err
}
}
}

log.Infof("Updating public index")
log.Debugf("Updating public index with \"%v\"", indexPath)

if strings.HasPrefix(indexPath, "http://") || strings.HasPrefix(indexPath, "https://") {
if !strings.HasPrefix(indexPath, "https://") {
log.Warnf("Non-HTTPS url: \"%s\"", indexPath)
Expand Down
9 changes: 2 additions & 7 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,10 @@ func CheckConnection(url string, timeOut int) error {
}
resp, err := client.Get(url)
connStatus := "offline"
if err != nil {
if !GetEncodedProgress() {
log.Info(err)
}
} else {
if err == nil {
connStatus = "online"
if !GetEncodedProgress() {
text := fmt.Sprintf("Respond: %v:%v (%v)", resp.StatusCode, resp.Status, connStatus)
log.Info(text)
log.Debugf("Respond: %v (%v)", resp.Status, connStatus)
}
}

Expand Down

0 comments on commit 2786cc5

Please sign in to comment.