Skip to content

Commit 6186ed5

Browse files
committed
Add replacement in the package config
1 parent 7a27747 commit 6186ed5

File tree

7 files changed

+94
-17
lines changed

7 files changed

+94
-17
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
build: fmt
1+
build: fmt test
22
export GOPROXY=https://goproxy.io
33
CGO_ENABLE=0 go build -ldflags "-w -s" -o bin/hd
44

5-
build-linux: fmt
5+
build-linux: fmt test
66
export GOPROXY=https://goproxy.io
77
CGO_ENABLE=0 GOOS=linux go build -ldflags "-w -s" -o bin/linux/hd
88
upx bin/linux/hd
99

10+
test:
11+
go test ./cmd/...
12+
1013
run:
1114
go run main.go
1215

cmd/get.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
121121

122122
if err = yaml.Unmarshal(data, &cfg); err == nil {
123123
hdPackage := &hdPackage{
124-
Name: o.name,
125-
Version: version,
126-
OS: runtime.GOOS,
127-
Arch: runtime.GOARCH,
124+
Name: o.name,
125+
Version: version,
126+
OS: getReplacement(runtime.GOOS, cfg.Replacements),
127+
Arch: getReplacement(runtime.GOARCH, cfg.Replacements),
128+
VersionNum: strings.TrimPrefix(version, "v"),
128129
}
130+
129131
if version == "latest" {
130132
ghClient := pkg.ReleaseClient{
131133
Org: org,
@@ -134,6 +136,7 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
134136
ghClient.Init()
135137
if asset, err := ghClient.GetLatestJCLIAsset(); err == nil {
136138
hdPackage.Version = asset.TagName
139+
hdPackage.VersionNum = strings.TrimPrefix(asset.TagName, "v")
137140
} else {
138141
fmt.Println(err, "cannot get the asset")
139142
}
@@ -148,10 +151,12 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
148151
org, repo, version, buf.String())
149152

150153
o.Output = buf.String()
154+
} else {
155+
return
151156
}
152157
}
153158

154-
o.Tar = cfg.Tar
159+
o.Tar = cfg.Tar != "false"
155160
if cfg.Binary != "" {
156161
o.name = cfg.Binary
157162
}
@@ -162,17 +167,19 @@ func (o *downloadOption) providerURLParse(path string) (url string, err error) {
162167
}
163168

164169
type hdConfig struct {
165-
Name string
166-
Filename string
167-
Binary string
168-
Tar bool
170+
Name string
171+
Filename string
172+
Binary string
173+
Tar string
174+
Replacements map[string]string
169175
}
170176

171177
type hdPackage struct {
172-
Name string
173-
Version string
174-
OS string
175-
Arch string
178+
Name string
179+
Version string // e.g. v1.0.1
180+
VersionNum string // e.g. 1.0.1
181+
OS string // e.g. linux, darwin
182+
Arch string // e.g. amd64
176183
}
177184

178185
func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error) {

cmd/install.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"runtime"
13+
"strings"
1314
"sync"
1415
"syscall"
1516
)
@@ -118,6 +119,7 @@ func (o *installOption) extractFiles(tarFile, targetName string) (err error) {
118119

119120
tarReader := tar.NewReader(gzf)
120121
var header *tar.Header
122+
var found bool
121123
for {
122124
if header, err = tarReader.Next(); err == io.EOF {
123125
err = nil
@@ -129,20 +131,26 @@ func (o *installOption) extractFiles(tarFile, targetName string) (err error) {
129131

130132
switch header.Typeflag {
131133
case tar.TypeReg:
132-
if name != targetName {
134+
if name != targetName && !strings.HasSuffix(name, "/"+targetName) {
133135
continue
134136
}
135137
var targetFile *os.File
136-
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(tarFile), name),
138+
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(tarFile), targetName),
137139
os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)); err != nil {
138140
break
139141
}
140142
if _, err = io.Copy(targetFile, tarReader); err != nil {
141143
break
142144
}
145+
fmt.Println("found ", targetName, header)
146+
found = true
143147
_ = targetFile.Close()
144148
}
145149
}
150+
151+
if err == nil && !found {
152+
err = fmt.Errorf("cannot found item '%s' from '%s'", targetName, tarFile)
153+
}
146154
return
147155
}
148156

cmd/util.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmd
2+
3+
func getOrDefault(key, def string, data map[string]string) (result string) {
4+
var ok bool
5+
if result, ok = data[key]; !ok {
6+
result = def
7+
}
8+
return
9+
}
10+
11+
func getReplacement(key string, data map[string]string) (result string) {
12+
return getOrDefault(key, key, data)
13+
}

cmd/util_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/magiconair/properties/assert"
6+
"testing"
7+
)
8+
9+
func TestGetOrDefault(t *testing.T) {
10+
table := []testCase{
11+
{key: "fake", def: "def", data: map[string]string{}, expected: "def"},
12+
{key: "fake", def: "def", data: nil, expected: "def"},
13+
{key: "fake", def: "def", data: map[string]string{
14+
"fake": "good",
15+
}, expected: "good"},
16+
}
17+
18+
for index, item := range table {
19+
result := getOrDefault(item.key, item.def, item.data)
20+
assert.Equal(t, result, item.expected, fmt.Sprintf("test failed with '%d'", index))
21+
}
22+
}
23+
24+
func TestGetReplacement(t *testing.T) {
25+
table := []testCase{
26+
{key: "fake", data: map[string]string{}, expected: "fake"},
27+
{key: "fake", data: nil, expected: "fake"},
28+
{key: "fake", data: map[string]string{
29+
"fake": "good",
30+
}, expected: "good"},
31+
}
32+
33+
for index, item := range table {
34+
result := getReplacement(item.key, item.data)
35+
assert.Equal(t, result, item.expected, fmt.Sprintf("test failed with '%d'", index))
36+
}
37+
}
38+
39+
type testCase struct {
40+
key string
41+
def string
42+
data map[string]string
43+
expected string
44+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/google/go-github/v29 v29.0.3
99
github.com/gosuri/uiprogress v0.0.1
1010
github.com/linuxsuren/cobra-extension v0.0.10
11+
github.com/magiconair/properties v1.8.1
1112
github.com/mitchellh/go-homedir v1.1.0
1213
github.com/onsi/ginkgo v1.14.2
1314
github.com/onsi/gomega v1.10.4

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ github.com/linuxsuren/cobra-extension v0.0.10/go.mod h1:nDsXgvm0lSWVV+byAEfwhIGF
168168
github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec/go.mod h1:zRZY9FCDBuYNDxbI2Ny5suasZsMk7J6q9ecQ3V3PIqI=
169169
github.com/linuxsuren/http-downloader v0.0.6/go.mod h1:xxgh2OE7WGL9TwDE9L8Gh7Lqq9fFPuHbh5tofUitEfE=
170170
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
171+
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
171172
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
172173
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
173174
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=

0 commit comments

Comments
 (0)