File: main.go

package info (click to toggle)
golang-github-wildducktheories-go-csv 0.0~git20210709.8745000-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 5
file content (61 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"github.com/wildducktheories/go-csv"

	"flag"
	"fmt"
	"os"
)

func configure(args []string) (*csv.SurrogateKeysProcess, error) {
	var naturalKey, surrogateKey string
	var err error

	flags := flag.NewFlagSet("surrogate-keys", flag.ContinueOnError)

	flags.StringVar(&naturalKey, "natural-key", "", "The fields of the natural key")
	flags.StringVar(&surrogateKey, "surrogate-key", "", "The field name for the surrogate key.")

	if err = flags.Parse(args); err != nil {
		return nil, err
	}

	usage := func() {
		fmt.Printf("usage: surrogate-keys {options}\n")
		flag.PrintDefaults()
	}

	// Use  a CSV parser to extract the partial keys from the parameter
	naturalKeys, err := csv.Parse(naturalKey)
	if err != nil || len(naturalKey) < 1 {
		usage()
		return nil, fmt.Errorf("--natural-key must specify one or more columns")
	}

	if surrogateKey == "" {
		usage()
		return nil, fmt.Errorf("--surrogate-key must specify the name of a new column")
	}

	return &csv.SurrogateKeysProcess{
		NaturalKeys:  naturalKeys,
		SurrogateKey: surrogateKey,
	}, nil
}

func main() {
	var p *csv.SurrogateKeysProcess
	var err error

	errCh := make(chan error, 1)
	if p, err = configure(os.Args[1:]); err == nil {
		p.Run(csv.WithIoReader(os.Stdin), csv.WithIoWriter(os.Stdout), errCh)
		err = <-errCh
	}

	if err != nil {
		fmt.Printf("fatal: %v\n", err)
		os.Exit(1)
	}
}