Skip to content

Commit

Permalink
span-compare: allow ad-hoc source names request via flag
Browse files Browse the repository at this point in the history
  • Loading branch information
miku committed Feb 12, 2020
1 parent 306a254 commit 3e04cb1
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/span-compare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path"
"sort"
"strings"
"text/template"

"github.com/miku/clam"
"github.com/miku/span"
"github.com/miku/span/solrutil"

Expand Down Expand Up @@ -175,6 +177,7 @@ var SourceNames = map[string]string{
var defaultConfigPath = path.Join(span.UserHomeDir(), ".config/span/span.json")

var (
amslLiveServer = flag.String("amsl", "", "url to live amsl api for ad-hoc source names, e.g. https://example.technology")
liveServer = flag.String("a", "http://localhost:8983/solr/biblio", "live server location")
nonliveServer = flag.String("b", "http://localhost:8983/solr/biblio", "non-live server location")
whatIsLive = flag.Bool("e", false, "use whatislive.url to determine live and non live servers")
Expand All @@ -192,6 +195,32 @@ type ResultWriter interface {
Err() error
}

// fetchSourceNames from AMSL, crudely via shell.
func fetchSourceNames(amsl string) (map[string]string, error) {
result := make(map[string]string)
filename, err := clam.RunOutput(`span-amsl-discovery -live {{ live }} | jq -rc '.[]| [.sourceID, .megaCollection] | @tsv' | sort -un > {{ output }}`,
clam.Map{"live": amsl})
if err != nil {
return nil, nil
}
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(b), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
fields := strings.Split(line, "\t")
if len(fields) != 2 {
return nil, fmt.Errorf("expected two fields, got %d: %v", len(fields), line)
}
result[fields[0]] = fields[1]
}
return result, nil
}

// TabWriter is the simplest writer.
type TabWriter struct {
w io.Writer
Expand Down Expand Up @@ -288,6 +317,16 @@ func renderSourceLink(tmpl string, data interface{}, text string) (string, error
func main() {
flag.Parse()

if *amslLiveServer != "" {
log.Printf("fetching source names via AMSL: %s", *amslLiveServer)
names, err := fetchSourceNames(*amslLiveServer)
if err != nil {
log.Fatal(err)
}
SourceNames = names
log.Printf("fetched %d names", len(SourceNames))
}

if *whatIsLive {
// Fallback configuration.
if _, err := os.Stat(*spanConfigFile); os.IsNotExist(err) {
Expand Down

0 comments on commit 3e04cb1

Please sign in to comment.