diff --git a/datarecord/datarecord.go b/datarecord/datarecord.go index 0600245..f5a74ba 100644 --- a/datarecord/datarecord.go +++ b/datarecord/datarecord.go @@ -88,6 +88,10 @@ func (obj *dataReader) WithDelimiter(delimiter string) *dataReader { } func (obj *dataReader) WithColumnNames(data string) *dataReader { + if len(data) == 0 { + return obj + } + obj.columns.names = strings.Split(data, ",") for i := range obj.columns.names { obj.columns.names[i] = strings.TrimSpace(obj.columns.names[i]) diff --git a/statecontext/config.go b/statecontext/config.go index d90c572..af2080e 100644 --- a/statecontext/config.go +++ b/statecontext/config.go @@ -10,12 +10,13 @@ import ( var errEmptyArgumentList = fmt.Errorf("empty argument list") type Configuration struct { - InputFiles []string - DateFormat string - DateColumn int - PivotColumn int - Delimiter string - ColumnNames string + InputFiles []string + OutputFile string + DateFormat string + DateColumn int + PivotColumn int + Delimiter string + ColumnNames string IsColumnNamesInFirstRow bool programName string @@ -56,6 +57,7 @@ func covertDateFormat(dateFormat string) string { func readCommandLineArguments(config *Configuration, args []string) (fs *flag.FlagSet, err error) { fs = flag.NewFlagSet("", flag.ContinueOnError) fs.BoolVar(&config.printUsage, "h", false, "print usage") + fs.StringVar(&config.OutputFile, "o", "", "output file name") 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") @@ -83,6 +85,10 @@ func readCommandLineArguments(config *Configuration, args []string) (fs *flag.Fl case 0 < fs.NArg(): config.InputFiles = fs.Args() default: + config.InputFiles = []string{""} + } + + if len(config.OutputFile) != 0 && 1 < len(config.InputFiles) { config.printUsage = true } diff --git a/statecontext/config_test.go b/statecontext/config_test.go index a1f1cae..bcf594e 100644 --- a/statecontext/config_test.go +++ b/statecontext/config_test.go @@ -23,13 +23,15 @@ func Test_readCommandLineArguments(t *testing.T) { printUsage: false}, errEmptyArgumentList}}, {"test 1", []string{"programname"}, want{Configuration{ + InputFiles: []string{""}, DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, Delimiter: " ", programName: "programname", - printUsage: true}, nil}}, + printUsage: false}, nil}}, {"help", []string{"programname", "-h"}, want{Configuration{ + InputFiles: []string{""}, DateFormat: "YYYYMMDDHHmmSS", DateColumn: 1, Delimiter: " ", @@ -37,18 +39,20 @@ func Test_readCommandLineArguments(t *testing.T) { printUsage: true}, nil}}, {"dateFormat", []string{"programname", "-t", "YYMMDDHHmm"}, want{Configuration{ + InputFiles: []string{""}, DateFormat: "YYMMDDHHmm", DateColumn: 1, Delimiter: " ", programName: "programname", - printUsage: true}, nil}}, + printUsage: false}, nil}}, {"dateColumn", []string{"programname", "-tc", "2"}, want{Configuration{ + InputFiles: []string{""}, DateFormat: "YYYYMMDDHHmmSS", DateColumn: 2, Delimiter: " ", programName: "programname", - printUsage: true}, nil}}, + printUsage: false}, nil}}, {"test 2", []string{"programname", "what"}, want{Configuration{ InputFiles: []string{"what"}, diff --git a/toGraph.go b/toGraph.go index d0a4192..276aefd 100644 --- a/toGraph.go +++ b/toGraph.go @@ -31,18 +31,41 @@ func main() { func run() { - for _, fileName := range state.Config().InputFiles { - inputFile, err := os.Open(fileName) - checkErr(err) + config := state.Config() + for _, fileName := range config.InputFiles { + inputFile := getInputStream(fileName) + outputFile, outName := getOutputStream(fileName, config) - outFilePath, outFileName := getOutFileName(fileName) + log.Printf("file being processed: %s", fileName) + processFile(inputFile, outputFile, config, outName) + } +} - outputFile, err := os.OpenFile(outFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0660) - checkErr(err) +func getInputStream(fileName string) io.Reader { + if fileName == "" { + return os.Stdin + } - log.Printf("file being processed: %s", fileName) - processFile(inputFile, outputFile, state.Config(), outFileName) + inputFile, err := os.Open(fileName) + checkErr(err) + + return inputFile +} + +func getOutputStream(fileName string, config state.Configuration) (io.Writer, string) { + var outputName string + var outputPath string + + if len(config.OutputFile) == 0 { + outputPath, outputName = getOutFileName(fileName) + } else { + _, outputName = getOutFileName(config.OutputFile) + outputPath = config.OutputFile } + outputFile, err := os.OpenFile(outputPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0660) + checkErr(err) + + return outputFile, outputName } func getOutFileName(fileName string) (outFilePath string, outFileName string) {