Skip to content

Commit

Permalink
feat: support to install package on windows (#365)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen committed Feb 28, 2023
1 parent b706d1b commit 579ac58
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 34 deletions.
12 changes: 11 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Cannot find your desired package? Please run command: hd fetch --reset, then try
"Indicate if install it via go install github.com/xxx/xxx")
flags.StringVarP(&opt.fromBranch, "from-branch", "", "master",
"Only works if the flag --from-source is true")
flags.StringVarP(&opt.target, "target", "", "/usr/local/bin", "The target installation directory")
flags.StringVarP(&opt.target, "target", "", opt.getDefaultInstallDir(), "The target installation directory")
flags.BoolVarP(&opt.goget, "goget", "", viper.GetBool("fetch"),
"Use command goget to download the binary, only works if the flag --from-source is true")

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

func (o *installOption) getDefaultInstallDir() string {
switch o.execer.OS() {
case "linux", "darwin":
return "/usr/local/bin"
case "windows":
return `C:\Program Files (x86)\Common Files`
}
return ""
}

type installOption struct {
*downloadOption
Download bool
Expand Down
37 changes: 37 additions & 0 deletions cmd/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,40 @@ func TestInstall(t *testing.T) {
})
}
}

func TestGetDefaultInstallDir(t *testing.T) {
tests := []struct {
name string
execer exec.Execer
expect string
}{{
name: "linux",
execer: exec.FakeExecer{
ExpectOS: "linux",
},
expect: "/usr/local/bin",
}, {
name: "darwin",
execer: exec.FakeExecer{
ExpectOS: "darwin",
},
expect: "/usr/local/bin",
}, {
name: "windows",
execer: exec.FakeExecer{
ExpectOS: "windows",
},
expect: `C:\Program Files (x86)\Common Files`,
}, {
name: "unknown",
execer: exec.FakeExecer{},
expect: "",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opt := &installOption{execer: tt.execer}
result := opt.getDefaultInstallDir()
assert.Equal(t, tt.expect, result)
})
}
}
4 changes: 1 addition & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/linuxsuren/http-downloader/pkg/log"

"github.com/AlecAivazis/survey/v2/terminal"
extpkg "github.com/linuxsuren/cobra-extension/pkg"
extver "github.com/linuxsuren/cobra-extension/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -45,8 +44,7 @@ func NewRoot(cxt context.Context) (cmd *cobra.Command) {
cxt = context.WithValue(cxt, log.LoggerContextKey, log.GetLogger())
cmd.AddCommand(
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newSetupCommand(v, stdio),
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil),
extpkg.NewCompletionCmd(cmd))
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil))

for _, c := range cmd.Commands() {
registerFlagCompletionFunc(c, "provider", ArrayCompletion(ProviderGitHub, ProviderGitee))
Expand Down
4 changes: 2 additions & 2 deletions pkg/installer/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ func FindByKeyword(keyword, configDir string) (result []string) {
if files, err := filepath.Glob(path.Join(configDir, "config/**/*.yml")); err == nil {
for _, metaFile := range files {
ext := path.Ext(metaFile)
fileName := path.Base(metaFile)
org := path.Base(path.Dir(metaFile))
fileName := filepath.Base(metaFile)
org := filepath.Base(filepath.Dir(metaFile))
repo := strings.TrimSuffix(fileName, ext)

if !strings.Contains(repo, keyword) && !hasKeyword(metaFile, keyword) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/installer/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package installer

import (
"fmt"
"github.com/linuxsuren/http-downloader/pkg/common"
"github.com/linuxsuren/http-downloader/pkg/compress"
"github.com/linuxsuren/http-downloader/pkg/exec"
"io"
"os"
"path"
"path/filepath"
"strings"

"github.com/linuxsuren/http-downloader/pkg/common"
"github.com/linuxsuren/http-downloader/pkg/compress"
"github.com/linuxsuren/http-downloader/pkg/exec"
)

// Install installs a package
func (o *Installer) Install() (err error) {
if o.Execer.OS() == "windows" {
o.Name = fmt.Sprintf("%s.exe", o.Name)
}
targetBinary := o.Name
if o.Package != nil && o.Package.TargetBinary != "" {
// this is the desired binary file
Expand Down
58 changes: 33 additions & 25 deletions wix.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
{
"product": "HTTP downloader",
"company": "LinuxSuRen",
"license": "LICENSE",
"upgrade-code": "7c0a5736-5b8e-4176-b350-613fa2d8a1b3",
"files": {
"guid": "6e6dcb19-3cf6-46d1-ac56-c6fb39485c9d",
"items": [
"hd.exe"
]
},
"env": {
"guid": "94faac3d-4478-431c-8497-fba55dcfb249",
"vars": [
{
"name": "PATH",
"value": "[INSTALLDIR]",
"permanent": "yes",
"system": "no",
"action": "set",
"part": "last"
}
]
},
"shortcuts": {}
}
"product": "HTTP downloader",
"company": "LinuxSuRen",
"license": "LICENSE",
"upgrade-code": "7c0a5736-5b8e-4176-b350-613fa2d8a1b3",
"files": {
"guid": "6e6dcb19-3cf6-46d1-ac56-c6fb39485c9d",
"items": [
"hd.exe"
]
},
"env": {
"guid": "94faac3d-4478-431c-8497-fba55dcfb249",
"vars": [
{
"name": "PATH",
"value": "[INSTALLDIR]",
"permanent": "yes",
"system": "no",
"action": "set",
"part": "last"
},
{
"name": "PATH",
"value": "C:\\Program Files (x86)\\Common Files",
"permanent": "yes",
"system": "no",
"action": "set",
"part": "last"
}
]
},
"shortcuts": {}
}

0 comments on commit 579ac58

Please sign in to comment.