Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.go-version
.go-version
14 changes: 8 additions & 6 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const (
)

type Args struct {
Files []string
Delim string
Pad int
Files []string
Delim string
Pad int
OutputFile string
ForceOverwrite bool
}

var Delims = []string{",", ";"}
Expand All @@ -32,21 +34,21 @@ func (args *Args) ValidateAll() error {

func (args *Args) validateFiles() error {
if len(args.Files) < 1 {
return fmt.Errorf(ERROR_NO_FILENAME)
return fmt.Errorf("%s", ERROR_NO_FILENAME)
}
return nil
}

func (args *Args) validateDelim() error {
if !contains(Delims, args.Delim) {
return fmt.Errorf("Error: delimiter should be one of %+q, but got %q", Delims, args.Delim)
return fmt.Errorf("error: delimiter should be one of %+q, but got %q", Delims, args.Delim)
}
return nil
}

func (args *Args) validatePad() error {
if args.Pad < 0 {
return fmt.Errorf("Error: padding should be positive integer, but got %d", args.Pad)
return fmt.Errorf("error: padding should be positive integer, but got %d", args.Pad)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestArgs(t *testing.T) {
}
err := args.ValidateAll()
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
t.Errorf("Input Args: %+v\n", args)
}
})
Expand All @@ -27,7 +27,7 @@ func TestArgs(t *testing.T) {
}
err := args.ValidateAll()
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
t.Errorf("Input Args: %+v\n", args)
}
})
Expand Down
21 changes: 15 additions & 6 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/csv"
"errors"
"fmt"
"os"
"strings"
Expand All @@ -19,8 +20,18 @@ func ConvertAll(args *Args) error {
if err != nil {
return err
}
// print markdown
fmt.Println(md)
if args.OutputFile == "" {
fmt.Println(md)
} else {
_, err := os.Stat(args.OutputFile)
if errors.Is(err, os.ErrNotExist) || args.ForceOverwrite {
os.WriteFile(args.OutputFile, []byte(md), 0644)
} else {
fmt.Printf(
"skip writing output, file exists: %s\n", args.OutputFile,
)
}
}
}
return nil
}
Expand Down Expand Up @@ -74,9 +85,7 @@ func ArrayToMd(records [][]string, args *Args) (string, error) {
md := []string{}
md = append(md, rows[0]) // header
md = append(md, horiz) // horizontal devider
for _, v := range rows[1:] {
md = append(md, v)
}
md = append(md, rows[1:]...)

return strings.Join(md, "\n"), nil
}
Expand All @@ -99,7 +108,7 @@ func padCells(records [][]string, colSizes []int) ([][]string, error) {
fmt.Println(v)
fmt.Println(colSizes[j])
fmt.Println(utf8.RuneCountInString(v))
return nil, fmt.Errorf("Internal error: column size is bigger than max.")
return nil, fmt.Errorf("internal error: column size is bigger than max")
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestConvert(t *testing.T) {

md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

if md != expected {
Expand All @@ -43,7 +43,7 @@ func TestConvert(t *testing.T) {

md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

if md != expected {
Expand All @@ -64,7 +64,7 @@ func TestConvert(t *testing.T) {

md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

if md != expected {
Expand All @@ -85,7 +85,7 @@ func TestConvert(t *testing.T) {

md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
t.Error(err.Error())
}

if md != expected {
Expand Down
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ func main() {
Usage: "CSV delimiter, expected values: ',', ';'.",
Destination: &args.Delim,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Value: "",
Usage: "Write output to file, not overwriting existing file by default",
Destination: &args.OutputFile,
},
Comment on lines 33 to 39
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about csv2md example.csv > test.txt? Is there any use case where you cannot cope with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there are cases in which I use other tools for CI pipelines for example. Some of them do not support piping because they have their own environments to run shell commands, which sometimes are different from the usual shell in the terminal. I think it does not hurt to have this option and sometimes you will benefit from it.

&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Value: false,
Usage: "Force overwrite, if output file already exists",
Destination: &args.ForceOverwrite,
},
},
Action: func(c *cli.Context) error {
args.Files = c.Args().Slice()
Expand Down