Skip to content

Commit 579ac58

Browse files
authored
feat: support to install package on windows (#365)
Co-authored-by: rick <[email protected]>
1 parent b706d1b commit 579ac58

File tree

6 files changed

+91
-34
lines changed

6 files changed

+91
-34
lines changed

cmd/install.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Cannot find your desired package? Please run command: hd fetch --reset, then try
5050
"Indicate if install it via go install github.com/xxx/xxx")
5151
flags.StringVarP(&opt.fromBranch, "from-branch", "", "master",
5252
"Only works if the flag --from-source is true")
53-
flags.StringVarP(&opt.target, "target", "", "/usr/local/bin", "The target installation directory")
53+
flags.StringVarP(&opt.target, "target", "", opt.getDefaultInstallDir(), "The target installation directory")
5454
flags.BoolVarP(&opt.goget, "goget", "", viper.GetBool("fetch"),
5555
"Use command goget to download the binary, only works if the flag --from-source is true")
5656

@@ -65,6 +65,16 @@ Cannot find your desired package? Please run command: hd fetch --reset, then try
6565
return
6666
}
6767

68+
func (o *installOption) getDefaultInstallDir() string {
69+
switch o.execer.OS() {
70+
case "linux", "darwin":
71+
return "/usr/local/bin"
72+
case "windows":
73+
return `C:\Program Files (x86)\Common Files`
74+
}
75+
return ""
76+
}
77+
6878
type installOption struct {
6979
*downloadOption
7080
Download bool

cmd/install_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,40 @@ func TestInstall(t *testing.T) {
191191
})
192192
}
193193
}
194+
195+
func TestGetDefaultInstallDir(t *testing.T) {
196+
tests := []struct {
197+
name string
198+
execer exec.Execer
199+
expect string
200+
}{{
201+
name: "linux",
202+
execer: exec.FakeExecer{
203+
ExpectOS: "linux",
204+
},
205+
expect: "/usr/local/bin",
206+
}, {
207+
name: "darwin",
208+
execer: exec.FakeExecer{
209+
ExpectOS: "darwin",
210+
},
211+
expect: "/usr/local/bin",
212+
}, {
213+
name: "windows",
214+
execer: exec.FakeExecer{
215+
ExpectOS: "windows",
216+
},
217+
expect: `C:\Program Files (x86)\Common Files`,
218+
}, {
219+
name: "unknown",
220+
execer: exec.FakeExecer{},
221+
expect: "",
222+
}}
223+
for _, tt := range tests {
224+
t.Run(tt.name, func(t *testing.T) {
225+
opt := &installOption{execer: tt.execer}
226+
result := opt.getDefaultInstallDir()
227+
assert.Equal(t, tt.expect, result)
228+
})
229+
}
230+
}

cmd/root.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/linuxsuren/http-downloader/pkg/log"
1010

1111
"github.com/AlecAivazis/survey/v2/terminal"
12-
extpkg "github.com/linuxsuren/cobra-extension/pkg"
1312
extver "github.com/linuxsuren/cobra-extension/version"
1413
"github.com/spf13/cobra"
1514
"github.com/spf13/viper"
@@ -45,8 +44,7 @@ func NewRoot(cxt context.Context) (cmd *cobra.Command) {
4544
cxt = context.WithValue(cxt, log.LoggerContextKey, log.GetLogger())
4645
cmd.AddCommand(
4746
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newSetupCommand(v, stdio),
48-
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil),
49-
extpkg.NewCompletionCmd(cmd))
47+
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil))
5048

5149
for _, c := range cmd.Commands() {
5250
registerFlagCompletionFunc(c, "provider", ArrayCompletion(ProviderGitHub, ProviderGitee))

pkg/installer/check.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ func FindByKeyword(keyword, configDir string) (result []string) {
434434
if files, err := filepath.Glob(path.Join(configDir, "config/**/*.yml")); err == nil {
435435
for _, metaFile := range files {
436436
ext := path.Ext(metaFile)
437-
fileName := path.Base(metaFile)
438-
org := path.Base(path.Dir(metaFile))
437+
fileName := filepath.Base(metaFile)
438+
org := filepath.Base(filepath.Dir(metaFile))
439439
repo := strings.TrimSuffix(fileName, ext)
440440

441441
if !strings.Contains(repo, keyword) && !hasKeyword(metaFile, keyword) {

pkg/installer/process.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ package installer
22

33
import (
44
"fmt"
5-
"github.com/linuxsuren/http-downloader/pkg/common"
6-
"github.com/linuxsuren/http-downloader/pkg/compress"
7-
"github.com/linuxsuren/http-downloader/pkg/exec"
85
"io"
96
"os"
107
"path"
118
"path/filepath"
129
"strings"
10+
11+
"github.com/linuxsuren/http-downloader/pkg/common"
12+
"github.com/linuxsuren/http-downloader/pkg/compress"
13+
"github.com/linuxsuren/http-downloader/pkg/exec"
1314
)
1415

1516
// Install installs a package
1617
func (o *Installer) Install() (err error) {
18+
if o.Execer.OS() == "windows" {
19+
o.Name = fmt.Sprintf("%s.exe", o.Name)
20+
}
1721
targetBinary := o.Name
1822
if o.Package != nil && o.Package.TargetBinary != "" {
1923
// this is the desired binary file

wix.json

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
{
2-
"product": "HTTP downloader",
3-
"company": "LinuxSuRen",
4-
"license": "LICENSE",
5-
"upgrade-code": "7c0a5736-5b8e-4176-b350-613fa2d8a1b3",
6-
"files": {
7-
"guid": "6e6dcb19-3cf6-46d1-ac56-c6fb39485c9d",
8-
"items": [
9-
"hd.exe"
10-
]
11-
},
12-
"env": {
13-
"guid": "94faac3d-4478-431c-8497-fba55dcfb249",
14-
"vars": [
15-
{
16-
"name": "PATH",
17-
"value": "[INSTALLDIR]",
18-
"permanent": "yes",
19-
"system": "no",
20-
"action": "set",
21-
"part": "last"
22-
}
23-
]
24-
},
25-
"shortcuts": {}
26-
}
2+
"product": "HTTP downloader",
3+
"company": "LinuxSuRen",
4+
"license": "LICENSE",
5+
"upgrade-code": "7c0a5736-5b8e-4176-b350-613fa2d8a1b3",
6+
"files": {
7+
"guid": "6e6dcb19-3cf6-46d1-ac56-c6fb39485c9d",
8+
"items": [
9+
"hd.exe"
10+
]
11+
},
12+
"env": {
13+
"guid": "94faac3d-4478-431c-8497-fba55dcfb249",
14+
"vars": [
15+
{
16+
"name": "PATH",
17+
"value": "[INSTALLDIR]",
18+
"permanent": "yes",
19+
"system": "no",
20+
"action": "set",
21+
"part": "last"
22+
},
23+
{
24+
"name": "PATH",
25+
"value": "C:\\Program Files (x86)\\Common Files",
26+
"permanent": "yes",
27+
"system": "no",
28+
"action": "set",
29+
"part": "last"
30+
}
31+
]
32+
},
33+
"shortcuts": {}
34+
}

0 commit comments

Comments
 (0)