Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

fix(weed/shell): suppress prompt when piped (#8990)

Chris Lu7f3908297cb5
diff --git a/weed/command/shell.go b/weed/command/shell.go
index 4053646ad5..f3382a8907 100644
--- a/weed/command/shell.go
+++ b/weed/command/shell.go
@@ -15,6 +15,7 @@ var (
shellOptions shell.ShellOptions
shellInitialFiler *string
shellCluster *string
+ shellDebug *bool
)
func init() {
@@ -23,6 +24,7 @@ func init() {
shellOptions.FilerGroup = cmdShell.Flag.String("filerGroup", "", "filerGroup for the filers")
shellInitialFiler = cmdShell.Flag.String("filer", "", "filer host and port for initial connection, e.g. localhost:8888")
shellCluster = cmdShell.Flag.String("cluster", "", "cluster defined in shell.toml")
+ shellDebug = cmdShell.Flag.Bool("debug", false, "print informational logs to stderr")
}
var cmdShell = &Command{
@@ -61,7 +63,10 @@ func runShell(command *Command, args []string) bool {
filerAddress = viper.GetString("cluster." + cluster + ".filer")
}
shellOptions.FilerAddress = pb.ServerAddress(filerAddress)
- fmt.Fprintf(os.Stderr, "master: %s filer: %s\n", *shellOptions.Masters, shellOptions.FilerAddress)
+ shellOptions.Debug = *shellDebug
+ if shellOptions.Debug {
+ fmt.Fprintf(os.Stderr, "master: %s filer: %s\n", *shellOptions.Masters, shellOptions.FilerAddress)
+ }
shell.RunShell(shellOptions)
diff --git a/weed/shell/commands.go b/weed/shell/commands.go
index 6679c15c90..9973ee6507 100644
--- a/weed/shell/commands.go
+++ b/weed/shell/commands.go
@@ -28,6 +28,7 @@ type ShellOptions struct {
FilerGroup *string
FilerAddress pb.ServerAddress
Directory string
+ Debug bool
}
type CommandEnv struct {
diff --git a/weed/shell/shell_liner.go b/weed/shell/shell_liner.go
index cc17e80f26..aaeaf6ae25 100644
--- a/weed/shell/shell_liner.go
+++ b/weed/shell/shell_liner.go
@@ -18,30 +18,40 @@ import (
"github.com/seaweedfs/seaweedfs/weed/util/grace"
"github.com/peterh/liner"
+ flag "github.com/seaweedfs/seaweedfs/weed/util/fla9"
+ "golang.org/x/term"
)
-var (
- line *liner.State
- historyPath = path.Join(os.TempDir(), "weed-shell")
-)
+var historyPath = path.Join(os.TempDir(), "weed-shell")
func RunShell(options ShellOptions) {
slices.SortFunc(Commands, func(a, b command) int {
return strings.Compare(a.Name(), b.Name())
})
- line = liner.NewLiner()
- defer line.Close()
- grace.OnInterrupt(func() {
- line.Close()
- })
- line.SetCtrlCAborts(true)
- line.SetTabCompletionStyle(liner.TabPrints)
+ if !options.Debug {
+ flag.Set("alsologtostderr", "false")
+ flag.Set("logtostderr", "false")
+ }
+
+ interactive := liner.TerminalSupported() && term.IsTerminal(int(os.Stdin.Fd())) && term.IsTerminal(int(os.Stdout.Fd()))
+
+ var line *liner.State
+ if interactive {
+ line = liner.NewLiner()
+ defer line.Close()
+ grace.OnInterrupt(func() {
+ line.Close()
+ })
+
+ line.SetCtrlCAborts(true)
+ line.SetTabCompletionStyle(liner.TabPrints)
- setCompletionHandler()
- loadHistory()
+ setCompletionHandler(line)
+ loadHistory(line)
- defer saveHistory()
+ defer saveHistory(line)
+ }
commandEnv := NewCommandEnv(&options)
@@ -65,15 +75,19 @@ func RunShell(options ShellOptions) {
}
return nil
})
- fmt.Fprintf(os.Stderr, "master: %s ", *options.Masters)
if len(filers) > 0 {
- fmt.Fprintf(os.Stderr, "filers: %v", filers)
commandEnv.option.FilerAddress = filers[rand.IntN(len(filers))]
}
- fmt.Fprintln(os.Stderr)
+ if options.Debug {
+ if len(filers) > 0 {
+ fmt.Fprintf(os.Stderr, "master: %s filers: %v\n", *options.Masters, filers)
+ } else {
+ fmt.Fprintf(os.Stderr, "master: %s\n", *options.Masters)
+ }
+ }
}
- if liner.TerminalSupported() {
+ if interactive {
for {
cmd, err := line.Prompt("> ")
if err != nil {
@@ -228,7 +242,7 @@ func printHelp(cmds []string) {
}
}
-func setCompletionHandler() {
+func setCompletionHandler(line *liner.State) {
line.SetCompleter(func(line string) (c []string) {
for _, i := range Commands {
if strings.HasPrefix(i.Name(), strings.ToLower(line)) {
@@ -239,14 +253,14 @@ func setCompletionHandler() {
})
}
-func loadHistory() {
+func loadHistory(line *liner.State) {
if f, err := os.Open(historyPath); err == nil {
line.ReadHistory(f)
f.Close()
}
}
-func saveHistory() {
+func saveHistory(line *liner.State) {
if f, err := os.Create(historyPath); err != nil {
fmt.Fprintf(os.Stderr, "Error creating history file: %v\n", err)
} else {