diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5732182..a28e98e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -50,6 +50,6 @@ jobs: # either 'goreleaser' (default) or 'goreleaser-pro' distribution: goreleaser version: latest - args: release --clean --snapshot + args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/datarecord/datarecord.go b/datarecord/datarecord.go index 90f91d2..11dde98 100644 --- a/datarecord/datarecord.go +++ b/datarecord/datarecord.go @@ -17,6 +17,7 @@ type dataReader struct { dateFormat string dateColumn int pivotColumn int + delimiter []byte columnNames map[string]void points int @@ -61,6 +62,11 @@ func (obj *dataReader) WithPivotColumn(column int) *dataReader { return obj } +func (obj *dataReader) WithDelimiter(delimiter string) *dataReader { + obj.delimiter = []byte(delimiter) + return obj +} + func (obj *dataReader) ReadDataRecord(data string) { record := obj.getDataRecord(data) @@ -158,7 +164,14 @@ func (obj *dataReader) getColumnNames() []string { func (obj *dataReader) getDataRecord(data string) (record dataRecord) { scan := bufio.NewScanner(strings.NewReader(data)) - scan.Split(bufio.ScanWords) + + onDelimiter := func(data []byte, atEOF bool) (advance int, token []byte, err error) { + if i := bytes.Index(data, obj.delimiter); i >= 0 { + return i + len(obj.delimiter), data[:i], nil + } + return 0, data, bufio.ErrFinalToken + } + scan.Split(onDelimiter) //bufio.ScanWords record.points = make([]float32, 0, 5) diff --git a/datarecord/datarecord_test.go b/datarecord/datarecord_test.go index 958ec34..4b4c2b0 100644 --- a/datarecord/datarecord_test.go +++ b/datarecord/datarecord_test.go @@ -12,6 +12,7 @@ func TestGetDataRecord(t *testing.T) { var reader dataReader reader.dateFormat = "20060102150405" reader.dateColumn = 1 + reader.delimiter = []byte{' '} type args struct { data string @@ -40,6 +41,7 @@ func TestGetDataRecordPivot(t *testing.T) { reader.dateFormat = "20060102150405" reader.dateColumn = 1 reader.pivotColumn = 2 + reader.delimiter = []byte{' '} type args struct { data string diff --git a/readme.md b/readme.md index 90959d5..568607b 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,9 @@ -Usage of C:\Users\worker\Documents\toGraph\toGraph.exe: +Usage of toGraph: -h print usage + -pc int + pivot column + -t string + time field format (YYYY-MM-DDTHH:mm:SS.ssssss) (default "YYYYMMDDHHmmSS") + -tc int + ordinal number of the column with time (default 1) \ No newline at end of file diff --git a/statecontext/config.go b/statecontext/config.go index 49d8863..3567cd2 100644 --- a/statecontext/config.go +++ b/statecontext/config.go @@ -14,6 +14,7 @@ type Configuration struct { DateFormat string DateColumn int PivotColumn int + Delimiter string programName string printUsage bool @@ -56,7 +57,7 @@ func readCommandLineArguments(config *Configuration, args []string) (fs *flag.Fl fs.StringVar(&config.DateFormat, "t", "YYYYMMDDHHmmSS", "time field format (YYYY-MM-DDTHH:mm:SS.ssssss)") fs.IntVar(&config.DateColumn, "tc", 1, "ordinal number of the column with time") fs.IntVar(&config.PivotColumn, "pc", 0, "pivot column") - // fs.StringVar(&config.LogOutputPath, "o", "", "log output file") + fs.StringVar(&config.Delimiter, "d", " ", "field separator") if len(args) == 0 { return nil, errEmptyArgumentList diff --git a/statecontext/config_test.go b/statecontext/config_test.go index 9d7da12..6b10102 100644 --- a/statecontext/config_test.go +++ b/statecontext/config_test.go @@ -19,29 +19,34 @@ func Test_readCommandLineArguments(t *testing.T) { want{Configuration{ DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, + Delimiter: " ", printUsage: false}, errEmptyArgumentList}}, {"test 1", []string{"programname"}, want{Configuration{ DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, + Delimiter: " ", programName: "programname", printUsage: true}, nil}}, {"help", []string{"programname", "-h"}, want{Configuration{ DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, + Delimiter: " ", programName: "programname", printUsage: true}, nil}}, {"dateFormat", []string{"programname", "-t", "YYMMDDHHmm"}, want{Configuration{ DateFormat: "YYMMDDHHmm", DateColumn: 1, + Delimiter: " ", programName: "programname", printUsage: true}, nil}}, {"dateColumn", []string{"programname", "-tc", "2"}, want{Configuration{ DateFormat: "YYYYMMDDHHmmSS", DateColumn: 2, + Delimiter: " ", programName: "programname", printUsage: true}, nil}}, {"test 2", []string{"programname", "what"}, @@ -49,18 +54,21 @@ func Test_readCommandLineArguments(t *testing.T) { InputFiles: []string{"what"}, DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, + Delimiter: " ", programName: "programname"}, nil}}, {"test 3", []string{"programname", "what", "where"}, want{Configuration{ InputFiles: []string{"what", "where"}, DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, + Delimiter: " ", programName: "programname"}, nil}}, {"test 4", []string{"programname", "newLine", "what", "where"}, want{Configuration{ InputFiles: []string{"newLine", "what", "where"}, DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, + Delimiter: " ", programName: "programname"}, nil}}, } for _, tt := range tests { diff --git a/toGraph.go b/toGraph.go index 814c8da..85ee013 100644 --- a/toGraph.go +++ b/toGraph.go @@ -17,9 +17,9 @@ var checkErr = func(err error) { } } -// TODO: truncate file // TODO: delimiter // TODO: column names +// TODO: graph name + output file name func main() { state.InitState() @@ -49,6 +49,7 @@ func processFile(sIn io.Reader, sOut io.Writer, config state.Configuration) erro reader.WithDateFormat(config.DateFormat) reader.WithDateColumn(config.DateColumn) reader.WithPivotColumn(config.PivotColumn) + reader.WithDelimiter(config.Delimiter) dataGraph, err := template.New("dataGraph").Parse(graphTemplate) checkErr(err)