Skip to content

Commit

Permalink
Merge pull request #1 from joeygibson/print-header
Browse files Browse the repository at this point in the history
added option to print column widths
  • Loading branch information
joeygibson authored Feb 10, 2021
2 parents a710d2b + 777c2aa commit fc0e452
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 34 deletions.
28 changes: 28 additions & 0 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,34 @@ func Cat(file *os.File, writer io.Writer, shouldWriteMetaData bool) error {
return nil
}

func PrintHeader(file *os.File, writer io.Writer) error {
valid, err := ReadSignature(file)
if err != nil {
return err
}

if !valid {
return errors.New(fmt.Sprintf("invalid file signature: %s", file.Name()))
}

definitions, err := ReadColumnDefinitions(file)
if err != nil {
return err
}

for _, width := range definitions.Widths {
if width == math.MaxUint32 {
fmt.Fprintln(writer, -1)
} else {
fmt.Fprintln(writer, width)
}
}

fmt.Fprintf(writer, "\n")

return nil
}

func Head(file *os.File, writer io.Writer, rowsToTake int, shouldWriteMetaData bool) error {
valid, err := ReadSignature(file)
if err != nil {
Expand Down
91 changes: 57 additions & 34 deletions lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"os"
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -46,40 +47,62 @@ func TestCount(t *testing.T) {
}
}

//func TestCat(t *testing.T) {
// file, err := os.Open("../test-data/sample")
// if err != nil {
// t.Fatal("couldn't open file", err)
// }
//
// fileInfo, _ := file.Stat()
// fileInfo.Size()
//
// defer file.Close()
//
// var buf bytes.Buffer
//
// err = Cat(file, &buf, true)
// if err != nil {
// t.Fatal("error reading rows", err)
// }
//
// fullLen := buf.Len()
//
// resetFilePosition(file, 0)
// buf.Reset()
//
// err = Cat(file, &buf, false)
// if err != nil {
// t.Fatal("error reading rows", err)
// }
//
// noMetaLen := buf.Len()
//
// if fullLen == noMetaLen {
// t.Fatal("lengths should differ")
// }
//}
func TestPrintHeader(t *testing.T) {
tests := []struct {
name string
file string
wantLen int
want []string
wantErr bool
}{
{name: "all-types file",
file: "../test-data/all-types.bin",
wantLen: 14,
want: []string{"8", "8", "10", "-1", "1", "8", "8", "8", "8", "8", "-1", "3", "24", "8"},
wantErr: false},
{name: "sample file",
file: "../test-data/sample",
wantLen: 75,
want: []string{"8", "8", "8", "4", "8", "8", "8", "8", "-1", "4", "-1", "8", "-1", "4",
"-1", "8", "-1", "4", "4", "4", "8", "-1", "-1", "4", "4", "8", "8", "4", "4", "4", "4",
"-1", "-1", "4", "4", "8", "8", "4", "4", "4", "4", "4", "4", "8", "8", "-1", "-1", "-1",
"4", "-1", "4", "-1", "4", "4", "-1", "-1", "-1", "-1", "-1", "-1", "-1", "-1", "-1", "-1",
"-1", "-1", "-1", "-1", "2", "2", "2", "2", "4", "4", "1"},
wantErr: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
file, err := os.Open(tt.file)
if err != nil {
t.Fatal("couldn't open file", err)
}

defer file.Close()

var buf bytes.Buffer

err = PrintHeader(file, &buf)
if (err != nil) != tt.wantErr {
t.Errorf("PrintHeader() error = %v, wantErr %v", err, tt.wantErr)
return
}

outStr := strings.TrimSpace(buf.String())
chunks := strings.Split(outStr, "\n")

if len(chunks) != tt.wantLen {
t.Errorf("wrong number of fields; got = %v, want %v", len(chunks), tt.wantLen)
}

for i, exp := range tt.want {
if exp != chunks[i] {
t.Errorf("PrintHeader() got = %v, want %v", chunks[i], exp)
}
}
})
}
}

func TestHead(t *testing.T) {
type args struct {
Expand Down
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
outFileName := getopt.StringLong("output", 'o', "", "write head/tail results to this file")
forceFlag := getopt.BoolLong("force", 'f', "force overwrite of output file")
versionFlag := getopt.BoolLong("version", 'v', "show version")
printHeaderFlag := getopt.BoolLong("print-header", 'p', "print out header and exit")

getopt.SetParameters("[file...]")
getopt.Parse()
Expand Down Expand Up @@ -120,6 +121,13 @@ func main() {
}
} else if *tailRows != 0 {
err := lib.Tail(inputFile, output, *tailRows, firstTime)
if err != nil {
fmt.Fprintln(os.Stderr, "error processing file: ", err)
os.Exit(1)
}
} else if *printHeaderFlag {
err := lib.PrintHeader(inputFile, output)

if err != nil {
fmt.Fprintln(os.Stderr, "error processing file: ", err)
os.Exit(1)
Expand All @@ -145,4 +153,6 @@ func printUsage() {
fmt.Fprintln(os.Stderr, "reading from stdin if no files are given. Since Vertica native files")
fmt.Fprintln(os.Stderr, "start with a metadata header, if you want to cat multiple files together,")
fmt.Fprintln(os.Stderr, "specify them as arguments to verticat itself.")
fmt.Fprintln(os.Stderr, "\nThe -print-header outputs just the column widths for each file. This might")
fmt.Fprintln(os.Stderr, "be useful in determining what an unknown file contains.")
}
Binary file added test-data/all-types.bin
Binary file not shown.

0 comments on commit fc0e452

Please sign in to comment.