-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuses.go
160 lines (134 loc) · 3.21 KB
/
uses.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Harry Randazzo
package vai
import (
"context"
"fmt"
"io"
"net/url"
"path/filepath"
"github.com/charmbracelet/log"
"github.com/noxsios/vai/uses"
"github.com/package-url/packageurl-go"
)
// CacheEnvVar is the environment variable for the cache directory.
const CacheEnvVar = "VAI_CACHE"
// ExecuteUses runs a task from a remote workflow source.
func ExecuteUses(ctx context.Context, store *uses.Store, u string, with With, prev string, dry bool) error {
logger := log.FromContext(ctx)
logger.Debug("using", "task", u)
uri, err := url.Parse(u)
if err != nil {
return err
}
if uri.Scheme == "" {
return fmt.Errorf("must contain a scheme: %q", u)
}
previous, err := url.Parse(prev)
if err != nil {
return err
}
if previous.Scheme == "" {
return fmt.Errorf("must contain a scheme: %q", prev)
}
var next *url.URL
if uri.Scheme == "file" {
switch previous.Scheme {
case "http", "https":
// turn relative paths into absolute references
next = previous
next.Path = filepath.Join(filepath.Dir(previous.Path), uri.Opaque)
if next.Path == "." {
next.Path = DefaultFileName
}
case "pkg":
pURL, err := packageurl.FromString(prev)
if err != nil {
return err
}
// turn relative paths into absolute references
pURL.Subpath = filepath.Join(filepath.Dir(pURL.Subpath), uri.Opaque)
if pURL.Subpath == "." {
pURL.Subpath = DefaultFileName
}
next, _ = url.Parse(pURL.String())
default:
dir := filepath.Dir(previous.Opaque)
if dir != "." {
next = &url.URL{
Scheme: uri.Scheme,
Opaque: filepath.Join(dir, uri.Opaque),
RawQuery: uri.RawQuery,
}
if next.Opaque == "." {
next.Opaque = DefaultFileName
}
}
}
if next != nil {
logger.Debug("merged", "previous", previous, "uses", u, "next", next)
u = next.String()
}
}
if next == nil {
next, _ = url.Parse(u)
}
if uri.Scheme == "pkg" {
// dogsledding the error here since we know it's a package URL
pURL, _ := packageurl.FromString(u)
if pURL.Subpath == "" {
pURL.Subpath = DefaultFileName
}
if pURL.Version == "" {
pURL.Version = "main"
}
u = pURL.String()
}
fetcher, err := uses.SelectFetcher(uri, previous)
if err != nil {
return err
}
logger.Debug("chosen", "fetcher", fmt.Sprintf("%T", fetcher))
var f io.ReadCloser
if downloader, ok := fetcher.(uses.Downloader); ok {
desc, err := downloader.Describe(ctx, u)
if err != nil {
return err
}
exists, err := store.Exists(desc)
if err != nil {
return err
}
if !exists {
logger.Debug("caching", "task", u)
rc, err := downloader.Fetch(ctx, u)
if err != nil {
return err
}
defer rc.Close()
if err := store.Store(rc); err != nil {
return err
}
}
f, err = store.Fetch(desc)
if err != nil {
return err
}
defer f.Close()
} else {
f, err = fetcher.Fetch(ctx, u)
if err != nil {
return err
}
defer f.Close()
}
if f == nil {
return fmt.Errorf("failed to fetch %s referenced by %s", u, prev)
}
wf, err := ReadAndValidate(f)
if err != nil {
return err
}
taskName := uri.Query().Get("task")
return Run(ctx, store, wf, taskName, with, next.String(), dry)
}