Skip to content

Commit 9080439

Browse files
authored
Add support to install a package with the simple name (#55)
1 parent c5627cd commit 9080439

File tree

4 files changed

+217
-4
lines changed

4 files changed

+217
-4
lines changed

cmd/get.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/AlecAivazis/survey/v2"
67
"github.com/linuxsuren/http-downloader/pkg"
78
"github.com/mitchellh/go-homedir"
89
"github.com/spf13/cobra"
910
"gopkg.in/yaml.v2"
1011
"io/ioutil"
1112
"net/url"
1213
"path"
14+
"path/filepath"
1315
"runtime"
1416
"strings"
1517
"text/template"
@@ -105,6 +107,28 @@ func (o *downloadOption) isSupport(cfg hdConfig) bool {
105107
return osSupport && archSupport
106108
}
107109

110+
func findByRepo(repo string) (result []string) {
111+
userHome, _ := homedir.Dir()
112+
configDir := userHome + "/.config/hd-home"
113+
matchedFile := configDir + "/config/*/" + repo + ".yml"
114+
115+
if files, err := filepath.Glob(matchedFile); err == nil {
116+
for _, metaFile := range files {
117+
result = append(result, filepath.Base(filepath.Dir(metaFile)))
118+
}
119+
}
120+
return
121+
}
122+
123+
func chooseOneFromArray(options []string) (result string, err error) {
124+
prompt := &survey.Select{
125+
Message: "Please select:",
126+
Options: options,
127+
}
128+
err = survey.AskOne(prompt, &result)
129+
return
130+
}
131+
108132
func (o *downloadOption) providerURLParse(path string) (url string, err error) {
109133
url = path
110134
if o.Provider != ProviderGitHub {
@@ -123,15 +147,29 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
123147
org = addr[0]
124148
repo = addr[1]
125149
name = repo
150+
} else if len(addr) > 0 {
151+
repo = addr[0]
152+
153+
if potentialOrgs := findByRepo(repo); len(potentialOrgs) == 0 {
154+
err = fmt.Errorf("cannot found the package: %s", repo)
155+
return
156+
} else if len(potentialOrgs) == 1 {
157+
org = potentialOrgs[0]
158+
} else {
159+
if org, err = chooseOneFromArray(potentialOrgs); err != nil {
160+
err = fmt.Errorf("failed to choose the potential organizations of your desired package")
161+
return
162+
}
163+
}
164+
165+
fmt.Printf("prepare to download %s/%s\n", org, repo)
126166
} else {
127-
err = fmt.Errorf("only support format xx/xx or xx/xx/xx")
167+
err = fmt.Errorf("only support format xx, xx/xx or xx/xx/xx")
128168
return
129169
}
130170

131171
if len(addr) == 3 {
132172
name = addr[2]
133-
} else if len(addr) > 3 {
134-
err = fmt.Errorf("only support format xx/xx or xx/xx/xx")
135173
}
136174

137175
// extract version from name
@@ -142,7 +180,7 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
142180

143181
url = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s-%s-%s.tar.gz",
144182
org, repo, version, name, o.OS, o.Arch)
145-
} else {
183+
} else if name != "" {
146184
version = "latest"
147185
url = fmt.Sprintf("https://github.com/%s/%s/releases/%s/download/%s-%s-%s.tar.gz",
148186
org, repo, version, name, o.OS, o.Arch)
@@ -163,6 +201,10 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
163201
}
164202

165203
if err = yaml.Unmarshal(data, &cfg); err == nil {
204+
if cfg.Name != "" {
205+
o.name = cfg.Name
206+
}
207+
166208
hdPkg := &hdPackage{
167209
Name: o.name,
168210
Version: version,
@@ -184,6 +226,16 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
184226
} else {
185227
fmt.Println(err, "cannot get the asset")
186228
}
229+
230+
if url == "" {
231+
url = fmt.Sprintf("https://github.com/%s/%s/releases/%s/download/%s-%s-%s.tar.gz",
232+
org, repo, version, o.name, o.OS, o.Arch)
233+
}
234+
} else {
235+
if url == "" {
236+
url = fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s-%s-%s.tar.gz",
237+
org, repo, version, name, o.OS, o.Arch)
238+
}
187239
}
188240

189241
if cfg.URL != "" {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/linuxsuren/http-downloader
33
go 1.15
44

55
require (
6+
github.com/AlecAivazis/survey/v2 v2.2.2
67
github.com/golang/mock v1.5.0
78
github.com/google/go-github/v29 v29.0.3
89
github.com/gosuri/uiprogress v0.0.1

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl
1111
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
1212
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
1313
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
14+
github.com/AlecAivazis/survey/v2 v2.2.2 h1:1I4qBrNsHQE+91tQCqVlfrKe9DEL65949d1oKZWVELY=
1415
github.com/AlecAivazis/survey/v2 v2.2.2/go.mod h1:9FJRdMdDm8rnT+zHVbvQT2RTSTLq0Ttd6q3Vl2fahjk=
1516
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
1617
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
@@ -146,6 +147,7 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
146147
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
147148
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
148149
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
150+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
149151
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
150152
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
151153
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
@@ -170,12 +172,14 @@ github.com/magiconair/properties v1.8.4 h1:8KGKTcQQGm0Kv7vEbKFErAoAOFyyacLStRtQS
170172
github.com/magiconair/properties v1.8.4/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
171173
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
172174
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
175+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
173176
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
174177
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
175178
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
176179
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
177180
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
178181
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
182+
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
179183
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
180184
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
181185
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=

pkg/installer.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package pkg
2+
3+
//
4+
//import (
5+
// "archive/tar"
6+
// "compress/gzip"
7+
// "fmt"
8+
// "github.com/spf13/cobra"
9+
// "io"
10+
// "os"
11+
// "os/exec"
12+
// "path/filepath"
13+
// "strings"
14+
// "syscall"
15+
//)
16+
//
17+
//type Installer struct {
18+
//}
19+
//
20+
//func (o *Installer) Download(cmd *cobra.Command, args []string) (err error) {
21+
// if o.Download {
22+
// if err = o.downloadOption.runE(cmd, args); err != nil {
23+
// return
24+
// }
25+
// }
26+
//
27+
// targetBinary := o.name
28+
// if o.Package != nil && o.Package.TargetBinary != "" {
29+
// // this is the desired binary file
30+
// targetBinary = o.Package.TargetBinary
31+
// }
32+
//
33+
// var source string
34+
// var target string
35+
// tarFile := o.Output
36+
// if o.Tar {
37+
// if err = o.extractFiles(tarFile, o.name); err == nil {
38+
// source = fmt.Sprintf("%s/%s", filepath.Dir(tarFile), o.name)
39+
// target = fmt.Sprintf("/usr/local/bin/%s", targetBinary)
40+
// } else {
41+
// err = fmt.Errorf("cannot extract %s from tar file, error: %v", tarFile, err)
42+
// }
43+
// } else {
44+
// source = o.downloadOption.Output
45+
// target = fmt.Sprintf("/usr/local/bin/%s", targetBinary)
46+
// }
47+
//
48+
// if err == nil {
49+
// if o.Package != nil && o.Package.PreInstall != nil {
50+
// if err = execCommand(o.Package.PreInstall.Cmd, o.Package.PreInstall.Args...); err != nil {
51+
// return
52+
// }
53+
// }
54+
//
55+
// if o.Package != nil && o.Package.Installation != nil {
56+
// err = execCommand(o.Package.Installation.Cmd, o.Package.Installation.Args...)
57+
// } else {
58+
// err = o.overWriteBinary(source, target)
59+
// }
60+
//
61+
// if err == nil && o.Package != nil && o.Package.PostInstall != nil {
62+
// err = execCommand(o.Package.PostInstall.Cmd, o.Package.PostInstall.Args...)
63+
// }
64+
//
65+
// if err == nil && o.Package != nil && o.Package.TestInstall != nil {
66+
// err = execCommand(o.Package.TestInstall.Cmd, o.Package.TestInstall.Args...)
67+
// }
68+
//
69+
// if err == nil && o.CleanPackage {
70+
// if cleanErr := os.RemoveAll(tarFile); cleanErr != nil {
71+
// cmd.Println("cannot remove file", tarFile, ", error:", cleanErr)
72+
// }
73+
// }
74+
// }
75+
// return
76+
//}
77+
//
78+
//func (o *Installer) overWriteBinary(sourceFile, targetPath string) (err error) {
79+
// fmt.Println("install", sourceFile, "to", targetPath)
80+
// switch runtime.GOOS {
81+
// case "linux", "darwin":
82+
// if err = execCommand("chmod", "u+x", sourceFile); err != nil {
83+
// return
84+
// }
85+
//
86+
// if err = execCommand("rm", "-rf", targetPath); err != nil {
87+
// return
88+
// }
89+
//
90+
// var cp string
91+
// if cp, err = exec.LookPath("mv"); err == nil {
92+
// err = syscall.Exec(cp, []string{"mv", sourceFile, targetPath}, os.Environ())
93+
// }
94+
// default:
95+
// sourceF, _ := os.Open(sourceFile)
96+
// targetF, _ := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, 0600)
97+
// if _, err = io.Copy(targetF, sourceF); err != nil {
98+
// err = fmt.Errorf("cannot copy %s from %s to %v, error: %v", o.name, sourceFile, targetPath, err)
99+
// }
100+
//
101+
// if err == nil {
102+
// _ = os.RemoveAll(sourceFile)
103+
// }
104+
// }
105+
// return
106+
//}
107+
//
108+
//func (o *Installer) extractFiles(tarFile, targetName string) (err error) {
109+
// var f *os.File
110+
// var gzf *gzip.Reader
111+
// if f, err = os.Open(tarFile); err != nil {
112+
// return
113+
// }
114+
// defer func() {
115+
// _ = f.Close()
116+
// }()
117+
//
118+
// if gzf, err = gzip.NewReader(f); err != nil {
119+
// return
120+
// }
121+
//
122+
// tarReader := tar.NewReader(gzf)
123+
// var header *tar.Header
124+
// var found bool
125+
// for {
126+
// if header, err = tarReader.Next(); err == io.EOF {
127+
// err = nil
128+
// break
129+
// } else if err != nil {
130+
// break
131+
// }
132+
// name := header.Name
133+
//
134+
// switch header.Typeflag {
135+
// case tar.TypeReg:
136+
// if name != targetName && !strings.HasSuffix(name, "/"+targetName) {
137+
// continue
138+
// }
139+
// var targetFile *os.File
140+
// if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(tarFile), targetName),
141+
// os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)); err != nil {
142+
// break
143+
// }
144+
// if _, err = io.Copy(targetFile, tarReader); err != nil {
145+
// break
146+
// }
147+
// found = true
148+
// _ = targetFile.Close()
149+
// }
150+
// }
151+
//
152+
// if err == nil && !found {
153+
// err = fmt.Errorf("cannot found item '%s' from '%s'", targetName, tarFile)
154+
// }
155+
// return
156+
//}

0 commit comments

Comments
 (0)