-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.go
More file actions
84 lines (64 loc) · 1.62 KB
/
util.go
File metadata and controls
84 lines (64 loc) · 1.62 KB
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
package github
import (
"errors"
"io"
"os"
"path/filepath"
"github.com/google/go-github/v35/github"
"github.com/nhatthm/plugin-registry/plugin"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)
// ErrArtifactNotFound indicates that the artifact is not found.
var ErrArtifactNotFound = errors.New("artifact not found")
func writeFile(fs afero.Fs, path string, r io.Reader) error {
f, err := fs.OpenFile(path, os.O_CREATE|os.O_RDWR, os.FileMode(0o644)) //nolint: nosnakecase
if err != nil {
return err
}
defer f.Close() //nolint: errcheck
if _, err := io.Copy(f, r); err != nil {
return err
}
if r, ok := r.(io.ReadCloser); ok {
_ = r.Close() //nolint: errcheck
}
return nil
}
func loadMetadata(r io.Reader) (*plugin.Plugin, error) {
var p plugin.Plugin
dec := yaml.NewDecoder(r)
if err := dec.Decode(&p); err != nil {
return nil, err
}
if r, ok := r.(io.ReadCloser); ok {
_ = r.Close() //nolint: errcheck
}
return &p, nil
}
func writeMetadata(fs afero.Fs, path string, p *plugin.Plugin) error {
return writeFile(fs, filepath.Join(path, plugin.MetadataFile), newYamlReader(p))
}
func findAsset(r *github.RepositoryRelease, file string) (*github.ReleaseAsset, error) {
if len(r.Assets) == 0 {
return nil, ErrArtifactNotFound
}
for _, a := range r.Assets {
if *a.Name == file {
return a, nil
}
}
return nil, ErrArtifactNotFound
}
func chmod(fs afero.Fs, contentType *string, path string, fileMode os.FileMode) error {
if contentType == nil {
return nil
}
switch *contentType {
case "application/octet-stream",
"application/gzip":
return fs.Chmod(path, fileMode)
default:
return nil
}
}