Skip to content

Commit

Permalink
add github release config adder
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Sep 14, 2023
1 parent e6db284 commit 0d95583
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
5 changes: 4 additions & 1 deletion cmd/binny/cli/command/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ func Add(app clio.Application) *cobra.Command {
Short: "Add a new tool to the configuration",
})

cmd.AddCommand(AddGoInstall(app))
cmd.AddCommand(
AddGoInstall(app),
AddGithubRelease(app),
)

return cmd
}
100 changes: 100 additions & 0 deletions cmd/binny/cli/command/add_github_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package command

import (
"fmt"
"strings"

"github.com/scylladb/go-set/strset"
"github.com/spf13/cobra"

"github.com/anchore/binny/cmd/binny/cli/internal/yamlpatch"
"github.com/anchore/binny/cmd/binny/cli/option"
"github.com/anchore/binny/internal/log"
"github.com/anchore/binny/tool/githubrelease"
"github.com/anchore/clio"
)

type AddGithubReleaseConfig struct {
Config string `json:"config" yaml:"config" mapstructure:"config"`
option.Core `json:"" yaml:",inline" mapstructure:",squash"`

VersionResolution option.VersionResolution `json:"version-resolver" yaml:"version-resolver" mapstructure:"version-resolver"`
}

func AddGithubRelease(app clio.Application) *cobra.Command {
cfg := &AddGithubReleaseConfig{
Core: option.DefaultCore(),
}

return app.SetupCommand(&cobra.Command{
Use: "github-release OWNER/REPO@VERSION",
Short: "Add a new tool configuration that sources binaries from GitHub releases",
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if !strings.Contains(args[0], "/") {
return fmt.Errorf("invalid 'owner/project@version' format: %q", args[0])
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runGithubReleaseConfig(*cfg, args[0])
},
}, cfg)
}

func runGithubReleaseConfig(cmdCfg AddGithubReleaseConfig, repoVersion string) error {
fields := strings.Split(repoVersion, "@")
var repo, name, version string

switch len(fields) {
case 1:
repo = repoVersion
version = "latest"
case 2:
repo = fields[0]
version = fields[1]
default:
return fmt.Errorf("invalid owner/project@version format: %s", repoVersion)
}

fields = strings.Split(repo, "/")
if len(fields) != 2 {
return fmt.Errorf("invalid owner/project format: %s", repo)
}

name = fields[1]

if strset.New(cmdCfg.Tools.Names()...).Has(name) {
// TODO: should this be an error?
log.Warnf("tool %q already configured", name)
return nil
}

vCfg := cmdCfg.VersionResolution

coreInstallParams := githubrelease.InstallerParameters{
Repo: repo,
}

installParamMap, err := toMap(coreInstallParams)
if err != nil {
return fmt.Errorf("unable to encode install params: %w", err)
}

installMethod := githubrelease.InstallMethod

log.WithFields("name", name, "version", version, "method", installMethod).Info("adding tool")

toolCfg := option.Tool{
Name: name,
Version: option.ToolVersionConfig{
Want: version,
Constraint: vCfg.Constraint,
ResolveMethod: vCfg.Method,
},
InstallMethod: installMethod,
Parameters: installParamMap,
}

return yamlpatch.Write(cmdCfg.Config, yamlToolAppender{toolCfg: toolCfg})
}
4 changes: 2 additions & 2 deletions cmd/binny/cli/command/add_go_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func AddGoInstall(app clio.Application) *cobra.Command {
}

return app.SetupCommand(&cobra.Command{
Use: "go-install NAME@VERSION",
Short: "Add a new application via 'go install ...'",
Use: "go-install NAME@VERSION --module GOMODULE [--entrypoint PATH] [--ldflags FLAGS]",
Short: "Add a new tool configuration from 'go install ...' invocations",
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if cfg.Install.GoInstall.Module == "" {
Expand Down

0 comments on commit 0d95583

Please sign in to comment.