Debian Codegraph @

cmd/csv-surrogate-keys/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "github.com/wildducktheories/go-csv"
5
6 "flag"
7 "fmt"
8 "os"
9 )
10
11 func configure(args []string) (*csv.SurrogateKeysProcess, error) {
12 var naturalKey, surrogateKey string
13 var err error
14
15 flags := flag.NewFlagSet("surrogate-keys", flag.ContinueOnError)
16
17 flags.StringVar(&naturalKey, "natural-key", "", "The fields of the natural key")
18 flags.StringVar(&surrogateKey, "surrogate-key", "", "The field name for the surrogate key.")
19
20 if err = flags.Parse(args); err != nil {
21 return nil, err
22 }
23
24 usage := func() {
25 fmt.Printf("usage: surrogate-keys {options}\n")
26 flag.PrintDefaults()
27 }
28
29 // Use a CSV parser to extract the partial keys from the parameter
30 naturalKeys, err := csv.Parse(naturalKey)
31 if err != nil || len(naturalKey) < 1 {
32 usage()
33 return nil, fmt.Errorf("--natural-key must specify one or more columns")
34 }
35
36 if surrogateKey == "" {
37 usage()
38 return nil, fmt.Errorf("--surrogate-key must specify the name of a new column")
39 }
40
41 return &csv.SurrogateKeysProcess{
42 NaturalKeys: naturalKeys,
43 SurrogateKey: surrogateKey,
44 }, nil
45 }
46
47 func main() {
48 var p *csv.SurrogateKeysProcess
49 var err error
50
51 errCh := make(chan error, 1)
52 if p, err = configure(os.Args[1:]); err == nil {
53 p.Run(csv.WithIoReader(os.Stdin), csv.WithIoWriter(os.Stdout), errCh)
54 err = <-errCh
55 }
56
57 if err != nil {
58 fmt.Printf("fatal: %v\n", err)
59 os.Exit(1)
60 }
61 }