-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command line options to choose input, output, encoder and key-pos…
…itions
- Loading branch information
1 parent
8ae552f
commit 34a2a2c
Showing
3 changed files
with
144 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters