Rework Run Log Cache so that cache dir is injected

This commit is contained in:
William Martin 2024-04-05 14:59:24 +02:00
parent f808dcee62
commit a89d50fc63
2 changed files with 52 additions and 28 deletions

View file

@ -29,35 +29,48 @@ import (
)
type runLogCache interface {
Exists(string) bool
Create(string, io.Reader) error
Open(string) (*zip.ReadCloser, error)
Exists(key string) bool
Create(key string, r io.Reader) error
Open(key string) (*zip.ReadCloser, error)
}
type rlc struct{}
type rlc struct {
cacheDir string
}
func (rlc) Exists(path string) bool {
if _, err := os.Stat(path); err != nil {
func (c rlc) Exists(key string) bool {
if _, err := os.Stat(c.filepath(key)); err != nil {
return false
}
return true
}
func (rlc) Create(path string, content io.Reader) error {
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return fmt.Errorf("could not create cache: %w", err)
}
out, err := os.Create(path)
func (c rlc) Create(key string, content io.Reader) error {
out, err := os.Create(c.filepath(key))
if err != nil {
return err
return fmt.Errorf("creating cache entry: %v", err)
}
defer out.Close()
_, err = io.Copy(out, content)
return err
if _, err := io.Copy(out, content); err != nil {
return fmt.Errorf("writing cache entry: %v", err)
}
return nil
}
func (rlc) Open(path string) (*zip.ReadCloser, error) {
return zip.OpenReader(path)
func (c rlc) Open(key string) (*zip.ReadCloser, error) {
r, err := zip.OpenReader(c.filepath(key))
if err != nil {
return nil, fmt.Errorf("opening cache entry: %v", err)
}
return r, nil
}
func (c rlc) filepath(key string) string {
return filepath.Join(c.cacheDir, fmt.Sprintf("run-log-%s.zip", key))
}
type ViewOptions struct {
@ -85,12 +98,11 @@ type ViewOptions struct {
func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Command {
opts := &ViewOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
Prompter: f.Prompter,
Now: time.Now,
Browser: f.Browser,
RunLogCache: rlc{},
IO: f.IOStreams,
HttpClient: f.HttpClient,
Prompter: f.Prompter,
Now: time.Now,
Browser: f.Browser,
}
cmd := &cobra.Command{
@ -126,6 +138,15 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
config, err := f.Config()
if err != nil {
return err
}
opts.RunLogCache = rlc{
cacheDir: config.CacheDir(),
}
if len(args) == 0 && opts.JobID == "" {
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("run or job ID required when not running interactively")
@ -431,9 +452,8 @@ func getLog(httpClient *http.Client, logURL string) (io.ReadCloser, error) {
}
func getRunLog(cache runLogCache, httpClient *http.Client, repo ghrepo.Interface, run *shared.Run, attempt uint64) (*zip.ReadCloser, error) {
filename := fmt.Sprintf("run-log-%d-%d.zip", run.ID, run.StartedTime().Unix())
filepath := filepath.Join(os.TempDir(), "gh-cli-cache", filename)
if !cache.Exists(filepath) {
cacheKey := fmt.Sprintf("%d-%d", run.ID, run.StartedTime().Unix())
if !cache.Exists(cacheKey) {
// Run log does not exist in cache so retrieve and store it
logURL := fmt.Sprintf("%srepos/%s/actions/runs/%d/logs",
ghinstance.RESTPrefix(repo.RepoHost()), ghrepo.FullName(repo), run.ID)
@ -461,13 +481,13 @@ func getRunLog(cache runLogCache, httpClient *http.Client, repo ghrepo.Interface
return nil, err
}
err = cache.Create(filepath, respReader)
err = cache.Create(cacheKey, respReader)
if err != nil {
return nil, err
}
}
return cache.Open(filepath)
return cache.Open(cacheKey)
}
func promptForJob(prompter shared.Prompter, cs *iostreams.ColorScheme, jobs []shared.Job) (*shared.Job, error) {

View file

@ -12,6 +12,7 @@ import (
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/internal/browser"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/internal/prompter"
"github.com/cli/cli/v2/pkg/cmd/run/shared"
@ -137,6 +138,9 @@ func TestNewCmdView(t *testing.T) {
f := &cmdutil.Factory{
IOStreams: ios,
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
}
argv, err := shlex.Split(tt.cli)