Skip to content

Commit

Permalink
v.0.42
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolainp committed Nov 5, 2023
1 parent c39c914 commit c233d43
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
4 changes: 4 additions & 0 deletions datarecord/datarecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
18 changes: 12 additions & 6 deletions statecontext/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions statecontext/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,36 @@ 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: " ",
programName: "programname",
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"},
Expand Down
39 changes: 31 additions & 8 deletions toGraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c233d43

Please sign in to comment.