@@ -47,6 +47,11 @@ func (R *Runtime) Load() (err error) {
47
47
return err
48
48
}
49
49
50
+ err = R .prepPlacedUserfiles ()
51
+ if err != nil {
52
+ return err
53
+ }
54
+
50
55
if ! R .Flags .IngoreErrors {
51
56
err = R .Value .Validate ()
52
57
if err != nil {
@@ -93,7 +98,7 @@ func (R *Runtime) prepPlacedDatafiles() {
93
98
fname , fpath := parts [0 ], parts [1 ]
94
99
R .dataMappings [fname ] = fpath
95
100
entries = append (entries , fname )
96
- continue
101
+ continue
97
102
}
98
103
}
99
104
@@ -108,6 +113,113 @@ func (R *Runtime) prepPlacedDatafiles() {
108
113
R .Entrypoints = entries
109
114
}
110
115
116
+ func (R * Runtime ) prepPlacedUserfiles () error {
117
+ start := time .Now ()
118
+ defer func () {
119
+ end := time .Now ()
120
+ R .Stats .Add ("files/load" , end .Sub (start ))
121
+ }()
122
+
123
+ buildFile := func (trimPath , filePath string ) (* ast.Field , error ) {
124
+ // prep inputs
125
+ d , err := os .ReadFile (filePath )
126
+ if err != nil {
127
+ return nil , fmt .Errorf ("while loading user file: %w" , err )
128
+ }
129
+ s := string (d )
130
+ l := strings .TrimPrefix (filePath , trimPath )
131
+
132
+ // build file field
133
+ ff := new (ast.Field )
134
+ ff .Constraint = token .STRING
135
+ ff .Label = ast .NewIdent (l )
136
+ ff .Value = ast .NewString (s )
137
+
138
+ return ff , nil
139
+ }
140
+
141
+ embedFiles := func (cuePath , trimPath , filePath string ) error {
142
+ files := []string {filePath }
143
+ // expand globs
144
+ if strings .Contains (filePath , "*" ) {
145
+ fs , err := yagu .FilesFromGlobs ([]string {filePath })
146
+ if err != nil {
147
+ return fmt .Errorf ("warning: error while globing %q: %v" , filePath , err )
148
+ }
149
+
150
+ files = fs
151
+ } else {
152
+ // maybe handle directory
153
+ stat , err := os .Stat (filePath )
154
+ if err != nil {
155
+ return err
156
+ }
157
+
158
+ if stat .IsDir () {
159
+ fs , err := yagu .FilesFromGlobs ([]string {filePath + "/*" })
160
+ if err != nil {
161
+ return fmt .Errorf ("warning: error while loading dir %q: %v" , filePath , err )
162
+ }
163
+
164
+ files = fs
165
+ }
166
+ }
167
+
168
+ fields := []interface {}{}
169
+ for _ , file := range files {
170
+ fld , err := buildFile (trimPath , file )
171
+ if err != nil {
172
+ return err
173
+ }
174
+ fields = append (fields , fld )
175
+ }
176
+
177
+ // build CUE struct
178
+ st := ast .NewStruct (fields ... )
179
+ // fmt.Sprintf("%+v\n", st)
180
+
181
+ cp := cue .ParsePath (cuePath )
182
+ R .Value = R .Value .FillPath (cp , st )
183
+
184
+ return nil
185
+ }
186
+
187
+ // handle the flag for placing user files
188
+ for _ , U := range R .Flags .UserFiles {
189
+ // fmt.Println(U)
190
+ parts := strings .Split (U , "=" )
191
+ if len (parts ) != 2 {
192
+ return fmt .Errorf ("-U/--user-files flag should have format <cue-path>=<glob>" )
193
+ }
194
+
195
+ cuePath , filePath := parts [0 ], parts [1 ]
196
+
197
+ trimPath := ""
198
+ if strings .Contains (filePath , "%" ) {
199
+ parts := strings .Split (filePath , "%" )
200
+ if len (parts ) != 2 {
201
+ return fmt .Errorf ("-U/--user-files only supports one % to trim prefix" )
202
+ }
203
+ trimPath = parts [0 ] + "/"
204
+ filePath = strings .Replace (filePath , "%" , "/" , 1 )
205
+ }
206
+
207
+ err := embedFiles (cuePath , trimPath , filePath )
208
+ if err != nil {
209
+ return err
210
+ }
211
+ }
212
+
213
+ // look for @userfiles attributes and do similar
214
+
215
+ if R .Flags .Verbosity > 1 {
216
+ fmt .Println ("user files:" , R .userFiles )
217
+ fmt .Println ("mod files: " , R .modFiles )
218
+ }
219
+
220
+ return nil
221
+ }
222
+
111
223
func (R * Runtime ) load () (err error ) {
112
224
beg := time .Now ()
113
225
defer func () {
@@ -133,28 +245,6 @@ func (R *Runtime) load() (err error) {
133
245
R .Entrypoints = append (R .Entrypoints , "-" )
134
246
}
135
247
136
- // fi, err := os.Stdin.Stat()
137
-
138
- //if fi.Size() == 0 {
139
- // fmt.Println("stdin(0)...")
140
- // R.CueConfig.Stdin = &bytes.Buffer{}
141
-
142
- //} else {
143
- // d, err := io.ReadAll(os.Stdin)
144
- // if err != nil {
145
- // return err
146
- // }
147
- // r := bytes.NewReader(d)
148
- // fmt.Println(len(d), r.Len(), r.Size(), fi.Size())
149
- // R.CueConfig.Stdin = r
150
-
151
- // //if .... {
152
- // //fmt.Println("no stdin...")
153
- // //}
154
- // // R.CueConfig.Stdin = os.Stdin
155
- //}
156
-
157
-
158
248
//
159
249
//
160
250
// load instances from entrypoints and config, this is the main CUE loader step
0 commit comments