From 2b5852aabe0dda4c541e02bd3731293d420533d0 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Fri, 17 Oct 2025 09:39:03 +0200 Subject: [PATCH 1/7] Add gitignore rule --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 20e1d72..9644cca 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.go-version \ No newline at end of file +build/ +.go-version From 96a83a62e948ef4aa0bccdbbfc763a963974c3a6 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Fri, 17 Oct 2025 09:39:25 +0200 Subject: [PATCH 2/7] Add arg to write to file --- args.go | 7 ++++--- convert.go | 7 +++++-- main.go | 7 +++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/args.go b/args.go index 5298add..bda5a28 100644 --- a/args.go +++ b/args.go @@ -9,9 +9,10 @@ const ( ) type Args struct { - Files []string - Delim string - Pad int + Files []string + Delim string + Pad int + OutputFile string } var Delims = []string{",", ";"} diff --git a/convert.go b/convert.go index c89acfa..70b400f 100644 --- a/convert.go +++ b/convert.go @@ -19,8 +19,11 @@ func ConvertAll(args *Args) error { if err != nil { return err } - // print markdown - fmt.Println(md) + if args.OutputFile == "" { + fmt.Println(md) + } else { + os.WriteFile(args.OutputFile, []byte(md), 0644) + } } return nil } diff --git a/main.go b/main.go index d9c3822..f19d96c 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,13 @@ func main() { Usage: "CSV delimiter, expected values: ',', ';'.", Destination: &args.Delim, }, + &cli.StringFlag{ + Name: "output", + Aliases: []string{"o"}, + Value: "", + Usage: "Write output to file", + Destination: &args.OutputFile, + }, }, Action: func(c *cli.Context) error { args.Files = c.Args().Slice() From 8baf3cacece49b2026c9d66b0287de98282debcb Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Fri, 17 Oct 2025 09:42:02 +0200 Subject: [PATCH 3/7] Replace unnecessary loop --- convert.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/convert.go b/convert.go index 70b400f..be413de 100644 --- a/convert.go +++ b/convert.go @@ -77,9 +77,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 } From e3159428aa81c194c38ab2571bda4233a90848fb Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Fri, 17 Oct 2025 09:46:48 +0200 Subject: [PATCH 4/7] Fix print style functions where not necessary --- args.go | 2 +- args_test.go | 4 ++-- convert_test.go | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/args.go b/args.go index bda5a28..3c40c5c 100644 --- a/args.go +++ b/args.go @@ -33,7 +33,7 @@ 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 } diff --git a/args_test.go b/args_test.go index a182bdb..23a9d72 100644 --- a/args_test.go +++ b/args_test.go @@ -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) } }) @@ -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) } }) diff --git a/convert_test.go b/convert_test.go index 122de5b..70a357e 100644 --- a/convert_test.go +++ b/convert_test.go @@ -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 { @@ -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 { @@ -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 { @@ -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 { From 787501780516737dbd62925dcd14e91ca407fae6 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Fri, 17 Oct 2025 09:47:24 +0200 Subject: [PATCH 5/7] Fix error strings to meet conventions --- args.go | 4 ++-- convert.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/args.go b/args.go index 3c40c5c..21da8f9 100644 --- a/args.go +++ b/args.go @@ -40,14 +40,14 @@ func (args *Args) validateFiles() error { 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 } diff --git a/convert.go b/convert.go index be413de..7f4f1df 100644 --- a/convert.go +++ b/convert.go @@ -100,7 +100,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") } } } From b8c50170958012cc8e46c3781c1e5b5ce8d9f7c5 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Tue, 28 Oct 2025 09:35:56 +0100 Subject: [PATCH 6/7] Add force overwrite option --- args.go | 9 +++++---- convert.go | 10 +++++++++- main.go | 9 ++++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/args.go b/args.go index 21da8f9..5eae1f5 100644 --- a/args.go +++ b/args.go @@ -9,10 +9,11 @@ const ( ) type Args struct { - Files []string - Delim string - Pad int - OutputFile string + Files []string + Delim string + Pad int + OutputFile string + ForceOverwrite bool } var Delims = []string{",", ";"} diff --git a/convert.go b/convert.go index 7f4f1df..095a10c 100644 --- a/convert.go +++ b/convert.go @@ -2,6 +2,7 @@ package main import ( "encoding/csv" + "errors" "fmt" "os" "strings" @@ -22,7 +23,14 @@ func ConvertAll(args *Args) error { if args.OutputFile == "" { fmt.Println(md) } else { - os.WriteFile(args.OutputFile, []byte(md), 0644) + _, 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 diff --git a/main.go b/main.go index f19d96c..6c7e985 100644 --- a/main.go +++ b/main.go @@ -34,9 +34,16 @@ func main() { Name: "output", Aliases: []string{"o"}, Value: "", - Usage: "Write output to file", + Usage: "Write output to file, not overwriting existing file by default", Destination: &args.OutputFile, }, + &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() From af75fa8e3c9b75b29e1c1397a4a361380c6483a8 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Tue, 28 Oct 2025 09:36:18 +0100 Subject: [PATCH 7/7] Remove line from gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9644cca..73731f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -build/ .go-version