From af4a93e86f794a4f6385b18a603d0bab704d1767 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Thu, 5 Oct 2023 13:03:10 -0700 Subject: [PATCH 1/2] [runx] Add support for github token, implement resolve --- pkg/sandbox/runx/cmd/pkg/cli/releases.go | 4 ++- pkg/sandbox/runx/cmd/pkg/cli/resolve.go | 38 ++++++++++++++++++++++ pkg/sandbox/runx/cmd/pkg/cli/root.go | 1 + pkg/sandbox/runx/cmd/runx/cli/root.go | 9 +++-- pkg/sandbox/runx/impl/github/github.go | 12 +++++-- pkg/sandbox/runx/impl/install.go | 21 ++++-------- pkg/sandbox/runx/impl/registry/registry.go | 28 +++++++++++----- pkg/sandbox/runx/impl/run.go | 9 ++--- pkg/sandbox/runx/impl/runopt/runopt.go | 5 +++ pkg/sandbox/runx/impl/runx.go | 13 ++++++++ pkg/sandbox/runx/runx.go | 16 +++++---- 11 files changed, 117 insertions(+), 39 deletions(-) create mode 100644 pkg/sandbox/runx/cmd/pkg/cli/resolve.go create mode 100644 pkg/sandbox/runx/impl/runopt/runopt.go create mode 100644 pkg/sandbox/runx/impl/runx.go diff --git a/pkg/sandbox/runx/cmd/pkg/cli/releases.go b/pkg/sandbox/runx/cmd/pkg/cli/releases.go index 2db1deb0..0c28d57d 100644 --- a/pkg/sandbox/runx/cmd/pkg/cli/releases.go +++ b/pkg/sandbox/runx/cmd/pkg/cli/releases.go @@ -4,6 +4,8 @@ package cli // Might be moot, this CLI is mostly for testing; we'll integrate w/ devbox instead. import ( + "os" + "github.com/k0kubun/pp" "github.com/spf13/cobra" "go.jetpack.io/pkg/sandbox/runx/impl/github" @@ -26,7 +28,7 @@ func releasesCmd(cmd *cobra.Command, args []string) error { return error } - gh := github.NewClient() + gh := github.NewClient(cmd.Context(), os.Getenv("RUNX_GITHUB_API_TOKEN")) releases, err := gh.ListReleases(cmd.Context(), ref.Owner, ref.Repo) if err != nil { return err diff --git a/pkg/sandbox/runx/cmd/pkg/cli/resolve.go b/pkg/sandbox/runx/cmd/pkg/cli/resolve.go new file mode 100644 index 00000000..cfe00a48 --- /dev/null +++ b/pkg/sandbox/runx/cmd/pkg/cli/resolve.go @@ -0,0 +1,38 @@ +package cli + +// TODO: should it be `pkg show` (like poetry) or `pkg info` (like yarn)? +// Might be moot, this CLI is mostly for testing; we'll integrate w/ devbox instead. + +import ( + "os" + + "github.com/k0kubun/pp" + "github.com/spf13/cobra" + "go.jetpack.io/pkg/sandbox/runx/impl/registry" + "go.jetpack.io/pkg/sandbox/runx/impl/types" +) + +func ResolveCmd() *cobra.Command { + command := &cobra.Command{ + Use: "resolve /@", + Args: cobra.ExactArgs(1), + RunE: resolveCmd, + } + + return command +} + +func resolveCmd(cmd *cobra.Command, args []string) error { + ref, error := types.NewPkgRef(args[0]) + if error != nil { + return error + } + + registry, err := registry.NewLocalRegistry(cmd.Context(), os.Getenv("RUNX_GITHUB_API_TOKEN")) + if err != nil { + return err + } + + pp.Println(registry.ResolveVersion(ref)) + return nil +} diff --git a/pkg/sandbox/runx/cmd/pkg/cli/root.go b/pkg/sandbox/runx/cmd/pkg/cli/root.go index a26c860d..d974bca9 100644 --- a/pkg/sandbox/runx/cmd/pkg/cli/root.go +++ b/pkg/sandbox/runx/cmd/pkg/cli/root.go @@ -20,6 +20,7 @@ func RootCmd() *cobra.Command { } command.AddCommand(ReleasesCmd()) + command.AddCommand(ResolveCmd()) return command } diff --git a/pkg/sandbox/runx/cmd/runx/cli/root.go b/pkg/sandbox/runx/cmd/runx/cli/root.go index 4206a225..f2b18877 100644 --- a/pkg/sandbox/runx/cmd/runx/cli/root.go +++ b/pkg/sandbox/runx/cmd/runx/cli/root.go @@ -8,6 +8,7 @@ import ( "github.com/fatih/color" "go.jetpack.io/pkg/sandbox/runx" + "go.jetpack.io/pkg/sandbox/runx/impl/runopt" ) func Help() { @@ -29,8 +30,12 @@ func Execute(ctx context.Context, args []string) int { install := flag.Bool("install", false, "install packages only") flag.Parse() + runx := runx.New(runopt.Opts{ + GithubAPIToken: os.Getenv("RUNX_GITHUB_API_TOKEN"), + }) + if *install { - paths, err := runx.Install(args[1:]...) + paths, err := runx.Install(ctx, args[1:]...) if err != nil { fmt.Fprintf(os.Stderr, "[ERROR] %s\n", err) return 1 @@ -40,7 +45,7 @@ func Execute(ctx context.Context, args []string) int { fmt.Printf(" %s\n", path) } } else { - if err := runx.Run(args...); err != nil { + if err := runx.Run(ctx, args...); err != nil { fmt.Fprintf(os.Stderr, "[ERROR] %s\n", err) return 1 } diff --git a/pkg/sandbox/runx/impl/github/github.go b/pkg/sandbox/runx/impl/github/github.go index c556a651..fe17906d 100644 --- a/pkg/sandbox/runx/impl/github/github.go +++ b/pkg/sandbox/runx/impl/github/github.go @@ -7,15 +7,23 @@ import ( githubimpl "github.com/google/go-github/v53/github" "go.jetpack.io/pkg/sandbox/runx/impl/httpcacher" "go.jetpack.io/pkg/sandbox/runx/impl/types" + "golang.org/x/oauth2" ) type Client struct { gh *githubimpl.Client } -func NewClient() *Client { +func NewClient(ctx context.Context, accessToken string) *Client { + tc := httpcacher.DefaultClient + if accessToken != "" { + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: accessToken}, + ) + tc = oauth2.NewClient(ctx, ts) + } return &Client{ - gh: githubimpl.NewClient(httpcacher.DefaultClient), + gh: githubimpl.NewClient(tc), } } diff --git a/pkg/sandbox/runx/impl/install.go b/pkg/sandbox/runx/impl/install.go index 01219dbf..526687ae 100644 --- a/pkg/sandbox/runx/impl/install.go +++ b/pkg/sandbox/runx/impl/install.go @@ -2,16 +2,12 @@ package impl import ( "context" - "os" - "path/filepath" "go.jetpack.io/pkg/sandbox/runx/impl/registry" "go.jetpack.io/pkg/sandbox/runx/impl/types" ) -var xdgInstallationSubdir = "jetpack.io/pkgs" - -func Install(pkgs ...string) ([]string, error) { +func (r *RunX) Install(ctx context.Context, pkgs ...string) ([]string, error) { refs := []types.PkgRef{} for _, pkg := range pkgs { @@ -22,13 +18,13 @@ func Install(pkgs ...string) ([]string, error) { refs = append(refs, ref) } - return install(refs...) + return r.install(ctx, refs...) } -func install(pkgs ...types.PkgRef) ([]string, error) { +func (r *RunX) install(ctx context.Context, pkgs ...types.PkgRef) ([]string, error) { paths := []string{} for _, pkg := range pkgs { - path, err := installOne(pkg) + path, err := r.installOne(ctx, pkg) if err != nil { return nil, err } @@ -37,13 +33,8 @@ func install(pkgs ...types.PkgRef) ([]string, error) { return paths, nil } -func installOne(ref types.PkgRef) (string, error) { - cacheDir, err := os.UserCacheDir() - if err != nil { - cacheDir = "~/.cache" - } - rootDir := filepath.Join(cacheDir, xdgInstallationSubdir) - reg, err := registry.NewLocalRegistry(rootDir) +func (r *RunX) installOne(ctx context.Context, ref types.PkgRef) (string, error) { + reg, err := registry.NewLocalRegistry(ctx, r.GithubAPIToken) if err != nil { return "", err } diff --git a/pkg/sandbox/runx/impl/registry/registry.go b/pkg/sandbox/runx/impl/registry/registry.go index 70155eeb..d7aafadc 100644 --- a/pkg/sandbox/runx/impl/registry/registry.go +++ b/pkg/sandbox/runx/impl/registry/registry.go @@ -2,6 +2,8 @@ package registry import ( "context" + "os" + "path/filepath" "go.jetpack.io/pkg/sandbox/runx/impl/download" "go.jetpack.io/pkg/sandbox/runx/impl/fileutil" @@ -9,21 +11,29 @@ import ( "go.jetpack.io/pkg/sandbox/runx/impl/types" ) +var xdgInstallationSubdir = "jetpack.io/pkgs" + type Registry struct { rootPath fileutil.Path gh *github.Client } -func NewLocalRegistry(rootDir string) (*Registry, error) { - rootPath := fileutil.Path(rootDir) - err := rootPath.EnsureDir() +func NewLocalRegistry(ctx context.Context, githubAPIToken string) (*Registry, error) { + cacheDir, err := os.UserCacheDir() if err != nil { + cacheDir = "~/.cache" + } + + rootDir := filepath.Join(cacheDir, xdgInstallationSubdir) + rootPath := fileutil.Path(rootDir) + + if err := rootPath.EnsureDir(); err != nil { return nil, err } return &Registry{ rootPath: rootPath, - gh: github.NewClient(), + gh: github.NewClient(ctx, githubAPIToken), }, nil } @@ -36,7 +46,7 @@ func (r *Registry) ListReleases(ctx context.Context, owner, repo string) ([]type } func (r *Registry) GetReleaseMetadata(ctx context.Context, ref types.PkgRef) (types.ReleaseMetadata, error) { - resolvedRef, err := r.resolveVersion(ref) + resolvedRef, err := r.ResolveVersion(ref) if err != nil { return types.ReleaseMetadata{}, err } @@ -49,7 +59,7 @@ func (r *Registry) GetReleaseMetadata(ctx context.Context, ref types.PkgRef) (ty } func (r *Registry) GetArtifactMetadata(ctx context.Context, ref types.PkgRef, platform types.Platform) (types.ArtifactMetadata, error) { - resolvedRef, err := r.resolveVersion(ref) + resolvedRef, err := r.ResolveVersion(ref) if err != nil { return types.ArtifactMetadata{}, err } @@ -67,7 +77,7 @@ func (r *Registry) GetArtifactMetadata(ctx context.Context, ref types.PkgRef, pl } func (r *Registry) GetArtifact(ctx context.Context, ref types.PkgRef, platform types.Platform) (string, error) { - resolvedRef, err := r.resolveVersion(ref) + resolvedRef, err := r.ResolveVersion(ref) if err != nil { return "", err } @@ -86,7 +96,7 @@ func (r *Registry) GetArtifact(ctx context.Context, ref types.PkgRef, platform t } func (r *Registry) GetPackage(ctx context.Context, ref types.PkgRef, platform types.Platform) (string, error) { - resolvedRef, err := r.resolveVersion(ref) + resolvedRef, err := r.ResolveVersion(ref) if err != nil { return "", err } @@ -115,7 +125,7 @@ func (r *Registry) GetPackage(ctx context.Context, ref types.PkgRef, platform ty return installPath.String(), nil } -func (r *Registry) resolveVersion(ref types.PkgRef) (types.PkgRef, error) { +func (r *Registry) ResolveVersion(ref types.PkgRef) (types.PkgRef, error) { if ref.Version != "" && ref.Version != "latest" { return ref, nil } diff --git a/pkg/sandbox/runx/impl/run.go b/pkg/sandbox/runx/impl/run.go index 91943549..e8294f6e 100644 --- a/pkg/sandbox/runx/impl/run.go +++ b/pkg/sandbox/runx/impl/run.go @@ -1,6 +1,7 @@ package impl import ( + "context" "errors" "os" "os/exec" @@ -9,12 +10,12 @@ import ( "go.jetpack.io/pkg/sandbox/runx/impl/types" ) -func Run(args ...string) error { +func (r *RunX) Run(ctx context.Context, args ...string) error { parsed, err := parseArgs(args) if err != nil { return err } - return run(parsed) + return r.run(ctx, parsed) } // TODO: is this the best name for this struct? @@ -24,8 +25,8 @@ type parsedArgs struct { Args []string } -func run(args parsedArgs) error { - paths, err := install(args.Packages...) +func (r *RunX) run(ctx context.Context, args parsedArgs) error { + paths, err := r.install(ctx, args.Packages...) if err != nil { return err } diff --git a/pkg/sandbox/runx/impl/runopt/runopt.go b/pkg/sandbox/runx/impl/runopt/runopt.go new file mode 100644 index 00000000..d52aedaa --- /dev/null +++ b/pkg/sandbox/runx/impl/runopt/runopt.go @@ -0,0 +1,5 @@ +package runopt + +type Opts struct { + GithubAPIToken string +} diff --git a/pkg/sandbox/runx/impl/runx.go b/pkg/sandbox/runx/impl/runx.go new file mode 100644 index 00000000..ced72cc4 --- /dev/null +++ b/pkg/sandbox/runx/impl/runx.go @@ -0,0 +1,13 @@ +package impl + +import "go.jetpack.io/pkg/sandbox/runx/impl/runopt" + +type RunX struct { + GithubAPIToken string +} + +func New(opts runopt.Opts) *RunX { + return &RunX{ + GithubAPIToken: opts.GithubAPIToken, + } +} diff --git a/pkg/sandbox/runx/runx.go b/pkg/sandbox/runx/runx.go index 261f60fe..0463aa5a 100644 --- a/pkg/sandbox/runx/runx.go +++ b/pkg/sandbox/runx/runx.go @@ -1,15 +1,19 @@ package runx import ( + "context" + "go.jetpack.io/pkg/sandbox/runx/impl" + "go.jetpack.io/pkg/sandbox/runx/impl/runopt" ) -// Install installs the given packages and returns the paths to the directories -// where they were installed. -func Install(pkgs ...string) ([]string, error) { - return impl.Install(pkgs...) +type RunX interface { + // Install installs the given packages and returns the paths to the directories + // where they were installed. + Install(ctx context.Context, pkgs ...string) ([]string, error) + Run(ctx context.Context, args ...string) error } -func Run(args ...string) error { - return impl.Run(args...) +func New(opts runopt.Opts) RunX { + return impl.New(opts) } From cb9c2480abd88ea41daf39c8f131072f50bf9b7f Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 6 Oct 2023 12:16:06 -0700 Subject: [PATCH 2/2] Requested changes --- pkg/sandbox/runx/cmd/runx/cli/root.go | 7 +++---- pkg/sandbox/runx/devbox.json | 3 ++- pkg/sandbox/runx/impl/registry/registry.go | 2 +- pkg/sandbox/runx/impl/runopt/runopt.go | 5 ----- pkg/sandbox/runx/impl/runx.go | 13 ------------- pkg/sandbox/runx/impl/{ => runx}/install.go | 2 +- pkg/sandbox/runx/impl/{ => runx}/run.go | 2 +- pkg/sandbox/runx/impl/runx/runx.go | 5 +++++ pkg/sandbox/runx/runx.go | 19 ------------------- 9 files changed, 13 insertions(+), 45 deletions(-) delete mode 100644 pkg/sandbox/runx/impl/runopt/runopt.go delete mode 100644 pkg/sandbox/runx/impl/runx.go rename pkg/sandbox/runx/impl/{ => runx}/install.go (98%) rename pkg/sandbox/runx/impl/{ => runx}/run.go (99%) create mode 100644 pkg/sandbox/runx/impl/runx/runx.go delete mode 100644 pkg/sandbox/runx/runx.go diff --git a/pkg/sandbox/runx/cmd/runx/cli/root.go b/pkg/sandbox/runx/cmd/runx/cli/root.go index f2b18877..64fb9d9b 100644 --- a/pkg/sandbox/runx/cmd/runx/cli/root.go +++ b/pkg/sandbox/runx/cmd/runx/cli/root.go @@ -7,8 +7,7 @@ import ( "os" "github.com/fatih/color" - "go.jetpack.io/pkg/sandbox/runx" - "go.jetpack.io/pkg/sandbox/runx/impl/runopt" + "go.jetpack.io/pkg/sandbox/runx/impl/runx" ) func Help() { @@ -30,9 +29,9 @@ func Execute(ctx context.Context, args []string) int { install := flag.Bool("install", false, "install packages only") flag.Parse() - runx := runx.New(runopt.Opts{ + runx := runx.RunX{ GithubAPIToken: os.Getenv("RUNX_GITHUB_API_TOKEN"), - }) + } if *install { paths, err := runx.Install(ctx, args[1:]...) diff --git a/pkg/sandbox/runx/devbox.json b/pkg/sandbox/runx/devbox.json index 79d2ae97..abfecc72 100644 --- a/pkg/sandbox/runx/devbox.json +++ b/pkg/sandbox/runx/devbox.json @@ -4,7 +4,8 @@ ], "shell": { "scripts": { - "build": "go build -o dist/runx cmd/runx/main.go" + "build": "go build -o dist/runx cmd/runx/main.go", + "build-pkg": "go build -o dist/pkg cmd/pkg/main.go" } } } diff --git a/pkg/sandbox/runx/impl/registry/registry.go b/pkg/sandbox/runx/impl/registry/registry.go index d7aafadc..19a341d2 100644 --- a/pkg/sandbox/runx/impl/registry/registry.go +++ b/pkg/sandbox/runx/impl/registry/registry.go @@ -21,7 +21,7 @@ type Registry struct { func NewLocalRegistry(ctx context.Context, githubAPIToken string) (*Registry, error) { cacheDir, err := os.UserCacheDir() if err != nil { - cacheDir = "~/.cache" + return nil, err } rootDir := filepath.Join(cacheDir, xdgInstallationSubdir) diff --git a/pkg/sandbox/runx/impl/runopt/runopt.go b/pkg/sandbox/runx/impl/runopt/runopt.go deleted file mode 100644 index d52aedaa..00000000 --- a/pkg/sandbox/runx/impl/runopt/runopt.go +++ /dev/null @@ -1,5 +0,0 @@ -package runopt - -type Opts struct { - GithubAPIToken string -} diff --git a/pkg/sandbox/runx/impl/runx.go b/pkg/sandbox/runx/impl/runx.go deleted file mode 100644 index ced72cc4..00000000 --- a/pkg/sandbox/runx/impl/runx.go +++ /dev/null @@ -1,13 +0,0 @@ -package impl - -import "go.jetpack.io/pkg/sandbox/runx/impl/runopt" - -type RunX struct { - GithubAPIToken string -} - -func New(opts runopt.Opts) *RunX { - return &RunX{ - GithubAPIToken: opts.GithubAPIToken, - } -} diff --git a/pkg/sandbox/runx/impl/install.go b/pkg/sandbox/runx/impl/runx/install.go similarity index 98% rename from pkg/sandbox/runx/impl/install.go rename to pkg/sandbox/runx/impl/runx/install.go index 526687ae..2445873f 100644 --- a/pkg/sandbox/runx/impl/install.go +++ b/pkg/sandbox/runx/impl/runx/install.go @@ -1,4 +1,4 @@ -package impl +package runx import ( "context" diff --git a/pkg/sandbox/runx/impl/run.go b/pkg/sandbox/runx/impl/runx/run.go similarity index 99% rename from pkg/sandbox/runx/impl/run.go rename to pkg/sandbox/runx/impl/runx/run.go index e8294f6e..d2609de7 100644 --- a/pkg/sandbox/runx/impl/run.go +++ b/pkg/sandbox/runx/impl/runx/run.go @@ -1,4 +1,4 @@ -package impl +package runx import ( "context" diff --git a/pkg/sandbox/runx/impl/runx/runx.go b/pkg/sandbox/runx/impl/runx/runx.go new file mode 100644 index 00000000..48cbdf84 --- /dev/null +++ b/pkg/sandbox/runx/impl/runx/runx.go @@ -0,0 +1,5 @@ +package runx + +type RunX struct { + GithubAPIToken string +} diff --git a/pkg/sandbox/runx/runx.go b/pkg/sandbox/runx/runx.go deleted file mode 100644 index 0463aa5a..00000000 --- a/pkg/sandbox/runx/runx.go +++ /dev/null @@ -1,19 +0,0 @@ -package runx - -import ( - "context" - - "go.jetpack.io/pkg/sandbox/runx/impl" - "go.jetpack.io/pkg/sandbox/runx/impl/runopt" -) - -type RunX interface { - // Install installs the given packages and returns the paths to the directories - // where they were installed. - Install(ctx context.Context, pkgs ...string) ([]string, error) - Run(ctx context.Context, args ...string) error -} - -func New(opts runopt.Opts) RunX { - return impl.New(opts) -}