Skip to content

Commit ee40567

Browse files
committed
improve logic of fabric command
1 parent 909ff64 commit ee40567

1 file changed

Lines changed: 53 additions & 46 deletions

File tree

foxden/cmd/fabric_node.go

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ func fabricUsage() {
4141
fmt.Println("# check health of data-service and catalog-service:")
4242
fmt.Println("foxden fabric health")
4343
fmt.Println()
44-
fmt.Println("# run SPARQL verification for a beamline dataset:")
45-
fmt.Println("foxden fabric sparql <beamline> <did>")
44+
fmt.Println("# run SPARQL verification for a dataset (beamline is extracted from the DID):")
45+
fmt.Println("foxden fabric sparql <did>")
46+
fmt.Println()
47+
fmt.Println("# limit the number of triples shown in SPARQL output:")
48+
fmt.Println("foxden fabric sparql <did> --limit 10")
4649
fmt.Println()
4750
fmt.Println("# verify catalog entry for a beamline:")
4851
fmt.Println("foxden fabric catalog <beamline>")
@@ -137,15 +140,21 @@ func fabricHealth(jsonOutput bool) {
137140
}
138141
}
139142

140-
// helper function to run SPARQL verification for a beamline/dataset
141-
func fabricSPARQL(args []string, jsonOutput bool) {
142-
if len(args) < 3 {
143-
fmt.Println("ERROR: sparql requires <beamline> <did>")
144-
fmt.Println("Usage: foxden fabric sparql <beamline> <did>")
143+
// helper function to run SPARQL verification for a dataset.
144+
// The beamline is extracted from the DID itself.
145+
func fabricSPARQL(args []string, jsonOutput bool, limit int) {
146+
if len(args) < 2 {
147+
fmt.Println("ERROR: sparql requires <did>")
148+
fmt.Println("Usage: foxden fabric sparql <did>")
149+
os.Exit(1)
150+
}
151+
did := args[1]
152+
153+
bl := utils.GetBeamline(did)
154+
if bl == "" {
155+
fmt.Printf("ERROR: cannot extract beamline from DID %q\n", did)
145156
os.Exit(1)
146157
}
147-
bl := args[1]
148-
did := args[2]
149158

150159
encodedDid := url.PathEscape(did)
151160
rurl := fmt.Sprintf("%s/beamlines/%s/datasets/%s/sparql",
@@ -190,21 +199,21 @@ func fabricSPARQL(args []string, jsonOutput bool) {
190199
return
191200
}
192201

193-
// Show the first few triples for quick inspection.
194-
limit := 5
195-
if len(bindings) < limit {
196-
limit = len(bindings)
202+
// Show up to `limit` triples for quick inspection.
203+
show := limit
204+
if show <= 0 || show > len(bindings) {
205+
show = len(bindings)
197206
}
198-
for i, b := range bindings[:limit] {
207+
for i, b := range bindings[:show] {
199208
bm, ok := b.(map[string]any)
200209
if !ok {
201210
continue
202211
}
203212
s, p, o := termValue(bm, "s"), termValue(bm, "p"), termValue(bm, "o")
204213
fmt.Printf(" [%d] <%s>\n <%s>\n %q\n", i+1, s, p, o)
205214
}
206-
if len(bindings) > limit {
207-
fmt.Printf(" … and %d more triple(s)\n", len(bindings)-limit)
215+
if len(bindings) > show {
216+
fmt.Printf(" … and %d more triple(s)\n", len(bindings)-show)
208217
}
209218
}
210219

@@ -372,9 +381,9 @@ func ingestOneDID(did string) {
372381
}
373382

374383
// helper function to ingest did (or a file of dids) into fabric node.
375-
// The second argument may be:
376-
// - a DID string starting with "/beamline=…", or
377-
// - a path to a plain-text file containing one DID per line.
384+
// The second argument is resolved in this order:
385+
// 1. If the path exists on disk → treat as a file containing one DID per line.
386+
// 2. Otherwise → treat as a literal DID string (must start with /beamline=).
378387
func fabricIngest(args []string) {
379388
if len(args) != 2 {
380389
fmt.Println("ERROR: wrong number of arguments")
@@ -388,37 +397,33 @@ func fabricIngest(args []string) {
388397

389398
arg := args[1]
390399

391-
// Detect whether the argument is a file path or a literal DID.
392-
// A DID starts with "/beamline="; anything else we treat as a file.
393-
if strings.HasPrefix(arg, "/beamline=") {
394-
// Single DID ingest.
395-
fmt.Printf("Ingesting DID: %s\n", arg)
396-
ingestOneDID(arg)
400+
// 1. Check whether the argument is an existing file first.
401+
if _, err := os.Stat(arg); err == nil {
402+
dids, err := readDIDsFromFile(arg)
403+
if err != nil {
404+
fmt.Println("ERROR reading DID file:", err)
405+
os.Exit(1)
406+
}
407+
if len(dids) == 0 {
408+
fmt.Println("ERROR: no valid DIDs found in file", arg)
409+
os.Exit(1)
410+
}
411+
fmt.Printf("Ingesting %d DID(s) from %s\n", len(dids), arg)
412+
for _, did := range dids {
413+
fmt.Printf(" → %s\n", did)
414+
ingestOneDID(did)
415+
}
416+
fmt.Printf("Done. %d DID(s) processed.\n", len(dids))
397417
return
398418
}
399419

400-
// Treat as a file containing DIDs.
401-
if _, err := os.Stat(arg); err != nil {
402-
fmt.Printf("ERROR: %q is neither a valid DID (must start with /beamline=) nor an existing file: %v\n", arg, err)
403-
os.Exit(1)
404-
}
405-
406-
dids, err := readDIDsFromFile(arg)
407-
if err != nil {
408-
fmt.Println("ERROR reading DID file:", err)
409-
os.Exit(1)
410-
}
411-
if len(dids) == 0 {
412-
fmt.Println("ERROR: no valid DIDs found in file", arg)
420+
// 2. Treat as a literal DID string.
421+
if !strings.HasPrefix(arg, "/beamline=") {
422+
fmt.Printf("ERROR: %q is neither an existing file nor a valid DID (must start with /beamline=)\n", arg)
413423
os.Exit(1)
414424
}
415-
416-
fmt.Printf("Ingesting %d DID(s) from %s\n", len(dids), arg)
417-
for _, did := range dids {
418-
fmt.Printf(" → %s\n", did)
419-
ingestOneDID(did)
420-
}
421-
fmt.Printf("Done. %d DID(s) processed.\n", len(dids))
425+
fmt.Printf("Ingesting DID: %s\n", arg)
426+
ingestOneDID(arg)
422427
}
423428

424429
func fabricCommand() *cobra.Command {
@@ -429,6 +434,7 @@ func fabricCommand() *cobra.Command {
429434
Args: cobra.MinimumNArgs(0),
430435
Run: func(cmd *cobra.Command, args []string) {
431436
jsonOutput, _ := cmd.Flags().GetBool("json")
437+
limit, _ := cmd.Flags().GetInt("limit")
432438
if len(args) == 0 {
433439
fabricUsage()
434440
return
@@ -449,7 +455,7 @@ func fabricCommand() *cobra.Command {
449455
fabricHealth(jsonOutput)
450456
case "sparql":
451457
accessToken()
452-
fabricSPARQL(args, jsonOutput)
458+
fabricSPARQL(args, jsonOutput, limit)
453459
case "catalog":
454460
accessToken()
455461
fabricCatalog(args, jsonOutput)
@@ -460,6 +466,7 @@ func fabricCommand() *cobra.Command {
460466
},
461467
}
462468
cmd.PersistentFlags().Bool("json", false, "json output")
469+
cmd.PersistentFlags().Int("limit", 5, "number of SPARQL triples to display (0 = all)")
463470
cmd.SetUsageFunc(func(*cobra.Command) error {
464471
fabricUsage()
465472
return nil

0 commit comments

Comments
 (0)