-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a plugin caching mechanism (#284)
* WIP on the github cache * More WIP on the cache * Finish impl and add cache tests * Go crap * More go crap * Fix x86 arch
- Loading branch information
1 parent
97b44c4
commit 437b752
Showing
10 changed files
with
167 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/google/go-github/v63/github" | ||
"github.com/unstoppablemango/tdl/pkg/cache" | ||
) | ||
|
||
type PluginCache interface { | ||
PathFor(string) (string, error) | ||
} | ||
|
||
// There's a better way to structure these types... | ||
// and I'll figure it out later | ||
type pluginCache struct { | ||
githubClient | ||
cache cache.Cache | ||
log *slog.Logger | ||
} | ||
|
||
func NewCache(client *github.Client, path string, logger *slog.Logger) PluginCache { | ||
fsCache := cache.NewFsCache(path, logger) | ||
gh := githubClient{ | ||
client: client, | ||
cache: fsCache, | ||
log: logger, | ||
} | ||
|
||
return &pluginCache{ | ||
githubClient: gh, | ||
cache: fsCache, | ||
log: logger, | ||
} | ||
} | ||
|
||
// PathFor implements PluginCache. | ||
func (c *pluginCache) PathFor(name string) (string, error) { | ||
p, err := c.cache.Path(name) | ||
if err == nil { | ||
return p, nil | ||
} | ||
|
||
if err = c.populate(context.Background()); err != nil { | ||
return "", err | ||
} | ||
|
||
return c.cache.Path(name) | ||
} | ||
|
||
func (c *pluginCache) populate(ctx context.Context) error { | ||
asset, err := c.getLatestAsset(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.cacheAsset(ctx, asset) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package plugin_test | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/google/go-github/v63/github" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/unstoppablemango/tdl/pkg/plugin" | ||
) | ||
|
||
var _ = Describe("PluginCache", func() { | ||
var cacheDir string | ||
|
||
BeforeEach(func() { | ||
if _, found := os.LookupEnv("CI"); !found { | ||
Skip("Only running cache test in CI") | ||
} | ||
|
||
By("creating a temporary directory") | ||
tmp, err := os.MkdirTemp("", "plugin-cache-test") | ||
Expect(err).NotTo(HaveOccurred()) | ||
cacheDir = tmp | ||
}) | ||
|
||
AfterEach(func() { | ||
_ = os.RemoveAll(cacheDir) | ||
}) | ||
|
||
It("should retrieve path", func() { | ||
gh := github.NewClient(nil) | ||
cache := plugin.NewCache(gh, cacheDir, slog.Default()) | ||
|
||
// TODO: Why is uml2ts not in this | ||
result, err := cache.PathFor("uml2go") | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(result).NotTo(BeEmpty()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters