From a8c5973934331b13415bf954c34fa085bf117677 Mon Sep 17 00:00:00 2001 From: cod3ddy Date: Mon, 1 Jun 2026 23:19:51 +0200 Subject: [PATCH] fix: use golang.org/x/term pkg to properly and easily determine whether the given file descriptor is a terminal or not --- cmd/root.go | 21 ++++++--------------- go.mod | 3 ++- go.sum | 4 ++++ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 104f4fa..ef1790c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -13,6 +13,7 @@ import ( "github.com/cod3ddy/mulonda/internal/prompter" "github.com/cod3ddy/mulonda/internal/watchlist" "github.com/spf13/cobra" + "golang.org/x/term" ) var rootCmd = &cobra.Command{ @@ -135,8 +136,8 @@ func parseGlobalFlags(args []string) (configPath, watchPath string, passthrough continue } - if strings.HasPrefix(token, "--config=") { - configPath = strings.TrimPrefix(token, "--config=") + if v, ok := strings.CutPrefix(token, "--config="); ok { + configPath = v continue } @@ -149,8 +150,8 @@ func parseGlobalFlags(args []string) (configPath, watchPath string, passthrough continue } - if strings.HasPrefix(token, "--watchlist=") { - watchPath = strings.TrimPrefix(token, "--watchlist=") + if v, ok := strings.CutPrefix(token, "--watchlist="); ok { + watchPath = v continue } @@ -174,15 +175,5 @@ func isManagementCommand(name string) bool { } func isInteractiveSession() bool { - stdinInfo, err := os.Stdin.Stat() - if err != nil { - return false - } - - stdoutInfo, err := os.Stdout.Stat() - if err != nil { - return false - } - - return (stdinInfo.Mode()&os.ModeCharDevice) != 0 && (stdoutInfo.Mode()&os.ModeCharDevice) != 0 + return term.IsTerminal(int(os.Stdin.Fd())) && term.IsTerminal(int(os.Stdout.Fd())) } diff --git a/go.mod b/go.mod index db1b67c..742d3b3 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/spf13/pflag v1.0.10 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect - golang.org/x/sys v0.29.0 // indirect + golang.org/x/sys v0.44.0 // indirect + golang.org/x/term v0.43.0 // indirect golang.org/x/text v0.28.0 // indirect ) diff --git a/go.sum b/go.sum index de19da9..73f7750 100644 --- a/go.sum +++ b/go.sum @@ -45,6 +45,10 @@ go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ= +golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4= +golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk= golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng= golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=