Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the context check #107

Merged
merged 1 commit into from
Aug 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// newGetCmd return the get command
func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
opt := &downloadOption{
RoundTripper: *getRoundTripper(ctx),
RoundTripper: getRoundTripper(ctx),
}
cmd = &cobra.Command{
Use: "get",
Expand Down
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func newInstallCmd(ctx context.Context) (cmd *cobra.Command) {
opt := &installOption{
downloadOption: downloadOption{
RoundTripper: *getRoundTripper(ctx),
RoundTripper: getRoundTripper(ctx),
},
}
cmd = &cobra.Command{
Expand Down
30 changes: 7 additions & 23 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,19 @@ func getOrDefault(key, def string, data map[string]string) (result string) {
return
}

func getReplacement(key string, data map[string]string) (result string) {
return getOrDefault(key, key, data)
}
func getRoundTripper(ctx context.Context) (tripper http.RoundTripper) {
if ctx == nil {
return
}

func getRoundTripper(ctx context.Context) (tripper *http.RoundTripper) {
roundTripper := ctx.Value("roundTripper")

var ok bool
if tripper, ok = roundTripper.(*http.RoundTripper); ok {
tripper = nil
switch v := roundTripper.(type) {
case *http.Transport:
tripper = v
}
return
}

func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

func execCommandInDir(name, dir string, arg ...string) (err error) {
command := exec.Command(name, arg...)
if dir != "" {
Expand Down Expand Up @@ -75,10 +63,6 @@ func execCommandInDir(name, dir string, arg ...string) (err error) {
return
}

func execCommand(name string, arg ...string) (err error) {
return execCommandInDir(name, "", arg...)
}

func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
var out []byte
buf := make([]byte, 1024, 1024)
Expand Down
79 changes: 79 additions & 0 deletions cmd/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"context"
"net/http"
"reflect"
"testing"
)

func Test_getRoundTripper(t *testing.T) {
type args struct {
ctx context.Context
}
tests := []struct {
name string
args args
wantTripper http.RoundTripper
}{{
name: "context is nil",
wantTripper: nil,
}, {
name: "invalid type of RounderTripper in the context",
args: args{
ctx: context.WithValue(context.TODO(), "roundTripper", "invalid"),
},
wantTripper: nil,
}, {
name: "valid type of RounderTripper in the context",
args: args{
ctx: context.WithValue(context.TODO(), "roundTripper", &http.Transport{}),
},
wantTripper: &http.Transport{},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotTripper := getRoundTripper(tt.args.ctx); !reflect.DeepEqual(gotTripper, tt.wantTripper) {
t.Errorf("getRoundTripper() = %v, want %v", gotTripper, tt.wantTripper)
}
})
}
}

func Test_getOrDefault(t *testing.T) {
type args struct {
key string
def string
data map[string]string
}
tests := []struct {
name string
args args
wantResult string
}{{
name: "no key exist",
args: args{
key: "key",
def: "def",
data: map[string]string{},
},
wantResult: "def",
}, {
name: "key exist",
args: args{
key: "key",
def: "def",
data: map[string]string{
"key": "key",
},
},
wantResult: "key",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := getOrDefault(tt.args.key, tt.args.def, tt.args.data); gotResult != tt.wantResult {
t.Errorf("getOrDefault() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}