Skip to content

Commit

Permalink
Merge pull request #139 from WillAbides/homepage
Browse files Browse the repository at this point in the history
Add homepage and description fields
  • Loading branch information
WillAbides authored May 9, 2023
2 parents 2e08cce + 0571f64 commit 07ccf8e
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 13 deletions.
2 changes: 2 additions & 0 deletions bindown.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ type OverrideMatcher map[string][]string

// Dependency is something to download, extract and install
type Dependency struct {
Homepage *string `json:"homepage,omitempty" yaml:",omitempty"`
Description *string `json:"description,omitempty" yaml:",omitempty"`
Template *string `json:"template,omitempty" yaml:",omitempty"`
URL *string `json:"url,omitempty" yaml:",omitempty"`
ArchivePath *string `json:"archive_path,omitempty" yaml:"archive_path,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions bindown.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"description": "The name of the binary to be installed. Default is the name of the dependency.",
"type": "string"
},
"description": {
"description": "A description of this dependency",
"type": "string"
},
"homepage": {
"description": "The homepage for this dependency",
"type": "string"
},
"link": {
"description": "Whether to create a symlink to the bin instead of copying it.",
"type": "boolean"
Expand Down
6 changes: 6 additions & 0 deletions bindown.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ $defs:
dependency:
type: object
properties:
homepage:
description: The homepage for this dependency
type: string
description:
description: A description of this dependency
type: string
template:
description: >
A template for this dependency. Any unset fields in this dependency will be set by values from the template.
Expand Down
4 changes: 4 additions & 0 deletions bindown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ url_checksums:
https://github.com/goreleaser/goreleaser/releases/download/v1.16.1/goreleaser_Darwin_x86_64.tar.gz: e709a44d112ceab37e017f3be6d436cfaaab0dbcf841381544c681408127761b
https://github.com/goreleaser/goreleaser/releases/download/v1.16.1/goreleaser_Linux_x86_64.tar.gz: fa370201538b2a93d960ca620cb3e26e25adba5abd115bb91f3517086f2324b7
https://github.com/goreleaser/goreleaser/releases/download/v1.16.1/goreleaser_Windows_x86_64.zip: 1b5c2a3d3a507e909c6fdf8d2061590a0933d695cce9ba37467f7a3b283a83f4
https://github.com/junegunn/fzf/releases/download/0.40.0/fzf-0.40.0-darwin_amd64.zip: cbd4b9c577295c54f8326088aeec162c9522af2338addc8d28fd0b5c31c8a419
https://github.com/junegunn/fzf/releases/download/0.40.0/fzf-0.40.0-darwin_arm64.zip: 6067881dbf70df8030e606310928a7d37fa1b7a94c72fceeacf3e53368f03952
https://github.com/junegunn/fzf/releases/download/0.40.0/fzf-0.40.0-linux_amd64.tar.gz: ec334c17ba437d280f4a690177cd65d66aca3e17d0e6c9499c1f4a1cfaa8da3a
https://github.com/junegunn/fzf/releases/download/0.40.0/fzf-0.40.0-windows_amd64.zip: 0f3ee8fee46f2be51aa81317c67994299e4d3f349fa6085daf295e7cac720d2c
https://github.com/koalaman/shellcheck/releases/download/v0.9.0/shellcheck-v0.9.0.darwin.x86_64.tar.xz: 7d3730694707605d6e60cec4efcb79a0632d61babc035aa16cda1b897536acf5
https://github.com/koalaman/shellcheck/releases/download/v0.9.0/shellcheck-v0.9.0.linux.x86_64.tar.xz: 700324c6dd0ebea0117591c6cc9d7350d9c7c5c287acbad7630fa17b1d4d9e2f
https://github.com/koalaman/shellcheck/releases/download/v0.9.0/shellcheck-v0.9.0.zip: ae58191b1ea4ffd9e5b15da9134146e636440302ce3e2f46863e8d71c8be1bbb
Expand Down
18 changes: 15 additions & 3 deletions cmd/bindown/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func (c *dependencyAddCmd) Run(ctx *runContext) error {
type dependencyAddByUrlsCmd struct {
Name string `kong:"arg,help='dependency name'"`
Version string `kong:"arg,help='dependency version'"`
Homepage string `kong:"name=homepage,help='dependency homepage'"`
Description string `kong:"name=description,help='dependency description'"`
URL []string `kong:"arg,help='dependency URL'"`
Force bool `kong:"name=force,help='overwrite existing dependency'"`
Experimental bool `kong:"required,name=experimental,help='enable experimental features',env='BINDOWN_EXPERIMENTAL'"`
Expand All @@ -233,7 +235,7 @@ func (c *dependencyAddByUrlsCmd) Run(ctx *runContext) error {
if config.Dependencies != nil && config.Dependencies[c.Name] != nil && !c.Force {
return fmt.Errorf("dependency %q already exists", c.Name)
}
err = builddep.AddDependency(ctx, &config.Config, c.Name, c.Version, c.URL)
err = builddep.AddDependency(ctx, &config.Config, c.Name, c.Version, c.Homepage, c.Description, c.URL)
if err != nil {
return err
}
Expand All @@ -244,6 +246,8 @@ type dependencyAddByGithubReleaseCmd struct {
Release string `kong:"arg,help='github release URL or \"owner/repo(@tag)\"'"`
Name string `kong:"name to use instead of repo name"`
Version string `kong:"version to use instead of release tag"`
Homepage string `kong:"name=homepage,help='dependency homepage'"`
Description string `kong:"name=description,help='dependency description'"`
Force bool `kong:"name=force,help='overwrite existing dependency'"`
Experimental bool `kong:"required,name=experimental,help='enable experimental features',env='BINDOWN_EXPERIMENTAL'"`
GithubToken string `kong:"hidden,env='GITHUB_TOKEN'"`
Expand All @@ -270,7 +274,7 @@ func (c *dependencyAddByGithubReleaseCmd) Run(ctx *runContext) error {
default:
return fmt.Errorf(`invalid release URL or "owner/repo(@tag)"`)
}
urls, releaseVer, err := builddep.QueryGitHubRelease(ctx, fmt.Sprintf("%s/%s", owner, repo), tag, c.GithubToken)
urls, releaseVer, repoDesc, err := builddep.QueryGitHubRelease(ctx, fmt.Sprintf("%s/%s", owner, repo), tag, c.GithubToken)
if err != nil {
return err
}
Expand All @@ -282,10 +286,18 @@ func (c *dependencyAddByGithubReleaseCmd) Run(ctx *runContext) error {
if name == "" {
name = repo
}
homepage := c.Homepage
if homepage == "" {
homepage = fmt.Sprintf("https://github.com/%s/%s", owner, repo)
}
description := c.Description
if description == "" {
description = repoDesc
}
if config.Dependencies != nil && config.Dependencies[name] != nil && !c.Force {
return fmt.Errorf("dependency %q already exists", name)
}
err = builddep.AddDependency(ctx, &config.Config, name, ver, urls)
err = builddep.AddDependency(ctx, &config.Config, name, ver, homepage, description, urls)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions internal/bindown/bindown.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
"description": "The name of the binary to be installed. Default is the name of the dependency.",
"type": "string"
},
"description": {
"description": "A description of this dependency",
"type": "string"
},
"homepage": {
"description": "The homepage for this dependency",
"type": "string"
},
"link": {
"description": "Whether to create a symlink to the bin instead of copying it.",
"type": "boolean"
Expand Down
4 changes: 4 additions & 0 deletions internal/bindown/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ type builtDependency struct {

// Dependency is something to download, extract and install
type Dependency struct {
Homepage *string `json:"homepage,omitempty" yaml:",omitempty"`
Description *string `json:"description,omitempty" yaml:",omitempty"`
Template *string `json:"template,omitempty" yaml:",omitempty"`
URL *string `json:"url,omitempty" yaml:",omitempty"`
ArchivePath *string `json:"archive_path,omitempty" yaml:"archive_path,omitempty"`
Expand Down Expand Up @@ -121,6 +123,8 @@ func varsWithSubstitutions(vars map[string]string, subs map[string]map[string]st

func (d *Dependency) Clone() *Dependency {
dep := Dependency{
Homepage: clonePointer(d.Homepage),
Description: clonePointer(d.Description),
Vars: maps.Clone(d.Vars),
URL: clonePointer(d.URL),
ArchivePath: clonePointer(d.ArchivePath),
Expand Down
40 changes: 32 additions & 8 deletions internal/builddep/builddep.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,24 @@ func distSystems() []bindown.SystemInfo {
return dists
}

func AddDependency(ctx context.Context, cfg *bindown.Config, name, version string, urls []string) error {
return addDependency(ctx, cfg, name, version, urls, nil)
func AddDependency(
ctx context.Context,
cfg *bindown.Config,
name, version string,
homepage, description string,
urls []string,
) error {
return addDependency(ctx, cfg, name, version, homepage, description, urls, nil)
}

func addDependency(ctx context.Context, cfg *bindown.Config, name, version string, urls []string, selector selectCandidateFunc) error {
func addDependency(
ctx context.Context,
cfg *bindown.Config,
name, version string,
homepage, description string,
urls []string,
selector selectCandidateFunc,
) error {
groups := parseDownloads(urls, name, version, cfg.Systems)
var regrouped []*depGroup
for _, g := range groups {
Expand Down Expand Up @@ -90,6 +103,12 @@ func addDependency(ctx context.Context, cfg *bindown.Config, name, version strin
cfg.Dependencies[k] = v
}
for k, v := range built.Templates {
if homepage != "" {
v.Homepage = &homepage
}
if description != "" {
v.Description = &description
}
if cfg.Templates == nil {
cfg.Templates = make(map[string]*bindown.Dependency)
}
Expand Down Expand Up @@ -1022,26 +1041,31 @@ func (g *depGroup) fileAllowed(f *dlFile, binName string) bool {
return true
}

func QueryGitHubRelease(ctx context.Context, repo, tag, tkn string) (urls []string, version string, _ error) {
func QueryGitHubRelease(ctx context.Context, repo, tag, tkn string) (urls []string, version, description string, _ error) {
client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: tkn},
)))
splitRepo := strings.Split(repo, "/")
orgName, repoName := splitRepo[0], splitRepo[1]
repoResp, _, err := client.Repositories.Get(ctx, orgName, repoName)
if err != nil {
return nil, "", "", err
}
description = repoResp.GetDescription()
var release *github.RepositoryRelease
var err error
if tag == "" {
release, _, err = client.Repositories.GetLatestRelease(ctx, orgName, repoName)
if err != nil {
return nil, "", err
return nil, "", "", err
}
tag = release.GetTagName()
} else {
release, _, err = client.Repositories.GetReleaseByTag(ctx, orgName, repoName, tag)
if err != nil {
return nil, "", err
return nil, "", "", err
}
}

if version == "" {
version = tag
if strings.HasPrefix(version, "v") {
Expand All @@ -1054,5 +1078,5 @@ func QueryGitHubRelease(ctx context.Context, repo, tag, tkn string) (urls []stri
for _, asset := range release.Assets {
urls = append(urls, asset.GetBrowserDownloadURL())
}
return urls, version, nil
return urls, version, description, nil
}
4 changes: 2 additions & 2 deletions internal/builddep/builddep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_sanity(t *testing.T) {
if tkn == "" {
t.Skip("GITHUB_TOKEN not set")
}
urls, version, err := QueryGitHubRelease(ctx, "willabides/bindown", "v3.16.1", tkn)
urls, version, description, err := QueryGitHubRelease(ctx, "willabides/bindown", "v3.16.1", tkn)
require.NoError(t, err)
require.Equal(t, "3.16.1", version)
require.Equal(t, 18, len(urls))
Expand All @@ -40,7 +40,7 @@ systems:
*candidate = *candidates[0]
return nil
}
err = addDependency(ctx, &cfg, "bindown", "3.16.1", urls, selectCandidate)
err = addDependency(ctx, &cfg, "bindown", "3.16.1", "", description, urls, selectCandidate)
require.NoError(t, err)
got, err := yaml.Marshal(&cfg)
require.NoError(t, err)
Expand Down
4 changes: 4 additions & 0 deletions internalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ func internalizeDependency(d *Dependency) *bindown.Dependency {
return nil
}
dep := &bindown.Dependency{
Homepage: d.Homepage,
Description: d.Description,
Template: d.Template,
URL: d.URL,
ArchivePath: d.ArchivePath,
Expand All @@ -30,6 +32,8 @@ func externalizeDependency(iDep *bindown.Dependency) *Dependency {
}
iDep = iDep.Clone()
return &Dependency{
Homepage: iDep.Homepage,
Description: iDep.Description,
Template: iDep.Template,
URL: iDep.URL,
ArchivePath: iDep.ArchivePath,
Expand Down

0 comments on commit 07ccf8e

Please sign in to comment.