diff --git a/app/vmctl/main.go b/app/vmctl/main.go index 6fc1c2dd9..a4efd2b6d 100644 --- a/app/vmctl/main.go +++ b/app/vmctl/main.go @@ -15,6 +15,7 @@ import ( "github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/backoff" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/native" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/remoteread" + "github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/terminal" "github.com/urfave/cli/v2" "github.com/VictoriaMetrics/VictoriaMetrics/app/vmctl/influx" @@ -71,7 +72,7 @@ func main() { } otsdbProcessor := newOtsdbProcessor(otsdbClient, importer, c.Int(otsdbConcurrency)) - return otsdbProcessor.run(c.Bool(globalSilent), c.Bool(globalVerbose)) + return otsdbProcessor.run(isNonInteractive(c), c.Bool(globalVerbose)) }, }, { @@ -112,7 +113,7 @@ func main() { c.String(influxMeasurementFieldSeparator), c.Bool(influxSkipDatabaseLabel), c.Bool(influxPrometheusMode)) - return processor.run(c.Bool(globalSilent), c.Bool(globalVerbose)) + return processor.run(isNonInteractive(c), c.Bool(globalVerbose)) }, }, { @@ -152,7 +153,7 @@ func main() { }, cc: c.Int(remoteReadConcurrency), } - return rmp.run(ctx, c.Bool(globalSilent), c.Bool(globalVerbose)) + return rmp.run(ctx, isNonInteractive(c), c.Bool(globalVerbose)) }, }, { @@ -186,7 +187,7 @@ func main() { im: importer, cc: c.Int(promConcurrency), } - return pp.run(c.Bool(globalSilent), c.Bool(globalVerbose)) + return pp.run(isNonInteractive(c), c.Bool(globalVerbose)) }, }, { @@ -244,7 +245,7 @@ func main() { backoff: backoff.New(), cc: c.Int(vmConcurrency), } - return p.run(ctx, c.Bool(globalSilent)) + return p.run(ctx, isNonInteractive(c)) }, }, { @@ -317,3 +318,8 @@ func initConfigVM(c *cli.Context) vm.Config { DisableProgressBar: c.Bool(vmDisableProgressBar), } } + +func isNonInteractive(c *cli.Context) bool { + isTerminal := terminal.IsTerminal(int(os.Stdout.Fd())) + return c.Bool(globalSilent) || !isTerminal +} diff --git a/app/vmctl/terminal/terminal.go b/app/vmctl/terminal/terminal.go new file mode 100644 index 000000000..2f3e26eaa --- /dev/null +++ b/app/vmctl/terminal/terminal.go @@ -0,0 +1,14 @@ +//go:build darwin || linux || solaris +// +build darwin linux solaris + +package terminal + +import ( + "golang.org/x/sys/unix" +) + +// IsTerminal returns true if the file descriptor is terminal +func IsTerminal(fd int) bool { + _, err := unix.IoctlGetTermios(fd, ioctlReadTermios) + return err == nil +} diff --git a/app/vmctl/terminal/unix.go b/app/vmctl/terminal/unix.go new file mode 100644 index 000000000..610e17f8c --- /dev/null +++ b/app/vmctl/terminal/unix.go @@ -0,0 +1,8 @@ +//go:build aix || linux || solaris || zos +// +build aix linux solaris zos + +package terminal + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TCGETS diff --git a/app/vmctl/terminal/unix_bsd.go b/app/vmctl/terminal/unix_bsd.go new file mode 100644 index 000000000..7f866d087 --- /dev/null +++ b/app/vmctl/terminal/unix_bsd.go @@ -0,0 +1,8 @@ +//go:build darwin +// +build darwin + +package terminal + +import "golang.org/x/sys/unix" + +const ioctlReadTermios = unix.TIOCGETA diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 7a0bf17b3..d11189715 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -27,6 +27,7 @@ created by v1.90.0 or newer versions. The solution is to upgrade to v1.90.0 or n * FEATURE: [vmui](https://docs.victoriametrics.com/#vmui): add support for drag'n'drop and paste from clipboard in the "Trace analyzer" page. See [this pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/3971). * FEATURE: [vmui](https://docs.victoriametrics.com/#vmui): Hide messages longer than 3 lines in the trace. You can view the full message by clicking on the `show more` button. See [this pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/3971). * FEATURE: [vmui](https://docs.victoriametrics.com/#vmui): Add the ability to manually input date and time when selecting a time range. See [this pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/3968). +* FEATURE: [vmctl](https://docs.victoriametrics.com/vmctl.html): `vmctl` check whether TTY is available and disable prompt automatically and progress bar when TTY not available. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3823). * BUGFIX: prevent from slow [snapshot creating](https://docs.victoriametrics.com/#how-to-work-with-snapshots) under high data ingestion rate. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3551).