Skip to content

Commit b9c0d1c

Browse files
authored
Merge pull request #28 from b4b4r07/feature/http-templates
2 parents 217c550 + 69d8907 commit b9c0d1c

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

docs/configuration/package/http.md

+36
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,42 @@ string | (required)
3939
4040
Specify a URL that a command or plugin you want to install are hosted.
4141
42+
In this field, you can use template variables:
43+
44+
=== "Case 1"
45+
46+
```yaml hl_lines="4 5 6 7"
47+
http:
48+
- name: gcping
49+
description: Like gcping.com but a command line tool
50+
url: 'https://storage.googleapis.com/gcping-release/{{ .Name }}_{{ .OS }}_{{ .Arch }}_latest'
51+
templates:
52+
replacements:
53+
darwin: darwin # can replace "darwin" as you like!
54+
command:
55+
link:
56+
- from: gcping_*
57+
to: gcping
58+
```
59+
60+
Key | Description
61+
---|---
62+
`.Name` | Same as `.name` (Package name)
63+
`.OS` | [GOOS](https://go.dev/doc/install/source#environment)[^1] (e.g. `darwin` etc)
64+
`.Arch` | [GOARCH](https://go.dev/doc/install/source#environment)[^1] (e.g. `amd64` etc)
65+
66+
[^1]: This can be overwritten by `templates.replacements`.
67+
68+
### templates.replacements
69+
70+
Type | Default
71+
---|---
72+
list | `[]`
73+
74+
In `.url` field, the template variables can be used. Also you can replace it with your own values. For more details, see also below page.
75+
76+
See [GitHub#release.asset.replacements](github.md#releaseassetreplacements)
77+
4278
### depends-on
4379

4480
See [GitHub#depends-on](github.md#depends-on) page. Same as that.

pkg/config/http.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"context"
5+
"fmt"
56
"io"
67
"io/ioutil"
78
"log"
@@ -10,8 +11,10 @@ import (
1011
"os"
1112
"path/filepath"
1213

14+
"github.com/b4b4r07/afx/pkg/data"
1315
"github.com/b4b4r07/afx/pkg/errors"
1416
"github.com/b4b4r07/afx/pkg/state"
17+
"github.com/b4b4r07/afx/pkg/templates"
1518
"github.com/h2non/filetype"
1619
"github.com/mholt/archiver"
1720
)
@@ -26,7 +29,12 @@ type HTTP struct {
2629
Plugin *Plugin `yaml:"plugin"`
2730
Command *Command `yaml:"command"`
2831

29-
DependsOn []string `yaml:"depends-on"`
32+
DependsOn []string `yaml:"depends-on"`
33+
Templates Templates `yaml:"templates"`
34+
}
35+
36+
type Templates struct {
37+
Replacements map[string]string `yaml:"replacements"`
3038
}
3139

3240
// Init is
@@ -55,6 +63,16 @@ func (c HTTP) call(ctx context.Context) error {
5563
}
5664
defer resp.Body.Close()
5765

66+
log.Printf("[DEBUG] response code: %d", resp.StatusCode)
67+
switch resp.StatusCode {
68+
case 200, 301, 302:
69+
// go
70+
case 404:
71+
return fmt.Errorf("%s: %d Not Found in %s", c.GetName(), resp.StatusCode, c.URL)
72+
default:
73+
return fmt.Errorf("%s: %d %s", c.GetName(), resp.StatusCode, http.StatusText(resp.StatusCode))
74+
}
75+
5876
os.MkdirAll(c.GetHome(), os.ModePerm)
5977
dest := filepath.Join(c.GetHome(), filepath.Base(c.URL))
6078
file, err := os.Create(dest)
@@ -84,6 +102,17 @@ func (c HTTP) Install(ctx context.Context, status chan<- Status) error {
84102
// Go installing step!
85103
}
86104

105+
url, err := templates.New(data.New(data.WithPackage(c))).
106+
Replace(c.Templates.Replacements).
107+
Apply(c.URL)
108+
if err != nil {
109+
return err
110+
}
111+
if url != c.URL {
112+
log.Printf("[DEBUG] %s: templating url %s", c.GetName(), url)
113+
c.URL = url
114+
}
115+
87116
ctx, cancel := context.WithCancel(ctx)
88117
defer cancel()
89118

0 commit comments

Comments
 (0)