diff --git a/Makefile b/Makefile index f8c6995..8add8d8 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,20 @@ + +# Copyright © 2022 Thomas von Dein + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + # # no need to modify anything below tool = tablizer diff --git a/cmd/parser.go b/cmd/parser.go index 0b2d6f0..c27566d 100644 --- a/cmd/parser.go +++ b/cmd/parser.go @@ -1,3 +1,20 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + package cmd import ( diff --git a/cmd/printer.go b/cmd/printer.go index 337c1bd..c6e9c3e 100644 --- a/cmd/printer.go +++ b/cmd/printer.go @@ -1,3 +1,20 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + package cmd import ( diff --git a/cmd/processinput.go b/cmd/processinput.go new file mode 100644 index 0000000..4800f45 --- /dev/null +++ b/cmd/processinput.go @@ -0,0 +1,79 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package cmd + +import ( + "errors" + "github.com/alecthomas/repr" + "os" + "strconv" + "strings" +) + +func process(args []string) error { + var pattern string + havefiles := false + + if len(Columns) > 0 { + for _, use := range strings.Split(Columns, ",") { + usenum, err := strconv.Atoi(use) + if err != nil { + die(err) + } + UseColumns = append(UseColumns, usenum) + } + } + + if len(args) > 0 { + if _, err := os.Stat(args[0]); err != nil { + pattern = args[0] + args = args[1:] + } + + if len(args) > 0 { + for _, file := range args { + fd, err := os.OpenFile(file, os.O_RDONLY, 0755) + if err != nil { + die(err) + } + + data := parseFile(fd, pattern) + if Debug { + repr.Print(data) + } + printTable(data) + } + havefiles = true + } + } + + if !havefiles { + stat, _ := os.Stdin.Stat() + if (stat.Mode() & os.ModeCharDevice) == 0 { + data := parseFile(os.Stdin, pattern) + if Debug { + repr.Print(data) + } + printTable(data) + } else { + return errors.New("No file specified and nothing to read on stdin!") + } + } + + return nil +} diff --git a/cmd/root.go b/cmd/root.go index 906103c..002441d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,68 +18,23 @@ package cmd import ( "fmt" - "github.com/alecthomas/repr" "github.com/spf13/cobra" "os" - "strconv" - "strings" ) -var version = "v1.0.0" +var version = "v1.0.1" var rootCmd = &cobra.Command{ Use: "tablizer [regex] [file, ...]", Short: "[Re-]tabularize tabular data", Long: `Manipulate tabular output of other programs`, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if Version { fmt.Printf("This is tablizer version %s\n", version) - return + return nil } - var pattern string - havefiles := false - - if len(Columns) > 0 { - for _, use := range strings.Split(Columns, ",") { - usenum, err := strconv.Atoi(use) - if err != nil { - die(err) - } - UseColumns = append(UseColumns, usenum) - } - } - - if len(args) > 0 { - if _, err := os.Stat(args[0]); err != nil { - pattern = args[0] - args = args[1:] - } - - if len(args) > 0 { - for _, file := range args { - fd, err := os.OpenFile(file, os.O_RDONLY, 0755) - if err != nil { - die(err) - } - - data := parseFile(fd, pattern) - if Debug { - repr.Print(data) - } - printTable(data) - } - havefiles = true - } - } - - if !havefiles { - data := parseFile(os.Stdin, pattern) - if Debug { - repr.Print(data) - } - printTable(data) - } + return process(args) }, } diff --git a/main.go b/main.go index 53c565d..0562bcc 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,20 @@ +/* +Copyright © 2022 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + package main import ( diff --git a/mkrel.sh b/mkrel.sh index 8f1d2cc..c93afa7 100755 --- a/mkrel.sh +++ b/mkrel.sh @@ -1,5 +1,21 @@ #!/bin/bash +# Copyright © 2022 Thomas von Dein + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + # get list with: go tool dist list DIST="darwin/amd64 freebsd/amd64