/
github_fetcher.go
66 lines (53 loc) · 1.36 KB
/
github_fetcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package tiltextension
import (
"context"
"fmt"
"io/ioutil"
"os"
"path"
"time"
"github.com/tilt-dev/go-get"
)
type Downloader interface {
RootDir() string
Download(pkg string) (string, error)
}
type TempDirDownloader struct {
rootDir string
}
func NewTempDirDownloader() (*TempDirDownloader, error) {
dir, err := ioutil.TempDir("", "tilt-extensions")
if err != nil {
return nil, err
}
return &TempDirDownloader{rootDir: dir}, nil
}
func (d *TempDirDownloader) RootDir() string {
return d.rootDir
}
func (d *TempDirDownloader) Download(pkg string) (string, error) {
dlr := get.NewDownloader(d.rootDir)
return dlr.Download(pkg)
}
type GithubFetcher struct {
dlr Downloader
}
func NewGithubFetcher(dlr Downloader) *GithubFetcher {
return &GithubFetcher{dlr: dlr}
}
func (f *GithubFetcher) CleanUp() error {
return os.RemoveAll(f.dlr.RootDir())
}
func (f *GithubFetcher) Fetch(ctx context.Context, moduleName string) (ModuleContents, error) {
dir, err := f.dlr.Download(path.Join("github.com/tilt-dev/tilt-extensions", moduleName))
if err != nil {
return ModuleContents{}, fmt.Errorf("Fetching tilt-extensions: %v", err)
}
return ModuleContents{
Name: moduleName,
Dir: dir,
ExtensionRegistry: "https://github.com/tilt-dev/tilt-extensions",
TimeFetched: time.Now(),
}, nil
}
var _ Fetcher = (*GithubFetcher)(nil)