Skip to content

Commit

Permalink
Merge branch 'main' into go_version_update
Browse files Browse the repository at this point in the history
  • Loading branch information
soumeh01 authored Nov 13, 2023
2 parents 2806a98 + 49b7939 commit f04b870
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 11 deletions.
62 changes: 62 additions & 0 deletions cmd/commands/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */

package commands

import (
"github.com/open-cmsis-pack/cpackget/cmd/installer"
"github.com/open-cmsis-pack/cpackget/cmd/utils"
"github.com/spf13/cobra"
)

var connectionCmdFlags struct {
// downloadPdscFiles forces all pdsc files from the public index to be downloaded
downloadPdscFiles bool

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

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

// check connection status
checkConnection bool
}

var ConnectionCmd = &cobra.Command{
Use: "connection [<url>]",
Short: "Check online connection to default or given URL",
Long: `Checks if the given or default url is accessible
The url is optional. Ex "cpackget connection https://www.keil.com/pack"`,
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
utils.SetEncodedProgress(connectionCmdFlags.encodedProgress)
utils.SetSkipTouch(connectionCmdFlags.skipTouch)

var indexPath string
if len(args) > 0 {
indexPath = args[0]
}
createPackRoot = false
var err error

if indexPath == "" { // try to fetch from environment
err = configureInstaller(cmd, args)
if err != nil {
return err
}
}

indexPath, err = installer.GetIndexPath(indexPath)
if err != nil {
return err
}

err = utils.CheckConnection(indexPath, viper.GetInt("timeout"))
return err
},
}

func init() {
ConnectionCmd.Flags().BoolVarP(&connectionCmdFlags.encodedProgress, "encoded-progress", "E", false, "Reports encoded progress for files and download when used by other tools")
}
60 changes: 60 additions & 0 deletions cmd/commands/connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */

package commands_test

import (
"errors"
"testing"
)

var (
urlPath string = "https://www.keil.com"
wrongURLPath string = "https://www.keilll.com"
)

var connectionCmdTests = []TestCase{
{
name: "test help command",
args: []string{"help", "connection"},
expectedErr: nil,
},
{
name: "test checking connection",
args: []string{"connection", urlPath},
expectedErr: nil,
},
{
name: "test checking invalid url",
args: []string{"connection", wrongURLPath},
expectedErr: errors.New("remote server is offline or cannot be reached"),
},

{ // set up environment for next test
name: "test checking default connection",
args: []string{"init"},
noCleanup: true,
setUpFunc: func(t *TestCase) {
server := NewServer()
t.args = append(t.args, server.URL()+"index.pidx")
server.AddRoute("index.pidx", []byte(`<?xml version="1.0" encoding="UTF-8" ?>
<index schemaVersion="1.1.0" xs:noNamespaceSchemaLocation="PackIndex.xsd" xmlns:xs="https://www.w3.org/2001/XMLSchema-instance">
<vendor>TheVendor</vendor>
<url>https://www.keil.com/</url>
<timestamp>2021-10-17T12:21:59.1747971+00:00</timestamp>
<pindex>
<pdsc url="https://www.keil.com" vendor="Keil" name="PackName" version="1.2.3" />
</pindex>
</index>`))
},
},
{
name: "test checking default connection",
args: []string{"connection"},
expectedErr: nil,
},
}

func TestConnectionCmd(t *testing.T) {
runTests(t, connectionCmdTests)
}
9 changes: 6 additions & 3 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var AllCommands = []*cobra.Command{
ChecksumVerifyCmd,
SignatureCreateCmd,
SignatureVerifyCmd,
ConnectionCmd,
}

// createPackRoot is a flag that determines if the pack root should be created or not
Expand Down Expand Up @@ -68,10 +69,12 @@ func configureInstaller(cmd *cobra.Command, args []string) error {
}

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

if targetPackRoot == installer.GetDefaultCmsisPackRoot() {
// If using the default pack root path and the public index is not found,
// initialize it
if !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) {
if !checkConnection && !utils.FileExists(filepath.Join(targetPackRoot, ".Web", "index.pidx")) {
err := installer.SetPackRoot(targetPackRoot, true)
if err != nil {
return err
Expand Down Expand Up @@ -110,10 +113,10 @@ var flags struct {
}

var Version string
var CopyRight string
var Copyright string

func printVersionAndLicense(file io.Writer) {
fmt.Fprintf(file, "cpackget version %v %s\n", strings.ReplaceAll(Version, "v", ""), CopyRight)
fmt.Fprintf(file, "cpackget version %v %s\n", strings.ReplaceAll(Version, "v", ""), Copyright)
}

// UsageTemplate returns usage template for the command.
Expand Down
7 changes: 5 additions & 2 deletions cmd/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type TestCase struct {
validationFunc func(t *testing.T)
assert *assert.Assertions
env map[string]string
noCleanup bool
}

type Server struct {
Expand Down Expand Up @@ -161,8 +162,10 @@ func runTests(t *testing.T, tests []TestCase) {
}

defer func() {
utils.UnsetReadOnlyR(localTestingDir)
os.RemoveAll(localTestingDir)
if !test.noCleanup {
utils.UnsetReadOnlyR(localTestingDir)
os.RemoveAll(localTestingDir)
}
}()

cmd := commands.NewCli()
Expand Down
33 changes: 29 additions & 4 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/open-cmsis-pack/cpackget/cmd/utils"
"github.com/open-cmsis-pack/cpackget/cmd/xml"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/mod/semver"
"golang.org/x/sync/semaphore"
)
Expand Down Expand Up @@ -422,6 +423,26 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int
return nil
}

func GetIndexPath(indexPath string) (string, error) {
if indexPath == "" {
indexPath = strings.TrimSuffix(Installation.PublicIndexXML.URL, "/")
}

if !utils.GetEncodedProgress() {
log.Infof("Using path: \"%v\"", indexPath)
}

var err error

if strings.HasPrefix(indexPath, "http://") || strings.HasPrefix(indexPath, "https://") {
if !strings.HasPrefix(indexPath, "https://") {
log.Warnf("Non-HTTPS url: \"%s\"", indexPath)
}
}

return indexPath, err
}

// UpdatePublicIndex receives a index path and place it under .Web/index.pidx.
func UpdatePublicIndex(indexPath string, overwrite bool, sparse bool, downloadPdsc bool, downloadRemainingPdscFiles bool, concurrency int, timeout int) error {
// TODO: Remove overwrite when cpackget v1 gets released
Expand Down Expand Up @@ -843,10 +864,14 @@ func SetPackRoot(packRoot string, create bool) error {
if !utils.DirExists(packRoot) && !create {
return errs.ErrPackRootDoesNotExist
}
if packRoot == GetDefaultCmsisPackRoot() {
log.Infof("Using pack root: \"%v\" (default mode - no specific CMSIS_PACK_ROOT chosen)", packRoot)
} else {
log.Infof("Using pack root: \"%v\"", packRoot)

checkConnection := viper.GetBool("check-connection")
if checkConnection && !utils.GetEncodedProgress() {
if packRoot == GetDefaultCmsisPackRoot() {
log.Infof("Using pack root: \"%v\" (default mode - no specific CMSIS_PACK_ROOT chosen)", packRoot)
} else {
log.Infof("Using pack root: \"%v\"", packRoot)
}
}

Installation = &PacksInstallationType{
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
start := time.Now()

commands.Version = version
commands.CopyRight = copyRight
commands.Copyright = copyRight
cmd := commands.NewCli()
err := cmd.Execute()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/utils/encodedProgress.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (p *EncodedProgress) Write(bs []byte) (int, error) {
* C: Currently processed bytes or numbers of files
* J: Total number of files beeing processed
* L: License file follows
* O: Online connection Status [offline|online]
*/
func (p *EncodedProgress) Print() {
newPercent := int(float64(p.current) / float64(p.total) * 100)
Expand Down
30 changes: 30 additions & 0 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,36 @@ func DownloadFile(URL string, timeout int) (string, error) {
return filePath, err
}

func CheckConnection(url string, timeOut int) error {
timeout := time.Duration(timeOut) * time.Second
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(url)
connStatus := "offline"
if err != nil {
if !GetEncodedProgress() {
log.Info(err)
}
} else {
connStatus = "online"
if !GetEncodedProgress() {
text := fmt.Sprintf("Respond: %v:%v (%v)", resp.StatusCode, resp.Status, connStatus)
log.Info(text)
}
}

if GetEncodedProgress() {
log.Infof("[O:%v]", connStatus)
}

if connStatus == "offline" {
return errors.New("remote server is offline or cannot be reached")
}

return nil
}

// FileExists checks if filePath is an actual file in the local file system
func FileExists(filePath string) bool {
info, err := os.Stat(filePath)
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ coverage-report: test
go tool cover -html=cover.out

coverage-check: test
@echo Checking if test coverage is atleast 85%
@echo Checking if test coverage is atleast 84%
test `go tool cover -func cover.out | tail -1 | awk '{print ($$3 + 0)*10}'` -ge 840

test-public-index:
Expand Down

0 comments on commit f04b870

Please sign in to comment.