Skip to content

Commit b80f4f5

Browse files
authored
feat: support to set the file mod (#347)
Usage: hd get https://xxx --mod 0755
1 parent d4190d0 commit b80f4f5

File tree

15 files changed

+233
-29
lines changed

15 files changed

+233
-29
lines changed

cmd/fetch.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/linuxsuren/http-downloader/pkg"
7+
"github.com/linuxsuren/http-downloader/pkg/log"
78
"os"
89
"strings"
910
"time"
@@ -59,6 +60,8 @@ func (o *fetchOption) preRunE(c *cobra.Command, _ []string) (err error) {
5960
}
6061

6162
func (o *fetchOption) runE(c *cobra.Command, _ []string) (err error) {
63+
logger := log.GetLoggerFromContextOrDefault(c)
64+
6265
var i int
6366
for i = 0; i < o.retry; i++ {
6467
err = o.fetcher.FetchLatestRepo(o.Provider, o.branch, c.OutOrStdout())
@@ -67,10 +70,10 @@ func (o *fetchOption) runE(c *cobra.Command, _ []string) (err error) {
6770
break
6871
}
6972
o.setTimeout(c)
70-
c.Print(".")
73+
logger.Print(".")
7174
}
7275
if i >= 1 {
73-
c.Println()
76+
logger.Println()
7477
}
7578
return
7679
}

cmd/get.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"github.com/linuxsuren/http-downloader/pkg/common"
8+
"github.com/linuxsuren/http-downloader/pkg/log"
79
"io"
10+
"io/fs"
811
"net/http"
912
"net/url"
1013
sysos "os"
@@ -45,6 +48,8 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
4548
"If you accept preRelease as the binary asset from GitHub")
4649
flags.BoolVarP(&opt.AcceptPreRelease, "pre", "", false,
4750
"Same with option --accept-preRelease")
51+
flags.BoolVarP(&opt.Force, "force", "f", false, "Overwrite the exist file if this is true")
52+
flags.IntVarP(&opt.Mod, "mod", "", -1, "The file permission, -1 means using the system default")
4853

4954
flags.IntVarP(&opt.Timeout, "time", "", 10,
5055
`The default timeout in seconds with the HTTP request`)
@@ -99,6 +104,8 @@ type downloadOption struct {
99104
AcceptPreRelease bool
100105
RoundTripper http.RoundTripper
101106
Magnet bool
107+
Force bool
108+
Mod int
102109

103110
ContinueAt int64
104111

@@ -233,6 +240,7 @@ func findAnchor(n *html.Node) (items []string) {
233240
}
234241

235242
func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
243+
logger := log.GetLoggerFromContextOrDefault(cmd)
236244
defer func() {
237245
if o.cancel != nil {
238246
o.cancel()
@@ -271,6 +279,13 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
271279
return
272280
}
273281

282+
// check if want to overwrite the exist file
283+
logger.Println("output file is", o.Output)
284+
if common.Exist(o.Output) && !o.Force {
285+
logger.Printf("The output file: '%s' was exist, please use flag --force if you want to overwrite it.\n", o.Output)
286+
return
287+
}
288+
274289
if o.Magnet || strings.HasPrefix(o.URL, "magnet:?") {
275290
err = downloadMagnetFile(o.ProxyGitHub, o.URL, o.execer)
276291
return
@@ -279,8 +294,9 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
279294
targetURL := o.URL
280295
if o.ProxyGitHub != "" {
281296
targetURL = strings.Replace(targetURL, "github.com", fmt.Sprintf("%s/github.com", o.ProxyGitHub), 1)
297+
targetURL = strings.Replace(targetURL, "raw.githubusercontent.com", fmt.Sprintf("%s/https://raw.githubusercontent.com", o.ProxyGitHub), 1)
282298
}
283-
cmd.Printf("start to download from %s\n", targetURL)
299+
logger.Printf("start to download from %s\n", targetURL)
284300
if o.Thread <= 1 {
285301
downloader := &net.ContinueDownloader{}
286302
downloader.WithoutProxy(o.NoProxy).
@@ -294,6 +310,11 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
294310
WithRoundTripper(o.RoundTripper)
295311
err = downloader.Download(targetURL, o.Output, o.Thread)
296312
}
313+
314+
// set file permission
315+
if o.Mod != -1 {
316+
err = sysos.Chmod(o.Output, fs.FileMode(o.Mod))
317+
}
297318
return
298319
}
299320

cmd/install.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/linuxsuren/http-downloader/pkg/version"
1212
"github.com/spf13/cobra"
1313
"github.com/spf13/viper"
14+
"log"
1415
sysos "os"
1516
"path"
1617
"runtime"
@@ -89,7 +90,13 @@ func (o *installOption) shouldInstall() (should, exist bool) {
8990
var greater bool
9091
if name, lookErr := o.execer.LookPath(o.tool); lookErr == nil {
9192
exist = true
92-
if data, err := o.execer.Command(name, "version"); err == nil &&
93+
versionCmd := "version"
94+
if o.downloadOption != nil && o.downloadOption.Package != nil && o.downloadOption.Package.VersionCmd != "" {
95+
versionCmd = o.downloadOption.Package.VersionCmd
96+
}
97+
98+
log.Println("check target version via", name, versionCmd)
99+
if data, err := o.execer.Command(name, versionCmd); err == nil &&
93100
(o.downloadOption.Package != nil && o.downloadOption.Package.Version != "") {
94101
greater = version.GreatThan(o.downloadOption.Package.Version, string(data))
95102
}
@@ -160,6 +167,7 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
160167

161168
// install a package from hd-home
162169
if o.Download {
170+
log.Println("check if it should be installed")
163171
if should, exist := o.shouldInstall(); !should {
164172
if exist {
165173
cmd.Printf("%s is already exist, please use the flag --force if you install it again\n", o.tool)
@@ -175,7 +183,7 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
175183
if o.Package == nil {
176184
o.Package = &installer.HDConfig{}
177185
}
178-
if o.target != "" {
186+
if o.target != "" && o.Package.TargetDirectory == "" {
179187
o.Package.TargetDirectory = o.target
180188
}
181189
if o.Package.TargetDirectory == "" {
@@ -185,6 +193,7 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
185193
return
186194
}
187195

196+
log.Println("target directory", o.Package.TargetDirectory)
188197
process := &installer.Installer{
189198
Source: o.downloadOption.Output,
190199
Name: o.name,

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"github.com/linuxsuren/http-downloader/pkg/log"
67
"os"
78
"runtime"
89

@@ -31,6 +32,7 @@ func NewRoot(cxt context.Context) (cmd *cobra.Command) {
3132
Err: os.Stderr,
3233
}
3334

35+
cxt = context.WithValue(cxt, log.LoggerContextKey, log.GetLogger())
3436
cmd.AddCommand(
3537
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newSetupCommand(v, stdio),
3638
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil),

cmd/setup.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
3535
provider string
3636
)
3737

38-
if proxyGitHub, err = selectFromList([]string{"ghproxy.com", "gh.api.99988866.xyz", "mirror.ghproxy.com", ""},
38+
if proxyGitHub, err = selectFromList([]string{"", "ghproxy.com", "gh.api.99988866.xyz", "mirror.ghproxy.com"},
3939
o.v.GetString("proxy-github"),
4040
"Select proxy-github", o.stdio); err == nil {
41+
fmt.Println(proxyGitHub)
4142
o.v.Set("proxy-github", proxyGitHub)
4243
} else {
4344
return
@@ -56,6 +57,7 @@ func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
5657
return
5758
}
5859

60+
fmt.Println(o.v.GetString("proxy-github"))
5961
err = o.v.WriteConfigAs(path.Join(configDir, "hd.yaml"))
6062
return
6163
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ require (
2727
github.com/creack/pty v1.1.17
2828
)
2929

30+
require github.com/prometheus/client_golang v1.11.1 // indirect
31+
3032
require (
3133
github.com/Microsoft/go-winio v0.4.16 // indirect
3234
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2

0 commit comments

Comments
 (0)