Skip to content

Commit 9dec242

Browse files
authored
fix: setup command can run any times without error (#321)
* fix: setup command can run any times without error * fix the lint errors
1 parent b075978 commit 9dec242

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+382
-255
lines changed

.github/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
make pre-commit

.github/workflows/coverage-report.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Set up Go
1313
uses: actions/setup-go@v3
1414
with:
15-
go-version: 1.16.x
15+
go-version: 1.18.x
1616
- uses: actions/[email protected]
1717
- name: Test
1818
run: |

.github/workflows/pull-request.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Set up Go 1.16
1414
uses: actions/setup-go@v3
1515
with:
16-
go-version: 1.16
16+
go-version: 1.18
1717
id: go
1818
- name: Check out code into the Go module directory
1919
uses: actions/[email protected]
@@ -55,7 +55,7 @@ jobs:
5555
- name: Set up Go 1.16
5656
uses: actions/setup-go@v3
5757
with:
58-
go-version: 1.16
58+
go-version: 1.18
5959
id: go
6060
- name: Check out code into the Go module directory
6161
uses: actions/[email protected]

.github/workflows/release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v3
1818
with:
19-
go-version: 1.16.x
19+
go-version: 1.18.x
2020
- name: Upgrade upx
2121
run: |
2222
# try to fix https://github.com/jenkins-zh/jenkins-cli/issues/493

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ build-linux: fmt lint
99

1010
test: fmt lint
1111
go test ./... -coverprofile coverage.out
12-
12+
pre-commit: fmt lint test build
13+
cp-pre-commit:
14+
cp .github/pre-commit .git/hooks/pre-commit
1315
run:
1416
go run main.go
1517

cmd/get_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8-
"io/ioutil"
8+
"io"
99
"net/http"
1010
"os"
1111
"path"
@@ -142,7 +142,7 @@ func TestRunE(t *testing.T) {
142142
Header: map[string][]string{
143143
"Content-Length": {"100"},
144144
},
145-
Body: ioutil.NopCloser(bytes.NewBufferString("responseBody")),
145+
Body: io.NopCloser(bytes.NewBufferString("responseBody")),
146146
}
147147
roundTripper.EXPECT().
148148
RoundTrip(mockRequest).Return(mockResponse, nil)
@@ -173,7 +173,7 @@ func TestRunE(t *testing.T) {
173173
Header: map[string][]string{
174174
"Content-Length": {"100"},
175175
},
176-
Body: ioutil.NopCloser(bytes.NewBufferString("responseBody")),
176+
Body: io.NopCloser(bytes.NewBufferString("responseBody")),
177177
}
178178
roundTripper.EXPECT().
179179
RoundTrip(mockRequest).Return(mockResponse, nil)
@@ -188,7 +188,7 @@ func TestRunE(t *testing.T) {
188188
Header: map[string][]string{
189189
"Content-Length": {"100"},
190190
},
191-
Body: ioutil.NopCloser(bytes.NewBufferString("responseBody")),
191+
Body: io.NopCloser(bytes.NewBufferString("responseBody")),
192192
}
193193
roundTripper.EXPECT().
194194
RoundTrip(mockRequest1).Return(mockResponse1, nil)
@@ -203,7 +203,7 @@ func TestRunE(t *testing.T) {
203203
Header: map[string][]string{
204204
"Content-Length": {"100"},
205205
},
206-
Body: ioutil.NopCloser(bytes.NewBufferString("responseBody")),
206+
Body: io.NopCloser(bytes.NewBufferString("responseBody")),
207207
}
208208
roundTripper.EXPECT().
209209
RoundTrip(mockRequest2).Return(mockResponse2, nil)

cmd/setup.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
6+
"path"
7+
58
"github.com/AlecAivazis/survey/v2"
69
"github.com/spf13/cobra"
710
"github.com/spf13/viper"
8-
"os"
9-
"path"
1011
)
1112

1213
func newSetupCommand() (cmd *cobra.Command) {
@@ -26,6 +27,7 @@ func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
2627
selector := &survey.Select{
2728
Message: "Select proxy-github",
2829
Options: []string{"gh.api.99988866.xyz", "ghproxy.com", "mirror.ghproxy.com", ""},
30+
Default: viper.Get("proxy-github"),
2931
}
3032

3133
var choose string
@@ -41,6 +43,6 @@ func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
4143
return
4244
}
4345

44-
err = viper.SafeWriteConfigAs(path.Join(configDir, "hd.yaml"))
46+
err = viper.WriteConfigAs(path.Join(configDir, "hd.yaml"))
4547
return
4648
}

cmd/util.go

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ package cmd
22

33
import (
44
"context"
5-
"github.com/spf13/cobra"
6-
"io"
75
"net/http"
8-
"os"
9-
"os/exec"
10-
"sync"
6+
7+
"github.com/spf13/cobra"
118
)
129

1310
func getOrDefault(key, def string, data map[string]string) (result string) {
@@ -33,62 +30,6 @@ func getRoundTripper(ctx context.Context) (tripper http.RoundTripper) {
3330
return
3431
}
3532

36-
func execCommandInDir(name, dir string, arg ...string) (err error) {
37-
command := exec.Command(name, arg...)
38-
if dir != "" {
39-
command.Dir = dir
40-
}
41-
42-
//var stdout []byte
43-
//var errStdout error
44-
stdoutIn, _ := command.StdoutPipe()
45-
stderrIn, _ := command.StderrPipe()
46-
err = command.Start()
47-
if err != nil {
48-
return err
49-
}
50-
51-
// cmd.Wait() should be called only after we finish reading
52-
// from stdoutIn and stderrIn.
53-
// wg ensures that we finish
54-
var wg sync.WaitGroup
55-
wg.Add(1)
56-
go func() {
57-
_, _ = copyAndCapture(os.Stdout, stdoutIn)
58-
wg.Done()
59-
}()
60-
61-
_, _ = copyAndCapture(os.Stderr, stderrIn)
62-
63-
wg.Wait()
64-
65-
err = command.Wait()
66-
return
67-
}
68-
69-
func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
70-
var out []byte
71-
buf := make([]byte, 1024, 1024)
72-
for {
73-
n, err := r.Read(buf[:])
74-
if n > 0 {
75-
d := buf[:n]
76-
out = append(out, d...)
77-
_, err := w.Write(d)
78-
if err != nil {
79-
return out, err
80-
}
81-
}
82-
if err != nil {
83-
// Read returns io.EOF at the end of file, which is not an error for us
84-
if err == io.EOF {
85-
err = nil
86-
}
87-
return out, err
88-
}
89-
}
90-
}
91-
9233
// CompletionFunc is the function for command completion
9334
type CompletionFunc func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
9435

go.mod

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/linuxsuren/http-downloader
22

3-
go 1.16
3+
go 1.18
44

55
require (
66
github.com/AlecAivazis/survey/v2 v2.3.2
@@ -11,14 +11,59 @@ require (
1111
github.com/h2non/gock v1.0.9
1212
github.com/linuxsuren/cobra-extension v0.0.16
1313
github.com/mitchellh/go-homedir v1.1.0
14-
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
1514
github.com/onsi/ginkgo v1.16.5
1615
github.com/onsi/gomega v1.18.1
1716
github.com/spf13/cobra v1.2.1
1817
github.com/spf13/pflag v1.0.5
1918
github.com/spf13/viper v1.9.0
2019
github.com/stretchr/testify v1.7.0
2120
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
22-
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
2321
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
2422
)
23+
24+
require (
25+
github.com/Microsoft/go-winio v0.4.16 // indirect
26+
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
27+
github.com/acomagu/bufpipe v1.0.3 // indirect
28+
github.com/davecgh/go-spew v1.1.1 // indirect
29+
github.com/emirpasic/gods v1.12.0 // indirect
30+
github.com/fsnotify/fsnotify v1.5.1 // indirect
31+
github.com/go-git/gcfg v1.5.0 // indirect
32+
github.com/go-git/go-billy/v5 v5.3.1 // indirect
33+
github.com/golang/protobuf v1.5.2 // indirect
34+
github.com/google/go-querystring v1.0.0 // indirect
35+
github.com/gosuri/uilive v0.0.3 // indirect
36+
github.com/hashicorp/hcl v1.0.0 // indirect
37+
github.com/imdario/mergo v0.3.12 // indirect
38+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
39+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
40+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
41+
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
42+
github.com/magiconair/properties v1.8.5 // indirect
43+
github.com/mattn/go-colorable v0.1.6 // indirect
44+
github.com/mattn/go-isatty v0.0.12 // indirect
45+
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
46+
github.com/mitchellh/mapstructure v1.4.2 // indirect
47+
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
48+
github.com/nxadm/tail v1.4.8 // indirect
49+
github.com/pelletier/go-toml v1.9.4 // indirect
50+
github.com/pmezard/go-difflib v1.0.0 // indirect
51+
github.com/sergi/go-diff v1.1.0 // indirect
52+
github.com/spf13/afero v1.6.0 // indirect
53+
github.com/spf13/cast v1.4.1 // indirect
54+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
55+
github.com/subosito/gotenv v1.2.0 // indirect
56+
github.com/xanzy/ssh-agent v0.3.0 // indirect
57+
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
58+
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
59+
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
60+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
61+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
62+
golang.org/x/text v0.3.7 // indirect
63+
google.golang.org/appengine v1.6.7 // indirect
64+
google.golang.org/protobuf v1.27.1 // indirect
65+
gopkg.in/ini.v1 v1.63.2 // indirect
66+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
67+
gopkg.in/warnings.v0 v0.1.2 // indirect
68+
gopkg.in/yaml.v2 v2.4.0 // indirect
69+
)

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,6 @@ golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5o
602602
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
603603
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
604604
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
605-
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
606605
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
607606
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
608607
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=

pkg/common/util.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package common
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
"os"
76
"path/filepath"
87
"regexp"
@@ -45,7 +44,7 @@ const (
4544
// to dir. It returns nil if dir is writable.
4645
func IsDirWriteable(dir string) error {
4746
f := filepath.Join(dir, ".touch")
48-
if err := ioutil.WriteFile(f, []byte(""), PrivateFileMode); err != nil {
47+
if err := os.WriteFile(f, []byte(""), PrivateFileMode); err != nil {
4948
return err
5049
}
5150
return os.Remove(f)

pkg/exec/command.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ func (e DefaultExecer) LookPath(file string) (string, error) {
2222
return exec.LookPath(file)
2323
}
2424

25-
// LookPath is the wrapper of os/exec.LookPath
26-
//
27-
// Deprecated: Use DefaultExecer.LookPath instead
28-
func LookPath(file string) (string, error) {
29-
return DefaultExecer{}.LookPath(file)
30-
}
31-
3225
// RunCommandAndReturn runs a command, then returns the output
3326
func RunCommandAndReturn(name, dir string, args ...string) (result string, err error) {
3427
stdout := &bytes.Buffer{}
@@ -104,7 +97,7 @@ func RunCommandWithSudo(name string, args ...string) (err error) {
10497

10598
func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
10699
var out []byte
107-
buf := make([]byte, 1024, 1024)
100+
buf := make([]byte, 1024)
108101
for {
109102
n, err := r.Read(buf[:])
110103
if n > 0 {

0 commit comments

Comments
 (0)