-
Notifications
You must be signed in to change notification settings - Fork 23
/
pull.go
151 lines (129 loc) · 3.57 KB
/
pull.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
package utils
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"github.com/jsonnet-bundler/jsonnet-bundler/pkg"
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
specv1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1"
"github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps"
)
const (
sourceFilename = ".source"
versionFilename = ".version"
relPathFilename = ".relpath"
)
func PullSource(dir, uri string) error {
d := deps.Parse("", uri)
if d == nil {
return errors.New("unable to parse URI: " + uri)
}
// read d and based on source write uri to uriFilename
source := ""
relPath := ""
version := d.Version
if d.Source.GitSource != nil {
source = d.Source.GitSource.Name()
relPath = source
} else if d.Source.LocalSource != nil {
source = d.Source.LocalSource.Directory
relPath = filepath.Base(source)
version = "local"
} else {
return errors.New("unable to parse URI: " + uri)
}
spec := specv1.New()
spec.LegacyImports = false
spec.Dependencies[d.Name()] = *d
jbLockFileBytes, err := os.ReadFile(filepath.Join(dir, jsonnetfile.LockFile))
if err != nil && !os.IsNotExist(err) {
return err
}
lockFile, err := jsonnetfile.Unmarshal(jbLockFileBytes)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(dir, ".tmp"), os.ModePerm)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(dir, sourceFilename), []byte(source), os.ModePerm)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(dir, relPathFilename), []byte(relPath), os.ModePerm)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(dir, versionFilename), []byte(version), os.ModePerm)
if err != nil {
return err
}
locked, err := pkg.Ensure(spec, dir, lockFile.Dependencies)
if err != nil {
return err
}
err = writeChangedJsonnetFile(jbLockFileBytes, &specv1.JsonnetFile{Dependencies: locked}, filepath.Join(dir, jsonnetfile.LockFile))
if err != nil {
return err
}
return nil
}
func writeChangedJsonnetFile(originalBytes []byte, modified *specv1.JsonnetFile, path string) error {
origJsonnetFile, err := jsonnetfile.Unmarshal(originalBytes)
if err != nil {
return err
}
if reflect.DeepEqual(origJsonnetFile, *modified) {
return nil
}
return writeJSONFile(path, *modified)
}
func writeJSONFile(name string, d interface{}) error {
b, err := json.MarshalIndent(d, "", " ")
if err != nil {
return fmt.Errorf("error encoding json: %w", err)
}
b = append(b, []byte("\n")...)
// nolint: gosec
return os.WriteFile(name, b, 0o644)
}
// GetSource returns the source of the dependency.
func GetSource(dir string) string {
source := ""
// if it does not exist, continue
if _, err := os.Stat(filepath.Join(dir, sourceFilename)); err == nil {
sourceBytes, err := os.ReadFile(filepath.Join(dir, sourceFilename))
if err == nil {
source = string(sourceBytes)
}
}
return source
}
// GetVersion returns the version of the dependency.
func GetVersion(dir string) string {
version := ""
// if it does not exist, continue
if _, err := os.Stat(filepath.Join(dir, versionFilename)); err == nil {
versionBytes, err := os.ReadFile(filepath.Join(dir, versionFilename))
if err == nil {
version = string(versionBytes)
}
}
return version
}
// GetRelPath returns the relative path to the dependency.
func GetRelPath(dir string) string {
relPath := ""
// if it does not exist, continue
if _, err := os.Stat(filepath.Join(dir, relPathFilename)); err == nil {
relPathBytes, err := os.ReadFile(filepath.Join(dir, relPathFilename))
if err == nil {
relPath = string(relPathBytes)
}
}
return relPath
}