@@ -9,7 +9,7 @@ use crate::{
9
9
parse:: ParseOpts ,
10
10
schema:: { Class , Property } ,
11
11
storelike:: Query ,
12
- urls, Storelike , Value ,
12
+ urls, Resource , Storelike , Value ,
13
13
} ;
14
14
15
15
const DEFAULT_ONTOLOGY_PATH : & str = "defaultOntology" ;
@@ -153,17 +153,43 @@ pub fn populate_base_models(store: &impl Storelike) -> AtomicResult<()> {
153
153
Ok ( ( ) )
154
154
}
155
155
156
- /// Creates a Drive resource at the base URL. Does not set rights. Use set_drive_rights for that.
157
- pub fn create_drive ( store : & impl Storelike ) -> AtomicResult < ( ) > {
158
- let self_url = store
159
- . get_self_url ( )
160
- . ok_or ( "No self_url set, cannot populate store with Drive" ) ?;
161
- let mut drive = store. get_resource_new ( & self_url) ;
156
+ /// Creates a Drive resource at the base URL if no name is passed.
157
+ #[ tracing:: instrument( skip( store) , level = "info" ) ]
158
+ pub fn create_drive (
159
+ store : & impl Storelike ,
160
+ drive_name : Option < & str > ,
161
+ for_agent : & str ,
162
+ public_read : bool ,
163
+ ) -> AtomicResult < Resource > {
164
+ let mut self_url = if let Some ( url) = store. get_self_url ( ) {
165
+ url. to_owned ( )
166
+ } else {
167
+ return Err ( "No self URL set. Cannot create drive." . into ( ) ) ;
168
+ } ;
169
+ let drive_subject: String = if let Some ( name) = drive_name {
170
+ // Let's make a subdomain
171
+ let host = self_url. host ( ) . expect ( "No host in server_url" ) ;
172
+ let subdomain_host = format ! ( "{}.{}" , name, host) ;
173
+ self_url. set_host ( Some ( & subdomain_host) ) ?;
174
+ self_url. to_string ( )
175
+ } else {
176
+ self_url. to_string ( )
177
+ } ;
178
+
179
+ let mut drive = if drive_name. is_some ( ) {
180
+ if store. get_resource ( & drive_subject) . is_ok ( ) {
181
+ return Err ( "Drive URL is already taken" . into ( ) ) ;
182
+ }
183
+ Resource :: new ( drive_subject)
184
+ } else {
185
+ // Only for the base URL (of no drive name is passed), we should not check if the drive exists.
186
+ // This is because we use `create_drive` in the `--initialize` command.
187
+ store. get_resource_new ( & drive_subject)
188
+ } ;
162
189
drive. set_class ( urls:: DRIVE ) ;
163
- let server_url = url:: Url :: parse ( store. get_server_url ( ) ) ?;
164
190
drive. set_string (
165
191
urls:: NAME . into ( ) ,
166
- server_url . host_str ( ) . ok_or ( "Can't use current base URL" ) ? ,
192
+ drive_name . unwrap_or_else ( || self_url . host_str ( ) . unwrap ( ) ) ,
167
193
store,
168
194
) ?;
169
195
drive. save_locally ( store) ?;
@@ -236,8 +262,10 @@ To use the data in your web apps checkout our client libraries: [@tomic/lib](htt
236
262
Use [@tomic/cli](https://docs.atomicdata.dev/js-cli) to generate typed ontologies inside your code.
237
263
"# , store. get_server_url( ) , & format!( "{}/{}" , drive. get_subject( ) , DEFAULT_ONTOLOGY_PATH ) ) , store) ?;
238
264
}
265
+
239
266
drive. save_locally ( store) ?;
240
- Ok ( ( ) )
267
+
268
+ Ok ( drive)
241
269
}
242
270
243
271
/// Imports the Atomic Data Core items (the entire atomicdata.dev Ontology / Vocabulary)
@@ -312,7 +340,11 @@ pub fn populate_importer(store: &crate::Db) -> AtomicResult<()> {
312
340
. ok_or ( "No self URL in this Store - required for populating importer" ) ?;
313
341
let mut importer = crate :: Resource :: new ( urls:: construct_path_import ( & base) ) ;
314
342
importer. set_class ( urls:: IMPORTER ) ;
315
- importer. set ( urls:: PARENT . into ( ) , Value :: AtomicUrl ( base) , store) ?;
343
+ importer. set (
344
+ urls:: PARENT . into ( ) ,
345
+ Value :: AtomicUrl ( base. to_string ( ) ) ,
346
+ store,
347
+ ) ?;
316
348
importer. set ( urls:: NAME . into ( ) , Value :: String ( "Import" . into ( ) ) , store) ?;
317
349
importer. save_locally ( store) ?;
318
350
Ok ( ( ) )
@@ -323,7 +355,7 @@ pub fn populate_importer(store: &crate::Db) -> AtomicResult<()> {
323
355
/// Useful for helping a new user get started.
324
356
pub fn populate_sidebar_items ( store : & crate :: Db ) -> AtomicResult < ( ) > {
325
357
let base = store. get_self_url ( ) . ok_or ( "No self_url" ) ?;
326
- let mut drive = store. get_resource ( & base) ?;
358
+ let mut drive = store. get_resource ( base. as_str ( ) ) ?;
327
359
let arr = vec ! [
328
360
format!( "{}/setup" , base) ,
329
361
format!( "{}/import" , base) ,
0 commit comments