Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/sandbox/runx/cmd/pkg/cli/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
38 changes: 38 additions & 0 deletions pkg/sandbox/runx/cmd/pkg/cli/resolve.go
Original file line number Diff line number Diff line change
@@ -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 <owner>/<repo>@<version>",
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
}
1 change: 1 addition & 0 deletions pkg/sandbox/runx/cmd/pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func RootCmd() *cobra.Command {
}

command.AddCommand(ReleasesCmd())
command.AddCommand(ResolveCmd())

return command
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/sandbox/runx/cmd/runx/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"os"

"github.com/fatih/color"
"go.jetpack.io/pkg/sandbox/runx"
"go.jetpack.io/pkg/sandbox/runx/impl/runx"
)

func Help() {
Expand All @@ -29,8 +29,12 @@ func Execute(ctx context.Context, args []string) int {
install := flag.Bool("install", false, "install packages only")
flag.Parse()

runx := runx.RunX{
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
Expand All @@ -40,7 +44,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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sandbox/runx/devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
12 changes: 10 additions & 2 deletions pkg/sandbox/runx/impl/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
28 changes: 19 additions & 9 deletions pkg/sandbox/runx/impl/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@ package registry

import (
"context"
"os"
"path/filepath"

"go.jetpack.io/pkg/sandbox/runx/impl/download"
"go.jetpack.io/pkg/sandbox/runx/impl/fileutil"
"go.jetpack.io/pkg/sandbox/runx/impl/github"
"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 {
return nil, err
}

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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package impl
package runx

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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package impl
package runx

import (
"context"
"errors"
"os"
"os/exec"
Expand All @@ -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?
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/sandbox/runx/impl/runx/runx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package runx

type RunX struct {
GithubAPIToken string
}
15 changes: 0 additions & 15 deletions pkg/sandbox/runx/runx.go

This file was deleted.