-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.go
More file actions
117 lines (100 loc) · 2.56 KB
/
Copy pathadd.go
File metadata and controls
117 lines (100 loc) · 2.56 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
package pin
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/git-pkgs/pin/manifest"
)
type AddOptions struct {
Dir string
Manifest string
Lock string
RegistryURL string
Exact bool
DryRun bool
}
// AddResult has SyncResult == nil under --dry-run.
type AddResult struct {
Entry manifest.Entry
Resolved string
SyncResult *SyncResult
}
func Add(ctx context.Context, spec string, files []string, opts AddOptions) (*AddResult, error) {
return New(ClientOptions{RegistryURL: opts.RegistryURL}).Add(ctx, spec, files, opts)
}
// Add resolves a package's latest-satisfying version, inserts it into
// the manifest at its alphabetic position, and runs Sync.
func (c *Client) Add(ctx context.Context, spec string, files []string, opts AddOptions) (*AddResult, error) {
if opts.Manifest == "" {
opts.Manifest = DefaultManifest
}
if opts.Lock == "" {
opts.Lock = DefaultLock
}
name, constraint := parseSpec(spec)
if name == "" {
return nil, fmt.Errorf("add: package name is required")
}
src := c.NPM
e := manifest.Entry{Name: name}
resolved, err := src.ResolveVersion(ctx, e.PURL(""), defaultIfEmpty(constraint, "latest"), nil)
if err != nil {
return nil, err
}
written := constraint
if written == "" {
if opts.Exact {
written = resolved
} else {
written = caretMajorMinor(resolved)
}
}
entry := manifest.Entry{Name: name, Version: written, Files: files}
manifestPath := filepath.Join(opts.Dir, opts.Manifest)
in, err := os.ReadFile(manifestPath)
if err != nil {
return nil, err
}
var out bytes.Buffer
if err := manifest.AddEntry(bytes.NewReader(in), &out, entry); err != nil {
return nil, err
}
if opts.DryRun {
return &AddResult{Entry: entry, Resolved: resolved}, nil
}
if err := os.WriteFile(manifestPath, out.Bytes(), filePerm); err != nil {
return nil, err
}
syncRes, err := c.Sync(ctx, SyncOptions{
Dir: opts.Dir,
Manifest: opts.Manifest,
Lock: opts.Lock,
})
if err != nil {
return nil, err
}
return &AddResult{Entry: entry, Resolved: resolved, SyncResult: syncRes}, nil
}
func parseSpec(spec string) (name, constraint string) {
at := strings.LastIndex(spec, "@")
if at <= 0 {
return spec, ""
}
return spec[:at], spec[at+1:]
}
func caretMajorMinor(version string) string {
parts := strings.SplitN(version, ".", 3) //nolint:mnd
if len(parts) < 2 { //nolint:mnd
return "^" + version
}
return "^" + parts[0] + "." + parts[1]
}
func defaultIfEmpty(s, fallback string) string {
if s == "" {
return fallback
}
return s
}