generated from LinuxSuRen/github-go
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context support in the commands (#37)
* Add context support in the commands * Add more test cases * Upload test coverage to codecov.io
- Loading branch information
1 parent
42825a0
commit 255c95a
Showing
11 changed files
with
244 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
extver "github.com/linuxsuren/cobra-extension/version" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewRoot returns the root command | ||
func NewRoot() (cmd *cobra.Command) { | ||
func NewRoot(cxt context.Context) (cmd *cobra.Command) { | ||
cmd = &cobra.Command{ | ||
Use: "hd", | ||
Short: "HTTP download tool", | ||
} | ||
|
||
cmd.AddCommand( | ||
NewGetCmd(), NewInstallCmd(), newFetchCmd(), newSearchCmd(), newTestCmd(), | ||
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newTestCmd(), | ||
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil)) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"sync" | ||
) | ||
|
||
func getOrDefault(key, def string, data map[string]string) (result string) { | ||
var ok bool | ||
if result, ok = data[key]; !ok { | ||
result = def | ||
} | ||
return | ||
} | ||
|
||
func getReplacement(key string, data map[string]string) (result string) { | ||
return getOrDefault(key, key, data) | ||
} | ||
|
||
func getRoundTripper(ctx context.Context) (tripper *http.RoundTripper) { | ||
roundTripper := ctx.Value("roundTripper") | ||
|
||
var ok bool | ||
if tripper, ok = roundTripper.(*http.RoundTripper); ok { | ||
tripper = nil | ||
} | ||
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 != "" { | ||
command.Dir = dir | ||
} | ||
|
||
//var stdout []byte | ||
//var errStdout error | ||
stdoutIn, _ := command.StdoutPipe() | ||
stderrIn, _ := command.StderrPipe() | ||
err = command.Start() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// cmd.Wait() should be called only after we finish reading | ||
// from stdoutIn and stderrIn. | ||
// wg ensures that we finish | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
_, _ = copyAndCapture(os.Stdout, stdoutIn) | ||
wg.Done() | ||
}() | ||
|
||
_, _ = copyAndCapture(os.Stderr, stderrIn) | ||
|
||
wg.Wait() | ||
|
||
err = command.Wait() | ||
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) | ||
for { | ||
n, err := r.Read(buf[:]) | ||
if n > 0 { | ||
d := buf[:n] | ||
out = append(out, d...) | ||
_, err := w.Write(d) | ||
if err != nil { | ||
return out, err | ||
} | ||
} | ||
if err != nil { | ||
// Read returns io.EOF at the end of file, which is not an error for us | ||
if err == io.EOF { | ||
err = nil | ||
} | ||
return out, err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"github.com/linuxsuren/http-downloader/cmd" | ||
"os" | ||
) | ||
|
||
func main() { | ||
if err := cmd.NewRoot().Execute(); err != nil { | ||
if err := cmd.NewRoot(context.TODO()).Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net_test | ||
|
||
import ( | ||
"github.com/linuxsuren/http-downloader/pkg/net" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestError(t *testing.T) { | ||
err := net.DownloadError{ | ||
Message: "message", | ||
StatusCode: 200, | ||
} | ||
assert.Contains(t, err.Error(), "message") | ||
assert.Contains(t, err.Error(), "200") | ||
} |
Oops, something went wrong.