Skip to content

Commit

Permalink
minimal CLI app for comparison (close #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimus committed Dec 27, 2021
1 parent d9b5ab7 commit 371c244
Show file tree
Hide file tree
Showing 6 changed files with 11,915 additions and 8 deletions.
13 changes: 12 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package config

import "github.com/gnames/gnfmt"

type Config struct {
Format gnfmt.Format
}

type Option func(*Config)

func OptFormat(f gnfmt.Format) Option {
return func(cfg *Config) {
cfg.Format = f
}
}

func New(opts ...Option) Config {
res := Config{}
res := Config{
Format: gnfmt.CSV,
}
for _, opt := range opts {
opt(&res)
}
Expand Down
11 changes: 7 additions & 4 deletions ent/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func MatchOutput(o Output, f gnfmt.Format) string {

// CSVHeader returns the header string for CSV output format.
func CSVHeader(f gnfmt.Format) string {
header := []string{"Source", "SourceRow", "Id", "Name", "MatchType", "Reference", "ReferenceRow", "Id", "Name", "EditDistance"}
header := []string{
"Source", "SourceRow", "Id",
"Name", "Reference", "MatchType",
"ReferenceRow", "Id", "Name", "EditDistance"}
switch f {
case gnfmt.CSV:
return gnfmt.ToCSV(header, ',')
Expand All @@ -54,15 +57,15 @@ func CSVHeader(f gnfmt.Format) string {
func csvOutput(o Output, sep rune) string {
var res []string
for i := range o.Matches {
rows := csvRow(o.Matches[i], i, sep)
rows := csvRow(o.Matches[i], sep)
for i := range rows {
res = append(res, rows[i])
}
}
return strings.Join(res, "\n")
}

func csvRow(m Match, i int, sep rune) []string {
func csvRow(m Match, sep rune) []string {
var res []string
s := m.SourceRecord
r := m.ReferenceRecords
Expand All @@ -72,8 +75,8 @@ func csvRow(m Match, i int, sep rune) []string {
strconv.Itoa(s.Index),
s.ID,
s.Name,
s.MatchType.String(),
r[i].DataSet,
r[i].MatchType.String(),
strconv.Itoa(r[i].Index),
r[i].ID,
r[i].Name,
Expand Down
75 changes: 73 additions & 2 deletions gndiff/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ package cmd

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/gnames/gndiff"
"github.com/gnames/gndiff/config"
"github.com/gnames/gndiff/ent/output"
"github.com/gnames/gndiff/ent/record"
"github.com/gnames/gndiff/io/ingestio"
"github.com/gnames/gnfmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/spf13/viper"
)

var cfgFile string
var quiet bool

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -45,10 +51,31 @@ prints out a match of a reference data to the source data.
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
var opts []config.Option
if versionFlag(cmd) {
os.Exit(0)
}

quiet, _ = cmd.Flags().GetBool("quiet")

opts = append(opts, fmtFlag(cmd))
cfg := config.New(opts...)
gnd := gndiff.New(cfg)
if len(args) != 2 {
log.Warn("Supply paths to two CSV/TSV files for comparison")
_ = cmd.Help()
os.Exit(1)
}
src, ref, err := readFiles(args[0], args[1])
if err != nil {
log.Fatal(err)
}
res, err := gnd.Compare(src, ref)
if err != nil {
log.Fatal(err)
}

fmt.Print(output.MatchOutput(res, cfg.Format))
},
}

Expand All @@ -68,6 +95,8 @@ func init() {
"'csv', 'tsv', 'compact', 'pretty'\n" +
"default is 'csv'."
rootCmd.Flags().StringP("format", "f", "", formatHelp)

rootCmd.Flags().BoolP("quiet", "q", false, "Do not output info and warning logs.")
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -92,3 +121,45 @@ func versionFlag(cmd *cobra.Command) bool {
}
return false
}

func fmtFlag(cmd *cobra.Command) config.Option {
f, err := cmd.Flags().GetString("format")
if f == "" || err != nil {
return config.OptFormat(gnfmt.CSV)
}

switch f {
case "csv":
return config.OptFormat(gnfmt.CSV)
case "tsv":
return config.OptFormat(gnfmt.TSV)
case "compact":
return config.OptFormat(gnfmt.CompactJSON)
case "pretty":
return config.OptFormat(gnfmt.PrettyJSON)
default:
if !quiet {
log.Warnf("Cannot recognize format '%s', keeping default 'CSV' format.", f)
}
return config.OptFormat(gnfmt.CSV)
}
}

func readFiles(srcPath, refPath string) ([]record.Record, []record.Record, error) {
cfg := config.New()
ing := ingestio.New(cfg)

src := filepath.Join(srcPath)
recSrc, err := ing.Records(src)
if err != nil {
return nil, nil, err
}

ref := filepath.Join(refPath)
recRef, err := ing.Records(ref)
if err != nil {
return nil, nil, err
}

return recSrc, recRef, nil
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ require (
github.com/gnames/gnparser v1.5.7
github.com/gnames/gnsys v0.2.2
github.com/gnames/levenshtein v0.2.1
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.0
github.com/stretchr/testify v1.7.0
golang.org/x/perf v0.0.0-20211012211434-03971e389cd3
golang.org/x/tools v0.1.8
Expand Down Expand Up @@ -46,7 +48,6 @@ require (
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.10.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tmthrgd/atomics v0.0.0-20180217065130-6910de195248 // indirect
github.com/tmthrgd/go-bitset v0.0.0-20180828125936-62ad9ed7ff29 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
Expand Down
Loading

0 comments on commit 371c244

Please sign in to comment.