|
| 1 | +package cmd |
| 2 | + |
| 3 | +// CHESComputing foxden tool: s3 module |
| 4 | +// |
| 5 | +// Copyright (c) 2023 - Valentin Kuznetsov <vkuznet@gmail.com> |
| 6 | +// |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "net/url" |
| 12 | + "os" |
| 13 | + |
| 14 | + srvConfig "github.com/CHESSComputing/golib/config" |
| 15 | + "github.com/CHESSComputing/golib/utils" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +// IngestRecord represents ingest record from FabricNode |
| 20 | +type IngestRecord struct { |
| 21 | + Ingested int `json:"ingested"` |
| 22 | + Did string `json:"did"` |
| 23 | + GraphIRI string `json:"graphIRI"` |
| 24 | +} |
| 25 | + |
| 26 | +// helper function to provide fabric usage info |
| 27 | +func fabricUsage() { |
| 28 | + fmt.Println("foxden fabric <ls|ingest> [options]") |
| 29 | + fmt.Println("\nExamples:") |
| 30 | + fmt.Println("\n# ingest did into FabricNode:") |
| 31 | + fmt.Println("foxden fabric ingest <did>") |
| 32 | +} |
| 33 | + |
| 34 | +// helper function to list content of a bucket on s3 storage |
| 35 | +func fabricList(args []string, jsonOutput bool) { |
| 36 | + // args contains [ls bucket] |
| 37 | + if args[0] != "ls" { |
| 38 | + fmt.Println("ERROR: wrong action", args) |
| 39 | + os.Exit(1) |
| 40 | + } |
| 41 | + |
| 42 | + // get beamlines datasets from fabric node |
| 43 | + bl := args[1] |
| 44 | + rurl := fmt.Sprintf("%s/catalog/beamlines/%s/datasets", |
| 45 | + srvConfig.Config.Services.FabricCatalogURL, bl) |
| 46 | + |
| 47 | + if verbose > 0 { |
| 48 | + fmt.Println("HTTP GET", rurl) |
| 49 | + } |
| 50 | + resp, err := _httpReadRequest.Get(rurl) |
| 51 | + if err != nil { |
| 52 | + fmt.Println("ERROR:", err) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + defer resp.Body.Close() |
| 56 | + dec := json.NewDecoder(resp.Body) |
| 57 | + var data map[string]any |
| 58 | + if err := dec.Decode(&data); err != nil { |
| 59 | + fmt.Println("ERROR:", err) |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | + if jsonOutput { |
| 63 | + if val, err := json.MarshalIndent(data, "", " "); err == nil { |
| 64 | + fmt.Println(string(val)) |
| 65 | + } |
| 66 | + return |
| 67 | + } |
| 68 | + printMap(data) |
| 69 | +} |
| 70 | + |
| 71 | +// helper function to ingest did into fabric node |
| 72 | +func fabricIngest(args []string) { |
| 73 | + // args contains [create bucket] |
| 74 | + if len(args) != 2 { |
| 75 | + fmt.Println("ERROR: wrong number of arguments") |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + if args[0] != "ingest" { |
| 79 | + fmt.Println("ERROR: wrong action", args) |
| 80 | + os.Exit(1) |
| 81 | + } |
| 82 | + did := args[1] |
| 83 | + bl := utils.GetBeamline(did) |
| 84 | + encodedDid := url.QueryEscape(did) |
| 85 | + rurl := fmt.Sprintf("%s/beamlines/%s/datasets/%s/foxden/ingest", |
| 86 | + srvConfig.Config.Services.FabricDataServiceURL, bl, encodedDid) |
| 87 | + if verbose > 0 { |
| 88 | + fmt.Println("HTTP POST", rurl) |
| 89 | + } |
| 90 | + resp, err := _httpWriteRequest.Post(rurl, "", bytes.NewBuffer([]byte{})) |
| 91 | + if err != nil { |
| 92 | + fmt.Println("ERROR:", err) |
| 93 | + os.Exit(1) |
| 94 | + } |
| 95 | + defer resp.Body.Close() |
| 96 | + dec := json.NewDecoder(resp.Body) |
| 97 | + var results IngestRecord |
| 98 | + if err := dec.Decode(&results); err != nil { |
| 99 | + fmt.Println("ERROR:", err) |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + fmt.Printf("fabric ingest results:\n%+v\n", results) |
| 103 | +} |
| 104 | + |
| 105 | +func fabricCommand() *cobra.Command { |
| 106 | + cmd := &cobra.Command{ |
| 107 | + Use: "fabric", |
| 108 | + Short: "foxden fabric commands", |
| 109 | + Long: "foxden fabric commands to access CHESS FabricNode service\n" + doc, |
| 110 | + Args: cobra.MinimumNArgs(0), |
| 111 | + Run: func(cmd *cobra.Command, args []string) { |
| 112 | + jsonOutput, _ := cmd.Flags().GetBool("json") |
| 113 | + if len(args) == 0 { |
| 114 | + fabricUsage() |
| 115 | + } else if args[0] == "ls" { |
| 116 | + accessToken() |
| 117 | + fabricList(args, jsonOutput) |
| 118 | + } else if args[0] == "ingest" { |
| 119 | + writeToken() |
| 120 | + fabricIngest(args) |
| 121 | + } else { |
| 122 | + fmt.Printf("WARNING: unsupported option(s) %+v\n", args) |
| 123 | + } |
| 124 | + }, |
| 125 | + } |
| 126 | + cmd.PersistentFlags().Bool("json", false, "json output") |
| 127 | + cmd.SetUsageFunc(func(*cobra.Command) error { |
| 128 | + fabricUsage() |
| 129 | + return nil |
| 130 | + }) |
| 131 | + return cmd |
| 132 | +} |
0 commit comments