From e2379a3098c01b0400ff2d46b1398222bb6a36fb Mon Sep 17 00:00:00 2001 From: ayyansea Date: Wed, 4 Dec 2024 21:47:02 +0300 Subject: feat: add InputFile argument and corresponding feature --- cmd/uptfs/main.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/cmd/uptfs/main.go b/cmd/uptfs/main.go index a088b52..9040e0b 100644 --- a/cmd/uptfs/main.go +++ b/cmd/uptfs/main.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "slices" + "strings" "github.com/alexflint/go-arg" "github.com/ayyansea/uptfs/internal/config" @@ -17,6 +18,7 @@ import ( var args struct { ConfigFile string `arg:"-c" help:"path to config file" default:""` Verbose bool `arg:"-v" help:"toggle verbose (debug) mode"` + InputFile string `arg:"-i" help:"name of input file to read text from"` Filter []string `arg:"-f,separate" help:"name of a filter that will be applied to text, can be specified multiple times"` } @@ -45,10 +47,26 @@ func main() { config.Filters = append(config.Filters, args.Filter...) + var scanner *bufio.Scanner + var inputFileData []byte var inputStrings []string - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - inputStrings = append(inputStrings, scanner.Text()) + + if len(args.InputFile) > 0 { + inputFilePath, _ := filepath.Abs(args.InputFile) + inputFileData, err = os.ReadFile(inputFilePath) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + scanner = bufio.NewScanner(strings.NewReader(string(inputFileData))) + for scanner.Scan() { + inputStrings = append(inputStrings, scanner.Text()) + } + } else { + scanner = bufio.NewScanner(os.Stdin) + for scanner.Scan() { + inputStrings = append(inputStrings, scanner.Text()) + } } additionalDelimeters := []string{",", ".", " "} -- cgit v1.2.3