Skip to content

Commit 2afdd19

Browse files
authored
Fix go-libs (#21)
1 parent 9f01ccf commit 2afdd19

File tree

14 files changed

+189
-570
lines changed

14 files changed

+189
-570
lines changed

go-libs/fileset/fileset.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package fileset
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"io/fs"
7-
"log"
88
"os"
99
"path"
1010
"regexp"
1111
"strings"
1212
"time"
13+
14+
"github.com/databricks/databricks-sdk-go/logger"
1315
)
1416

1517
type FileSet []File
@@ -100,7 +102,7 @@ func (fi File) MustMatch(needle string) bool {
100102
func (fi File) FindAll(needle *regexp.Regexp) (all []string, err error) {
101103
raw, err := fi.Raw()
102104
if err != nil {
103-
log.Printf("[ERROR] read %s: %s", fi.Absolute, err)
105+
logger.Errorf(context.Background(), "read %s: %s", fi.Absolute, err)
104106
return nil, err
105107
}
106108
for _, v := range needle.FindAllStringSubmatch(string(raw), -1) {
@@ -112,7 +114,7 @@ func (fi File) FindAll(needle *regexp.Regexp) (all []string, err error) {
112114
func (fi File) Match(needle *regexp.Regexp) bool {
113115
raw, err := fi.Raw()
114116
if err != nil {
115-
log.Printf("[ERROR] read %s: %s", fi.Absolute, err)
117+
logger.Errorf(context.Background(), "read %s: %s", fi.Absolute, err)
116118
return false
117119
}
118120
return needle.Match(raw)
@@ -146,14 +148,6 @@ func RecursiveChildren(dir string) (found FileSet, err error) {
146148
if current.Name() == "vendor" {
147149
continue
148150
}
149-
if current.Name() == "scripts" {
150-
continue
151-
}
152-
// ext directory contains external projects at the current project depends
153-
// on. We should ignore it for listing files in the project
154-
if current.Name() == "ext" {
155-
continue
156-
}
157151
children, err := ReadDir(current.Absolute)
158152
if err != nil {
159153
return nil, err

go-libs/github/github.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ package github
22

33
import (
44
"context"
5-
"encoding/json"
6-
"errors"
75
"fmt"
8-
"io"
96
"net/http"
10-
"strings"
117
"time"
128

139
"github.com/databricks/databricks-sdk-go/httpclient"
@@ -16,10 +12,6 @@ import (
1612
const gitHubAPI = "https://api.github.com"
1713
const gitHubUserContent = "https://raw.githubusercontent.com"
1814

19-
// Placeholders to use as unique keys in context.Context.
20-
var apiOverride int
21-
var userContentOverride int
22-
2315
type GitHubClient struct {
2416
api *httpclient.ApiClient
2517
cfg *GitHubConfig
@@ -91,48 +83,3 @@ func (c *GitHubClient) ListRuns(ctx context.Context, org, repo, workflow string)
9183
err := c.api.Do(ctx, "GET", path, httpclient.WithResponseUnmarshal(&response))
9284
return response.WorkflowRuns, err
9385
}
94-
95-
func WithApiOverride(ctx context.Context, override string) context.Context {
96-
return context.WithValue(ctx, &apiOverride, override)
97-
}
98-
99-
func WithUserContentOverride(ctx context.Context, override string) context.Context {
100-
return context.WithValue(ctx, &userContentOverride, override)
101-
}
102-
103-
var ErrNotFound = errors.New("not found")
104-
105-
func getBytes(ctx context.Context, method, url string, body io.Reader) ([]byte, error) {
106-
ao, ok := ctx.Value(&apiOverride).(string)
107-
if ok {
108-
url = strings.Replace(url, gitHubAPI, ao, 1)
109-
}
110-
uco, ok := ctx.Value(&userContentOverride).(string)
111-
if ok {
112-
url = strings.Replace(url, gitHubUserContent, uco, 1)
113-
}
114-
req, err := http.NewRequestWithContext(ctx, "GET", url, body)
115-
if err != nil {
116-
return nil, err
117-
}
118-
res, err := http.DefaultClient.Do(req)
119-
if err != nil {
120-
return nil, err
121-
}
122-
if res.StatusCode == 404 {
123-
return nil, ErrNotFound
124-
}
125-
if res.StatusCode >= 400 {
126-
return nil, fmt.Errorf("github request failed: %s", res.Status)
127-
}
128-
defer res.Body.Close()
129-
return io.ReadAll(res.Body)
130-
}
131-
132-
func httpGetAndUnmarshal(ctx context.Context, url string, response any) error {
133-
raw, err := getBytes(ctx, "GET", url, nil)
134-
if err != nil {
135-
return err
136-
}
137-
return json.Unmarshal(raw, response)
138-
}

go-libs/github/releases.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,30 @@ const cacheTTL = 1 * time.Hour
1313

1414
// NewReleaseCache creates a release cache for a repository in the GitHub org.
1515
// Caller has to provide different cache directories for different repositories.
16-
func NewReleaseCache(org, repo, cacheDir string) *ReleaseCache {
16+
func NewReleaseCache(client *GitHubClient, org, repo, cacheDir string) *ReleaseCache {
1717
pattern := fmt.Sprintf("%s-%s-releases", org, repo)
1818
return &ReleaseCache{
19-
cache: localcache.NewLocalCache[Versions](cacheDir, pattern, cacheTTL),
20-
Org: org,
21-
Repo: repo,
19+
cache: localcache.NewLocalCache[Versions](cacheDir, pattern, cacheTTL),
20+
client: client,
21+
Org: org,
22+
Repo: repo,
2223
}
2324
}
2425

2526
type ReleaseCache struct {
26-
cache localcache.LocalCache[Versions]
27-
Org string
28-
Repo string
27+
cache localcache.LocalCache[Versions]
28+
client *GitHubClient
29+
Org string
30+
Repo string
2931
}
3032

3133
func (r *ReleaseCache) Load(ctx context.Context) (Versions, error) {
3234
return r.cache.Load(ctx, func() (Versions, error) {
33-
return getVersions(ctx, r.Org, r.Repo)
35+
logger.Debugf(ctx, "Fetching latest releases for %s/%s from GitHub API", r.Org, r.Repo)
36+
return r.client.Versions(ctx, r.Org, r.Repo)
3437
})
3538
}
3639

37-
// getVersions is considered to be a private API, as we want the usage go through a cache
38-
func getVersions(ctx context.Context, org, repo string) (Versions, error) {
39-
var releases Versions
40-
logger.Debugf(ctx, "Fetching latest releases for %s/%s from GitHub API", org, repo)
41-
url := fmt.Sprintf("%s/repos/%s/%s/releases", gitHubAPI, org, repo)
42-
err := httpGetAndUnmarshal(ctx, url, &releases)
43-
return releases, err
44-
}
45-
4640
type ghAsset struct {
4741
Name string `json:"name"`
4842
ContentType string `json:"content_type"`

go-libs/github/releases_test.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

go-libs/github/repositories.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,28 @@ import (
1111

1212
const repositoryCacheTTL = 24 * time.Hour
1313

14-
func NewRepositoryCache(org, cacheDir string) *repositoryCache {
14+
func NewRepositoryCache(client *GitHubClient, org, cacheDir string) *repositoryCache {
1515
filename := fmt.Sprintf("%s-repositories", org)
1616
return &repositoryCache{
17-
cache: localcache.NewLocalCache[Repositories](cacheDir, filename, repositoryCacheTTL),
18-
Org: org,
17+
cache: localcache.NewLocalCache[Repositories](cacheDir, filename, repositoryCacheTTL),
18+
client: client,
19+
Org: org,
1920
}
2021
}
2122

2223
type repositoryCache struct {
23-
cache localcache.LocalCache[Repositories]
24-
Org string
24+
cache localcache.LocalCache[Repositories]
25+
client *GitHubClient
26+
Org string
2527
}
2628

2729
func (r *repositoryCache) Load(ctx context.Context) (Repositories, error) {
2830
return r.cache.Load(ctx, func() (Repositories, error) {
29-
return getRepositories(ctx, r.Org)
31+
logger.Debugf(ctx, "Loading repositories for %s from GitHub API", r.Org)
32+
return r.client.ListRepositories(ctx, r.Org)
3033
})
3134
}
3235

33-
// getRepositories is considered to be privata API, as we want the usage to go through a cache
34-
func getRepositories(ctx context.Context, org string) (Repositories, error) {
35-
var repos Repositories
36-
logger.Debugf(ctx, "Loading repositories for %s from GitHub API", org)
37-
url := fmt.Sprintf("%s/users/%s/repos", gitHubAPI, org)
38-
err := httpGetAndUnmarshal(ctx, url, &repos)
39-
return repos, err
40-
}
41-
4236
type Repositories []Repo
4337

4438
type Repo struct {

go-libs/github/repositories_test.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

go-libs/go.mod

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.21.0
44

55
require (
66
github.com/briandowns/spinner v1.23.0
7-
github.com/databricks/databricks-sdk-go v0.26.0
7+
github.com/databricks/databricks-sdk-go v0.26.1
88
github.com/fatih/color v1.16.0
99
github.com/nwidger/jsoncolor v0.3.2
1010
github.com/spf13/cobra v1.8.0
@@ -18,7 +18,7 @@ require (
1818
cloud.google.com/go/compute v1.23.3 // indirect
1919
cloud.google.com/go/compute/metadata v0.2.3 // indirect
2020
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
21-
github.com/fsnotify/fsnotify v1.6.0 // indirect
21+
github.com/fsnotify/fsnotify v1.7.0 // indirect
2222
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
2323
github.com/golang/protobuf v1.5.3 // indirect
2424
github.com/google/go-querystring v1.1.0 // indirect
@@ -32,26 +32,25 @@ require (
3232
github.com/mitchellh/mapstructure v1.5.0 // indirect
3333
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
3434
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
35-
github.com/sagikazarmark/locafero v0.3.0 // indirect
35+
github.com/sagikazarmark/locafero v0.4.0 // indirect
3636
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
3737
github.com/sourcegraph/conc v0.3.0 // indirect
38-
github.com/spf13/afero v1.10.0 // indirect
39-
github.com/spf13/cast v1.5.1 // indirect
38+
github.com/spf13/afero v1.11.0 // indirect
39+
github.com/spf13/cast v1.6.0 // indirect
4040
github.com/subosito/gotenv v1.6.0 // indirect
4141
go.opencensus.io v0.24.0 // indirect
42-
go.uber.org/atomic v1.9.0 // indirect
43-
go.uber.org/multierr v1.9.0 // indirect
42+
go.uber.org/multierr v1.11.0 // indirect
4443
golang.org/x/crypto v0.16.0 // indirect
45-
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
44+
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
4645
golang.org/x/mod v0.14.0 // indirect
4746
golang.org/x/net v0.19.0 // indirect
4847
golang.org/x/sys v0.15.0 // indirect
4948
golang.org/x/term v0.15.0 // indirect
5049
golang.org/x/text v0.14.0 // indirect
5150
golang.org/x/time v0.5.0 // indirect
5251
google.golang.org/api v0.152.0 // indirect
53-
google.golang.org/appengine v1.6.7 // indirect
54-
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
52+
google.golang.org/appengine v1.6.8 // indirect
53+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
5554
google.golang.org/grpc v1.59.0 // indirect
5655
google.golang.org/protobuf v1.31.0 // indirect
5756
gopkg.in/ini.v1 v1.67.0 // indirect

0 commit comments

Comments
 (0)