From 0571f646e1ae98dc4272786fcf93ee5df72e05f0 Mon Sep 17 00:00:00 2001 From: Will Roden Date: Tue, 9 May 2023 16:24:14 -0500 Subject: [PATCH] Add homepage and description fields --- bindown.go | 2 ++ bindown.schema.json | 8 ++++++ bindown.schema.yml | 6 +++++ bindown.yml | 4 +++ cmd/bindown/dependency.go | 18 ++++++++++--- internal/bindown/bindown.schema.json | 8 ++++++ internal/bindown/dependency.go | 4 +++ internal/builddep/builddep.go | 40 ++++++++++++++++++++++------ internal/builddep/builddep_test.go | 4 +-- internalize.go | 4 +++ 10 files changed, 85 insertions(+), 13 deletions(-) diff --git a/bindown.go b/bindown.go index 42be07f2..c83dc042 100644 --- a/bindown.go +++ b/bindown.go @@ -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"` diff --git a/bindown.schema.json b/bindown.schema.json index 7e17f597..5d990a20 100644 --- a/bindown.schema.json +++ b/bindown.schema.json @@ -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" diff --git a/bindown.schema.yml b/bindown.schema.yml index 2622c11e..e82e990b 100644 --- a/bindown.schema.yml +++ b/bindown.schema.yml @@ -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. diff --git a/bindown.yml b/bindown.yml index 5eed1893..f9de01fa 100644 --- a/bindown.yml +++ b/bindown.yml @@ -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 diff --git a/cmd/bindown/dependency.go b/cmd/bindown/dependency.go index d57c8b11..e6716319 100644 --- a/cmd/bindown/dependency.go +++ b/cmd/bindown/dependency.go @@ -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'"` @@ -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 } @@ -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'"` @@ -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 } @@ -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 } diff --git a/internal/bindown/bindown.schema.json b/internal/bindown/bindown.schema.json index 7e17f597..5d990a20 100644 --- a/internal/bindown/bindown.schema.json +++ b/internal/bindown/bindown.schema.json @@ -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" diff --git a/internal/bindown/dependency.go b/internal/bindown/dependency.go index fc869dab..1b0e088b 100644 --- a/internal/bindown/dependency.go +++ b/internal/bindown/dependency.go @@ -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"` @@ -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), diff --git a/internal/builddep/builddep.go b/internal/builddep/builddep.go index 0b30b763..d96658f0 100644 --- a/internal/builddep/builddep.go +++ b/internal/builddep/builddep.go @@ -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 { @@ -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) } @@ -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") { @@ -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 } diff --git a/internal/builddep/builddep_test.go b/internal/builddep/builddep_test.go index 595c295b..a0b81ab5 100644 --- a/internal/builddep/builddep_test.go +++ b/internal/builddep/builddep_test.go @@ -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)) @@ -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) diff --git a/internalize.go b/internalize.go index d887c635..b802b350 100644 --- a/internalize.go +++ b/internalize.go @@ -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, @@ -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,