Skip to content

Commit

Permalink
feat: check the version before install (#322)
Browse files Browse the repository at this point in the history
* feat: check the version before install

* add unit tests

* support to fetch repo as background task
  • Loading branch information
LinuxSuRen authored Jan 27, 2023
1 parent 9dec242 commit c9ab375
Show file tree
Hide file tree
Showing 78 changed files with 1,356 additions and 423 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ jobs:
run: |
sudo ./release/http-downloader_linux_amd64_v1/hd install jenkins-zh/jenkins-cli/jcli
jcli version
sudo ./release/http-downloader_linux_amd64_v1/hd install ks
ks version
- name: Run Trivy vulnerability scanner
uses: aquasecurity/[email protected]
if: github.event_name == 'pull_request'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ bin/
*/**/*.xml
coverage.out
node_modules
release/
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ gen-mock:

update:
git fetch
git reset --hard origin/$(shell git branch --show-current)
git reset --hard origin/$(shell git branch --show-current)
goreleaser:
goreleaser build --snapshot --rm-dist
31 changes: 17 additions & 14 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package cmd
import (
"context"
"fmt"
"github.com/linuxsuren/http-downloader/pkg"
"os"

"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
"os"
)

func newFetchCmd(context.Context) (cmd *cobra.Command) {
opt := &fetchOption{}
opt := &fetchOption{
fetcher: &installer.DefaultFetcher{},
}
cmd = &cobra.Command{
Use: "fetch",
Short: "Fetch the latest hd config",
Expand All @@ -28,31 +32,30 @@ func newFetchCmd(context.Context) (cmd *cobra.Command) {
return
}

func (o *fetchOption) preRunE(_ *cobra.Command, _ []string) (err error) {
fetcher := &installer.DefaultFetcher{}
func (o *fetchOption) preRunE(c *cobra.Command, _ []string) (err error) {
if c.Context() != nil {
o.fetcher.SetContext(c.Context())
}
if o.reset {
var configDir string
if configDir, err = fetcher.GetConfigDir(); err == nil {
if err = os.RemoveAll(configDir); err != nil {
err = fmt.Errorf("failed to remove directory: %s, error %v", configDir, err)
return
}
if configDir, err = o.fetcher.GetConfigDir(); err == nil {
err = os.RemoveAll(configDir)
err = pkg.ErrorWrap(err, "failed to remove directory: %s, error %v", configDir, err)
} else {
err = fmt.Errorf("failed to get config directory, error %v", err)
return
}
}
return
}

func (o *fetchOption) runE(cmd *cobra.Command, _ []string) (err error) {
fetcher := &installer.DefaultFetcher{}
return fetcher.FetchLatestRepo(o.Provider, o.branch, cmd.OutOrStdout())
return o.fetcher.FetchLatestRepo(o.Provider, o.branch, cmd.OutOrStdout())
}

type fetchOption struct {
searchOption

branch string
reset bool
branch string
reset bool
fetcher installer.Fetcher
}
74 changes: 73 additions & 1 deletion cmd/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package cmd

import (
"context"
"github.com/stretchr/testify/assert"
"errors"
"os"
"path"
"testing"

"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func Test_newFetchCmd(t *testing.T) {
Expand All @@ -29,3 +35,69 @@ func Test_newFetchCmd(t *testing.T) {
})
}
}

func TestFetchPreRunE(t *testing.T) {
tests := []struct {
name string
opt *fetchOption
hasErr bool
}{{
name: "not reset",
opt: &fetchOption{},
hasErr: false,
}, {
name: "reset, cannot get config dir",
opt: &fetchOption{
reset: true,
fetcher: &installer.FakeFetcher{
GetConfigDirErr: errors.New("no config dir"),
},
},
hasErr: true,
}, {
name: "reset, remove dir",
opt: &fetchOption{
reset: true,
fetcher: &installer.FakeFetcher{
ConfigDir: path.Join(os.TempDir(), "hd-config"),
},
},
hasErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &cobra.Command{}
err := tt.opt.preRunE(c, nil)
if tt.hasErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}

func TestFetchRunE(t *testing.T) {
tests := []struct {
name string
opt *fetchOption
hasErr bool
}{{
name: "normal",
opt: &fetchOption{
fetcher: &installer.FakeFetcher{},
},
hasErr: false,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &cobra.Command{}
err := tt.opt.runE(c, nil)
if tt.hasErr {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}
53 changes: 41 additions & 12 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"context"
"fmt"
"net/http"
Expand All @@ -9,6 +10,7 @@ import (
"path"
"runtime"
"strings"
"sync"

"github.com/linuxsuren/http-downloader/pkg"
"github.com/linuxsuren/http-downloader/pkg/installer"
Expand All @@ -20,9 +22,7 @@ import (

// newGetCmd return the get command
func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
opt := &downloadOption{
RoundTripper: getRoundTripper(ctx),
}
opt := newDownloadOption(ctx)
cmd = &cobra.Command{
Use: "get",
Short: "download the file",
Expand Down Expand Up @@ -68,8 +68,18 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
return
}

func newDownloadOption(ctx context.Context) *downloadOption {
return &downloadOption{
RoundTripper: getRoundTripper(ctx),
fetcher: &installer.DefaultFetcher{},
wait: &sync.WaitGroup{},
}
}

type downloadOption struct {
searchOption
cancel context.CancelFunc
wait *sync.WaitGroup

URL string
Category string
Expand All @@ -94,12 +104,13 @@ type downloadOption struct {
PrintCategories bool

// inner fields
name string
Tar bool
Package *installer.HDConfig
org string
repo string
fetcher installer.Fetcher
name string
Tar bool
Package *installer.HDConfig
org string
repo string
fetcher installer.Fetcher
ExpectVersion string // should be like >v1.1.0
}

const (
Expand All @@ -111,14 +122,19 @@ const (

func (o *downloadOption) fetch() (err error) {
if !o.Fetch {
o.wait.Add(1)
go func() {
// no need to handle the error due to this is a background task
if o.fetcher != nil {
err = o.fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, bytes.NewBuffer(nil))
}
o.wait.Done()
}()
return
}

// fetch the latest config
fmt.Println("start to fetch the config")
if o.fetcher == nil {
o.fetcher = &installer.DefaultFetcher{}
}
if err = o.fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
return
Expand All @@ -133,6 +149,11 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
return
}

if cmd.Context() != nil {
ctx, cancel := context.WithCancel(cmd.Context())
o.cancel = cancel
o.fetcher.SetContext(ctx)
}
if err = o.fetch(); err != nil {
return
}
Expand All @@ -147,6 +168,7 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
}

targetURL := args[0]
o.Package = &installer.HDConfig{}
if !strings.HasPrefix(targetURL, "http://") && !strings.HasPrefix(targetURL, "https://") {
ins := &installer.Installer{
Provider: o.Provider,
Expand Down Expand Up @@ -186,6 +208,13 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
}

func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
defer func() {
if o.cancel != nil {
o.cancel()
o.wait.Wait()
}
}()

// only print the schema for documentation
if o.PrintSchema {
var data []byte
Expand Down
16 changes: 11 additions & 5 deletions cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -68,7 +69,9 @@ func Test_newGetCmd(t *testing.T) {
}

func TestFetch(t *testing.T) {
opt := &downloadOption{}
opt := &downloadOption{
wait: &sync.WaitGroup{},
}
opt.Fetch = false
opt.fetcher = &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")}

Expand All @@ -85,7 +88,9 @@ func TestFetch(t *testing.T) {
}

func TestPreRunE(t *testing.T) {
opt := &downloadOption{}
opt := &downloadOption{
wait: &sync.WaitGroup{},
}
opt.Fetch = true
opt.fetcher = &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")}
opt.PrintSchema = true
Expand All @@ -94,17 +99,18 @@ func TestPreRunE(t *testing.T) {
assert.Nil(t, opt.preRunE(nil, nil))

// failed to fetch
fakeC := &cobra.Command{}
opt.PrintSchema = false
assert.NotNil(t, opt.preRunE(nil, nil))
assert.NotNil(t, opt.preRunE(fakeC, nil))

// pripnt categories
opt.fetcher = &installer.FakeFetcher{}
opt.PrintCategories = true
assert.Nil(t, opt.preRunE(nil, nil))
assert.Nil(t, opt.preRunE(fakeC, nil))

// not args provided
opt.PrintCategories = false
assert.NotNil(t, opt.preRunE(nil, nil))
assert.NotNil(t, opt.preRunE(fakeC, nil))
}

func TestRunE(t *testing.T) {
Expand Down
Loading

0 comments on commit c9ab375

Please sign in to comment.