Skip to content

Commit e283f10

Browse files
authored
[runx] Add support for github token, implement resolve (#171)
## Summary This PR does a few things: * Adds support for github token. This prevents rate-limiting when getting `releases.json` * Adds resolve to API. This will allow devbox to validate packages exist and resolve versions in order to use lockfile. @gcurtis I'm copying the devbox structure for the public interface (RunX interface) but was curious if you would do it differently. Specifically, we could remove the public interface and rename `impl` to just be `runx` and use a struct: ```golang runx.New(runx.Opts{ GithubAPIToken: "", }).Install(...) ``` thoughts? ## How was it tested? ``` RUNX_GITHUB_API_TOKEN=<token> runx +jetpack-io/envsec envsec RUNX_GITHUB_API_TOKEN=<token> pkg resolve jetpack-io/envsec@latest RUNX_GITHUB_API_TOKEN=<token> pkg resolve jetpack-io/envsec@v0.0.3 ```
1 parent 1b6cb51 commit e283f10

File tree

11 files changed

+98
-52
lines changed

11 files changed

+98
-52
lines changed

pkg/sandbox/runx/cmd/pkg/cli/releases.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package cli
44
// Might be moot, this CLI is mostly for testing; we'll integrate w/ devbox instead.
55

66
import (
7+
"os"
8+
79
"github.com/k0kubun/pp"
810
"github.com/spf13/cobra"
911
"go.jetpack.io/pkg/sandbox/runx/impl/github"
@@ -26,7 +28,7 @@ func releasesCmd(cmd *cobra.Command, args []string) error {
2628
return error
2729
}
2830

29-
gh := github.NewClient()
31+
gh := github.NewClient(cmd.Context(), os.Getenv("RUNX_GITHUB_API_TOKEN"))
3032
releases, err := gh.ListReleases(cmd.Context(), ref.Owner, ref.Repo)
3133
if err != nil {
3234
return err
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cli
2+
3+
// TODO: should it be `pkg show` (like poetry) or `pkg info` (like yarn)?
4+
// Might be moot, this CLI is mostly for testing; we'll integrate w/ devbox instead.
5+
6+
import (
7+
"os"
8+
9+
"github.com/k0kubun/pp"
10+
"github.com/spf13/cobra"
11+
"go.jetpack.io/pkg/sandbox/runx/impl/registry"
12+
"go.jetpack.io/pkg/sandbox/runx/impl/types"
13+
)
14+
15+
func ResolveCmd() *cobra.Command {
16+
command := &cobra.Command{
17+
Use: "resolve <owner>/<repo>@<version>",
18+
Args: cobra.ExactArgs(1),
19+
RunE: resolveCmd,
20+
}
21+
22+
return command
23+
}
24+
25+
func resolveCmd(cmd *cobra.Command, args []string) error {
26+
ref, error := types.NewPkgRef(args[0])
27+
if error != nil {
28+
return error
29+
}
30+
31+
registry, err := registry.NewLocalRegistry(cmd.Context(), os.Getenv("RUNX_GITHUB_API_TOKEN"))
32+
if err != nil {
33+
return err
34+
}
35+
36+
pp.Println(registry.ResolveVersion(ref))
37+
return nil
38+
}

pkg/sandbox/runx/cmd/pkg/cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func RootCmd() *cobra.Command {
2020
}
2121

2222
command.AddCommand(ReleasesCmd())
23+
command.AddCommand(ResolveCmd())
2324

2425
return command
2526
}

pkg/sandbox/runx/cmd/runx/cli/root.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"os"
88

99
"github.com/fatih/color"
10-
"go.jetpack.io/pkg/sandbox/runx"
10+
"go.jetpack.io/pkg/sandbox/runx/impl/runx"
1111
)
1212

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

32+
runx := runx.RunX{
33+
GithubAPIToken: os.Getenv("RUNX_GITHUB_API_TOKEN"),
34+
}
35+
3236
if *install {
33-
paths, err := runx.Install(args[1:]...)
37+
paths, err := runx.Install(ctx, args[1:]...)
3438
if err != nil {
3539
fmt.Fprintf(os.Stderr, "[ERROR] %s\n", err)
3640
return 1
@@ -40,7 +44,7 @@ func Execute(ctx context.Context, args []string) int {
4044
fmt.Printf(" %s\n", path)
4145
}
4246
} else {
43-
if err := runx.Run(args...); err != nil {
47+
if err := runx.Run(ctx, args...); err != nil {
4448
fmt.Fprintf(os.Stderr, "[ERROR] %s\n", err)
4549
return 1
4650
}

pkg/sandbox/runx/devbox.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
],
55
"shell": {
66
"scripts": {
7-
"build": "go build -o dist/runx cmd/runx/main.go"
7+
"build": "go build -o dist/runx cmd/runx/main.go",
8+
"build-pkg": "go build -o dist/pkg cmd/pkg/main.go"
89
}
910
}
1011
}

pkg/sandbox/runx/impl/github/github.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ import (
77
githubimpl "github.com/google/go-github/v53/github"
88
"go.jetpack.io/pkg/sandbox/runx/impl/httpcacher"
99
"go.jetpack.io/pkg/sandbox/runx/impl/types"
10+
"golang.org/x/oauth2"
1011
)
1112

1213
type Client struct {
1314
gh *githubimpl.Client
1415
}
1516

16-
func NewClient() *Client {
17+
func NewClient(ctx context.Context, accessToken string) *Client {
18+
tc := httpcacher.DefaultClient
19+
if accessToken != "" {
20+
ts := oauth2.StaticTokenSource(
21+
&oauth2.Token{AccessToken: accessToken},
22+
)
23+
tc = oauth2.NewClient(ctx, ts)
24+
}
1725
return &Client{
18-
gh: githubimpl.NewClient(httpcacher.DefaultClient),
26+
gh: githubimpl.NewClient(tc),
1927
}
2028
}
2129

pkg/sandbox/runx/impl/registry/registry.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,38 @@ package registry
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
57

68
"go.jetpack.io/pkg/sandbox/runx/impl/download"
79
"go.jetpack.io/pkg/sandbox/runx/impl/fileutil"
810
"go.jetpack.io/pkg/sandbox/runx/impl/github"
911
"go.jetpack.io/pkg/sandbox/runx/impl/types"
1012
)
1113

14+
var xdgInstallationSubdir = "jetpack.io/pkgs"
15+
1216
type Registry struct {
1317
rootPath fileutil.Path
1418
gh *github.Client
1519
}
1620

17-
func NewLocalRegistry(rootDir string) (*Registry, error) {
18-
rootPath := fileutil.Path(rootDir)
19-
err := rootPath.EnsureDir()
21+
func NewLocalRegistry(ctx context.Context, githubAPIToken string) (*Registry, error) {
22+
cacheDir, err := os.UserCacheDir()
2023
if err != nil {
2124
return nil, err
2225
}
2326

27+
rootDir := filepath.Join(cacheDir, xdgInstallationSubdir)
28+
rootPath := fileutil.Path(rootDir)
29+
30+
if err := rootPath.EnsureDir(); err != nil {
31+
return nil, err
32+
}
33+
2434
return &Registry{
2535
rootPath: rootPath,
26-
gh: github.NewClient(),
36+
gh: github.NewClient(ctx, githubAPIToken),
2737
}, nil
2838
}
2939

@@ -36,7 +46,7 @@ func (r *Registry) ListReleases(ctx context.Context, owner, repo string) ([]type
3646
}
3747

3848
func (r *Registry) GetReleaseMetadata(ctx context.Context, ref types.PkgRef) (types.ReleaseMetadata, error) {
39-
resolvedRef, err := r.resolveVersion(ref)
49+
resolvedRef, err := r.ResolveVersion(ref)
4050
if err != nil {
4151
return types.ReleaseMetadata{}, err
4252
}
@@ -49,7 +59,7 @@ func (r *Registry) GetReleaseMetadata(ctx context.Context, ref types.PkgRef) (ty
4959
}
5060

5161
func (r *Registry) GetArtifactMetadata(ctx context.Context, ref types.PkgRef, platform types.Platform) (types.ArtifactMetadata, error) {
52-
resolvedRef, err := r.resolveVersion(ref)
62+
resolvedRef, err := r.ResolveVersion(ref)
5363
if err != nil {
5464
return types.ArtifactMetadata{}, err
5565
}
@@ -67,7 +77,7 @@ func (r *Registry) GetArtifactMetadata(ctx context.Context, ref types.PkgRef, pl
6777
}
6878

6979
func (r *Registry) GetArtifact(ctx context.Context, ref types.PkgRef, platform types.Platform) (string, error) {
70-
resolvedRef, err := r.resolveVersion(ref)
80+
resolvedRef, err := r.ResolveVersion(ref)
7181
if err != nil {
7282
return "", err
7383
}
@@ -86,7 +96,7 @@ func (r *Registry) GetArtifact(ctx context.Context, ref types.PkgRef, platform t
8696
}
8797

8898
func (r *Registry) GetPackage(ctx context.Context, ref types.PkgRef, platform types.Platform) (string, error) {
89-
resolvedRef, err := r.resolveVersion(ref)
99+
resolvedRef, err := r.ResolveVersion(ref)
90100
if err != nil {
91101
return "", err
92102
}
@@ -115,7 +125,7 @@ func (r *Registry) GetPackage(ctx context.Context, ref types.PkgRef, platform ty
115125
return installPath.String(), nil
116126
}
117127

118-
func (r *Registry) resolveVersion(ref types.PkgRef) (types.PkgRef, error) {
128+
func (r *Registry) ResolveVersion(ref types.PkgRef) (types.PkgRef, error) {
119129
if ref.Version != "" && ref.Version != "latest" {
120130
return ref, nil
121131
}

pkg/sandbox/runx/impl/install.go renamed to pkg/sandbox/runx/impl/runx/install.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
package impl
1+
package runx
22

33
import (
44
"context"
5-
"os"
6-
"path/filepath"
75

86
"go.jetpack.io/pkg/sandbox/runx/impl/registry"
97
"go.jetpack.io/pkg/sandbox/runx/impl/types"
108
)
119

12-
var xdgInstallationSubdir = "jetpack.io/pkgs"
13-
14-
func Install(pkgs ...string) ([]string, error) {
10+
func (r *RunX) Install(ctx context.Context, pkgs ...string) ([]string, error) {
1511
refs := []types.PkgRef{}
1612

1713
for _, pkg := range pkgs {
@@ -22,13 +18,13 @@ func Install(pkgs ...string) ([]string, error) {
2218
refs = append(refs, ref)
2319
}
2420

25-
return install(refs...)
21+
return r.install(ctx, refs...)
2622
}
2723

28-
func install(pkgs ...types.PkgRef) ([]string, error) {
24+
func (r *RunX) install(ctx context.Context, pkgs ...types.PkgRef) ([]string, error) {
2925
paths := []string{}
3026
for _, pkg := range pkgs {
31-
path, err := installOne(pkg)
27+
path, err := r.installOne(ctx, pkg)
3228
if err != nil {
3329
return nil, err
3430
}
@@ -37,13 +33,8 @@ func install(pkgs ...types.PkgRef) ([]string, error) {
3733
return paths, nil
3834
}
3935

40-
func installOne(ref types.PkgRef) (string, error) {
41-
cacheDir, err := os.UserCacheDir()
42-
if err != nil {
43-
cacheDir = "~/.cache"
44-
}
45-
rootDir := filepath.Join(cacheDir, xdgInstallationSubdir)
46-
reg, err := registry.NewLocalRegistry(rootDir)
36+
func (r *RunX) installOne(ctx context.Context, ref types.PkgRef) (string, error) {
37+
reg, err := registry.NewLocalRegistry(ctx, r.GithubAPIToken)
4738
if err != nil {
4839
return "", err
4940
}

pkg/sandbox/runx/impl/run.go renamed to pkg/sandbox/runx/impl/runx/run.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package impl
1+
package runx
22

33
import (
4+
"context"
45
"errors"
56
"os"
67
"os/exec"
@@ -9,12 +10,12 @@ import (
910
"go.jetpack.io/pkg/sandbox/runx/impl/types"
1011
)
1112

12-
func Run(args ...string) error {
13+
func (r *RunX) Run(ctx context.Context, args ...string) error {
1314
parsed, err := parseArgs(args)
1415
if err != nil {
1516
return err
1617
}
17-
return run(parsed)
18+
return r.run(ctx, parsed)
1819
}
1920

2021
// TODO: is this the best name for this struct?
@@ -24,8 +25,8 @@ type parsedArgs struct {
2425
Args []string
2526
}
2627

27-
func run(args parsedArgs) error {
28-
paths, err := install(args.Packages...)
28+
func (r *RunX) run(ctx context.Context, args parsedArgs) error {
29+
paths, err := r.install(ctx, args.Packages...)
2930
if err != nil {
3031
return err
3132
}

pkg/sandbox/runx/impl/runx/runx.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package runx
2+
3+
type RunX struct {
4+
GithubAPIToken string
5+
}

0 commit comments

Comments
 (0)