-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmds.go
More file actions
134 lines (124 loc) · 3.25 KB
/
Copy pathcmds.go
File metadata and controls
134 lines (124 loc) · 3.25 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
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
package pin
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/git-pkgs/pin/manifest"
)
const initTemplate = `out: "static/vendor"
assets: []
`
// Init writes a starter pin.yaml in dir. Fails if the destination
// already exists; pin will not overwrite an existing manifest.
func Init(dir, manifestPath string) error {
if manifestPath == "" {
manifestPath = DefaultManifest
}
p := filepath.Join(dir, manifestPath)
if _, err := os.Stat(p); err == nil {
return fmt.Errorf("%s already exists", p)
} else if !errors.Is(err, fs.ErrNotExist) {
return err
}
return os.WriteFile(p, []byte(initTemplate), filePerm)
}
func Remove(ctx context.Context, names []string, opts SyncOptions) (*SyncResult, error) {
c, err := clientFromSyncOptions(opts)
if err != nil {
return nil, err
}
return c.Remove(ctx, names, opts)
}
// Remove deletes the named entries from the manifest and runs Sync to
// clean up the resulting lockfile and on-disk files.
func (c *Client) Remove(ctx context.Context, names []string, opts SyncOptions) (*SyncResult, error) {
if opts.Manifest == "" {
opts.Manifest = DefaultManifest
}
manifestPath := filepath.Join(opts.Dir, opts.Manifest)
in, err := os.ReadFile(manifestPath)
if err != nil {
return nil, err
}
current := in
for _, name := range names {
var out bytes.Buffer
if err := manifest.RemoveEntry(bytes.NewReader(current), &out, name); err != nil {
return nil, err
}
current = out.Bytes()
}
if opts.DryRun {
return &SyncResult{}, nil
}
if err := os.WriteFile(manifestPath, current, filePerm); err != nil {
return nil, err
}
return c.Sync(ctx, opts)
}
type ListEntry struct {
Name string `json:"name"`
Version string `json:"version"`
PURL string `json:"purl"`
Path string `json:"path"`
Out string `json:"out"`
Type string `json:"type"`
Integrity string `json:"integrity"`
Size int64 `json:"size"`
}
// List returns one ListEntry per vendored file recorded in the
// lockfile.
func List(opts VerifyOptions) ([]ListEntry, error) {
if opts.Lock == "" {
opts.Lock = DefaultLock
}
l, err := readLock(filepath.Join(opts.Dir, opts.Lock))
if err != nil {
return nil, err
}
if l == nil {
return nil, fmt.Errorf("%w at %s", ErrNoLockfile, filepath.Join(opts.Dir, opts.Lock))
}
out := make([]ListEntry, 0, len(l.Assets))
for _, a := range l.Assets {
out = append(out, ListEntry{
Name: a.Name,
Version: a.Version,
PURL: a.PURL,
Path: a.Path,
Out: a.Out,
Type: a.Type,
Integrity: a.Integrity,
Size: a.Size,
})
}
return out, nil
}
// Path returns the on-disk paths for every vendored file belonging
// to the named package.
func Path(name string, opts VerifyOptions) ([]string, error) {
if opts.Lock == "" {
opts.Lock = DefaultLock
}
l, err := readLock(filepath.Join(opts.Dir, opts.Lock))
if err != nil {
return nil, err
}
if l == nil {
return nil, fmt.Errorf("%w at %s", ErrNoLockfile, filepath.Join(opts.Dir, opts.Lock))
}
var paths []string
for _, a := range l.Assets {
if a.Name == name {
paths = append(paths, filepath.Join(opts.Dir, l.OutDir, filepath.FromSlash(a.Out)))
}
}
if len(paths) == 0 {
return nil, fmt.Errorf("%s not found in lockfile", name)
}
return paths, nil
}