Skip to content

Commit

Permalink
Add command line options to choose input, output, encoder and key-pos…
Browse files Browse the repository at this point in the history
…itions
  • Loading branch information
aswinkarthik committed Apr 15, 2018
1 parent 8ae552f commit 34a2a2c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 22 deletions.
79 changes: 79 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"fmt"
"io"
"log"
"os"

"github.com/aswinkarthik93/csv-digest/pkg/encoder"
)

var encoders map[string]encoder.Encoder
var config Config

func init() {
encoders = map[string]encoder.Encoder{"json": encoder.JsonEncoder{}}
config = Config{}
}

type Config struct {
KeyPositions []int
Encoder string
Input string
Output string
}

func (c Config) GetKeyPositions() []int {
if len(c.KeyPositions) > 0 {
return c.KeyPositions
}
return []int{0}
}

func (c Config) GetEncoder() encoder.Encoder {
if val, ok := encoders[c.Encoder]; ok {
return val
} else {
fmt.Println("Using JSON encoder")
return encoders["json"]
}
}

func (c Config) GetReader() io.Reader {
if c.Input != "STDIN" && c.Input != "-" && c.Input != "" {
file, err := os.Open(c.Input)

if err != nil {
log.Fatal(err)
}

return file
}
return os.Stdin
}

func (c Config) GetWriter() io.Writer {
if c.Output != "STDOUT" && c.Output != "-" && c.Output != "" {
file, err := os.Create(c.Output)

if err != nil {
log.Fatal(err)
}

return file
}
return os.Stdout
}

func GetEncoders() []string {
result := make([]string, len(encoders))

counter := 0
for k := range encoders {
result[counter] = k
counter++
}

return result
}
41 changes: 41 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"os"
"testing"

"github.com/aswinkarthik93/csv-digest/pkg/encoder"
"github.com/stretchr/testify/assert"
)

func TestGetEncoder(t *testing.T) {
config := Config{Encoder: "json"}
assert.Equal(t, encoder.JsonEncoder{}, config.GetEncoder())

config = Config{Encoder: "random"}
assert.Equal(t, encoder.JsonEncoder{}, config.GetEncoder())
}

func TestGetKeyPositions(t *testing.T) {
config := Config{KeyPositions: []int{0, 1}}
assert.Equal(t, []int{0, 1}, config.GetKeyPositions())

config = Config{KeyPositions: []int{}}
assert.Equal(t, []int{0}, config.GetKeyPositions())
}

func TestReader(t *testing.T) {
config := Config{Input: "STDIN"}
assert.Equal(t, os.Stdin, config.GetReader())

config = Config{Input: "-"}
assert.Equal(t, os.Stdin, config.GetReader())
}

func TestWriter(t *testing.T) {
config := Config{Input: "STDOUT"}
assert.Equal(t, os.Stdout, config.GetWriter())

config = Config{Input: "-"}
assert.Equal(t, os.Stdout, config.GetWriter())
}
46 changes: 24 additions & 22 deletions cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package cmd

import (
"errors"
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/aswinkarthik93/csv-digest/pkg/digest"
"github.com/aswinkarthik93/csv-digest/pkg/encoder"
"github.com/spf13/cobra"
)

Expand All @@ -31,36 +31,32 @@ var digestCmd = &cobra.Command{
Long: `Takes a Csv file and creates a digest for each line.
The tool can output to stdout or a file in plaintext.
It can also serialize the output as a binary file for any other go program to consume directly`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
return nil
} else if len(args) > 1 {
return errors.New("requires exactly one arg - the csv file")
}
return errors.New("requires atleast one arg - the csv file")
},
Run: func(cmd *cobra.Command, args []string) {
runDigest(args[0])
runDigest()
},
}

func runDigest(csvFile string) {
config := digest.DigestConfig{
KeyPositions: primaryKeyPositions(),
Encoder: encoder.JsonEncoder{},
Reader: os.Stdin,
Writer: os.Stdout,
func runDigest() {
if str, err := json.Marshal(config); err == nil && debug {
fmt.Println(string(str))
} else if err != nil {
log.Fatal(err)
}

digestConfig := digest.DigestConfig{
KeyPositions: config.GetKeyPositions(),
Encoder: config.GetEncoder(),
Reader: config.GetReader(),
Writer: config.GetWriter(),
}

err := digest.DigestForFile(config)
err := digest.DigestForFile(digestConfig)
if err != nil {
log.Fatal(err)
}
}

func primaryKeyPositions() []int {
return []int{0}
}
var debug bool

func init() {
rootCmd.AddCommand(digestCmd)
Expand All @@ -73,4 +69,10 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// digestCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

digestCmd.Flags().StringVarP(&config.Input, "input", "i", "STDIN", "Input csv file to read from")
digestCmd.Flags().StringVarP(&config.Output, "output", "o", "STDOUT", "Digest filename to save to")
digestCmd.Flags().StringVarP(&config.Encoder, "encoder", "e", "json", "Encoder to use to output the digest. Available Encoders: "+strings.Join(GetEncoders(), ","))
digestCmd.Flags().IntSliceVarP(&config.KeyPositions, "key-positions", "k", []int{0}, "Primary key positions of the Input CSV as comma separated values Eg: 1,2")
digestCmd.Flags().BoolVarP(&debug, "debug", "", false, "Debug mode")
}

0 comments on commit 34a2a2c

Please sign in to comment.