Debian Codegraph @

cmd/csv-to-influx-line-format/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "errors"
5 "flag"
6 "fmt"
7 "os"
8 "time"
9
10 "github.com/wildducktheories/go-csv"
11 )
12
13 func configure(args []string) (*csv.InfluxLineFormatProcess, error) {
14 var measurement string
15 var timestamp string
16 var format string
17 var location string
18 var tags string
19 var values string
20
21 flags := flag.NewFlagSet("influx-line-format", flag.ExitOnError)
22
23 flags.StringVar(&measurement, "measurement", "", "The name of the influx measurement.")
24 flags.StringVar(&timestamp, "timestamp", "timestamp", "The name of the CSV timestamp field.")
25 flags.StringVar(&format, "format", "2006-01-02 15:04:05", "The format of the CSV timestamp field. A go timestamp format or s|ms|ns.")
26 flags.StringVar(&location, "location", "UTC", "The location in which the timestamp should be interpreted.")
27 flags.StringVar(&tags, "tags", "", "The CSV columns to be used as tags.")
28 flags.StringVar(&values, "values", "", "The CSV columns to be used as values.")
29 if err := flags.Parse(args); err != nil {
30 return nil, err
31 }
32
33 if _, err := time.LoadLocation(location); err != nil {
34 return nil, err
35 }
36
37 if timestamp == "" {
38 return nil, errors.New("--timestamp column must be specified")
39 }
40
41 if measurement == "" {
42 return nil, errors.New("--measurement must be specified")
43 }
44
45 if valuesSlice, err := csv.Parse(values); err != nil {
46 return nil, errors.New("--values must specify a set of values columns")
47 } else if tagsSlice, err := csv.Parse(tags); err != nil {
48 return nil, errors.New("--tags must specify a st of tag columns")
49 } else {
50 if len(valuesSlice) == 0 {
51 return nil, errors.New("at least one values column must be specified")
52 }
53 if len(tagsSlice) == 0 {
54 return nil, errors.New("at least one tag column must be specified")
55 }
56 return &csv.InfluxLineFormatProcess{
57 Measurement: measurement,
58 Timestamp: timestamp,
59 Format: format,
60 Location: location,
61 Tags: tagsSlice,
62 Values: valuesSlice,
63 }, nil
64 }
65 }
66
67 func main() {
68 var p *csv.InfluxLineFormatProcess
69 var err error
70
71 if p, err = configure(os.Args[1:]); err == nil {
72 errCh := make(chan error, 1)
73 p.Run(csv.WithIoReader(os.Stdin), os.Stdout, errCh)
74 err = <-errCh
75 }
76 if err != nil {
77 fmt.Printf("fatal: %v\n", err)
78 os.Exit(1)
79 }
80 }