Skip to content

Commit

Permalink
csvtable: Update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed May 19, 2023
1 parent 11eaa8b commit eb71eec
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
67 changes: 47 additions & 20 deletions clitable/cmd/csvtable/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Package csvtable provides a tool to view csv files on the cmdline.
package main

import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -31,7 +33,11 @@ import (

const semVersion = "0.3.0"

var logger = log.New(ioutil.Discard, "main DEBUG ", log.LstdFlags)
var Logger = log.New(ioutil.Discard, "", log.LstdFlags)

func main() {
os.Exit(program(os.Args))
}

func examples() {
fmt.Fprintf(os.Stderr, `EXAMPLES:
Expand All @@ -40,22 +46,26 @@ func examples() {
# Pipe CSV file to csvtable
cat <csv_filename> | csvtable
# Read TSV file
csvtable <tsv_filename> --tsv
`)
}

func main() {
func program(args []string) int {
opt := getoptions.New()
opt.Bool("help", false, opt.Alias("?"))
opt.Bool("debug", false)
opt.Bool("version", false, opt.Alias("V"))
opt.Bool("tsv", false)
header := opt.Bool("no-header", true)
opt.HelpSynopsisArgs("<csv_filename>")
opt.Bool("no-header", true)
opt.HelpSynopsisArg("<filename>", "CSV|TSV file to read")

opt.SetCommandFn(Run)
opt.HelpCommand("help", opt.Alias("?"))
remaining, err := opt.Parse(os.Args[1:])
if opt.Called("help") {
fmt.Fprint(os.Stderr, opt.Help())
examples()
os.Exit(1)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
Expand All @@ -71,13 +81,31 @@ func main() {
os.Exit(0)
}
if opt.Called("debug") {
logger.SetOutput(os.Stderr)
Logger.SetOutput(os.Stderr)
clitable.Logger.SetOutput(os.Stderr)
}
logger.Println(remaining)
Logger.Println(remaining)

ctx, cancel, done := getoptions.InterruptContext()
defer func() { cancel(); <-done }()

err = opt.Dispatch(ctx, remaining)
if err != nil {
if errors.Is(err, getoptions.ErrorHelpCalled) {
examples()
return 1
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
return 1
}
return 0
}

func Run(ctx context.Context, opt *getoptions.GetOpt, args []string) error {
header := opt.Value("no-header").(bool)

var reader io.Reader
if len(remaining) < 1 {
if len(args) < 1 {
// Check if stdin is pipe p or device D
statStdin, _ := os.Stdin.Stat()
stdinIsDevice := (statStdin.Mode() & os.ModeDevice) != 0
Expand All @@ -87,15 +115,14 @@ func main() {
examples()
os.Exit(1)
}
logger.Printf("Reading from stdin\n")
Logger.Printf("Reading from stdin\n")
reader = os.Stdin
} else {
filename := remaining[0]
logger.Printf("Reading from file %s\n", filename)
filename := args[0]
Logger.Printf("Reading from file %s\n", filename)
fh, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
return err
}
defer fh.Close()
reader = fh
Expand All @@ -104,11 +131,11 @@ func main() {
if opt.Called("tsv") {
tp.Separator('\t')
}
err = tp.HasHeader(*header).FprintCSVReader(os.Stdout, reader)
err := tp.HasHeader(header).FprintCSVReader(os.Stdout, reader)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
return err
}
return nil
}

func version(semVersion string) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion clitable/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module github.com/DavidGamba/dgtools/clitable

go 1.18

require github.com/DavidGamba/go-getoptions v0.25.3
require github.com/DavidGamba/go-getoptions v0.27.0

require golang.org/x/text v0.9.0
4 changes: 2 additions & 2 deletions clitable/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/DavidGamba/go-getoptions v0.25.3 h1:lSPcMkwWvVZU05C+Uz4DKnKN5wz4bcD1QvJ/QHCRexo=
github.com/DavidGamba/go-getoptions v0.25.3/go.mod h1:qLaLSYeQ8sUVOfKuu5JT5qKKS3OCwyhkYSJnoG+ggmo=
github.com/DavidGamba/go-getoptions v0.27.0 h1:hldKJSwO9SwvR+z9pe6ojhEcYECrRiO/bar9B7MnBKA=
github.com/DavidGamba/go-getoptions v0.27.0/go.mod h1:qLaLSYeQ8sUVOfKuu5JT5qKKS3OCwyhkYSJnoG+ggmo=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=

0 comments on commit eb71eec

Please sign in to comment.