Skip to content

Commit 2e5db6c

Browse files
authored
refactor(terminal): replace IsInteractiveTerminal with IsNonInteractiveTerminal (#555)
1 parent 6a1b14c commit 2e5db6c

File tree

8 files changed

+18
-14
lines changed

8 files changed

+18
-14
lines changed

cmd/commands/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func installCmd(ctx context.Context, cmd *cli.Command) error {
9797
var resolvedVersion = manager.ResolveVersion(sdk.Name, version)
9898
logger.Debugf("resolved version: %s\n", resolvedVersion)
9999
if resolvedVersion == "" {
100-
if !internal.IsInteractiveTerminal() {
100+
if util.IsNonInteractiveTerminal() {
101101
return cli.Exit(fmt.Sprintf("install requires specifying a version for %s", name), 1)
102102
}
103103
showAvailable, _ := pterm.DefaultInteractiveConfirm.Show(fmt.Sprintf("No %s version provided, do you want to select a version to install?", pterm.Red(name)))
@@ -146,7 +146,7 @@ func installAll(autoConfirm bool) error {
146146
printSdk(sdks, nil)
147147

148148
if !autoConfirm {
149-
if !internal.IsInteractiveTerminal() {
149+
if util.IsNonInteractiveTerminal() {
150150
return cli.Exit("Use the -y flag to automatically confirm installation in non-interactive environments", 1)
151151
}
152152
result, _ := pterm.DefaultInteractiveConfirm.

cmd/commands/remove.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package commands
1818

1919
import (
2020
"context"
21+
2122
"github.com/pterm/pterm"
2223
"github.com/urfave/cli/v3"
2324
"github.com/version-fox/vfox/internal"
25+
"github.com/version-fox/vfox/internal/util"
2426
)
2527

2628
var Remove = &cli.Command{
@@ -50,7 +52,7 @@ func removeCmd(ctx context.Context, cmd *cli.Command) error {
5052
pterm.Println("Removing this plugin will remove the installed sdk along with the plugin.")
5153

5254
if !yes {
53-
if !internal.IsInteractiveTerminal() {
55+
if util.IsNonInteractiveTerminal() {
5456
return cli.Exit("Use the -y flag to skip confirmation in non-interactive environments", 1)
5557
}
5658
result, _ := pterm.DefaultInteractiveConfirm.

cmd/commands/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func RunSearch(sdkName string, availableArgs []string) error {
7979
installedVersions.Add(string(version))
8080
}
8181

82-
if !internal.IsInteractiveTerminal() {
82+
if util.IsNonInteractiveTerminal() {
8383
fmt.Println("Available versions:")
8484
for _, option := range options {
8585
label := option.Value

cmd/commands/use.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"os"
2323
"strings"
2424

25+
"github.com/version-fox/vfox/internal/util"
26+
2527
"github.com/pterm/pterm"
2628
"github.com/version-fox/vfox/internal"
2729
"github.com/version-fox/vfox/internal/base"
@@ -98,7 +100,7 @@ func useCmd(ctx context.Context, cmd *cli.Command) error {
98100
if len(arr) == 0 {
99101
return fmt.Errorf("no versions available for %s", name)
100102
}
101-
if !internal.IsInteractiveTerminal() {
103+
if util.IsNonInteractiveTerminal() {
102104
return cli.Exit("Please specify a version to use in non-interactive environments", 1)
103105
}
104106
selectPrinter := pterm.InteractiveSelectPrinter{

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/yuin/gopher-lua v1.1.1
1717
golang.org/x/crypto v0.35.0
1818
golang.org/x/sys v0.30.0
19+
golang.org/x/term v0.29.0
1920
gopkg.in/yaml.v3 v3.0.1
2021
)
2122

@@ -45,6 +46,5 @@ require (
4546
github.com/yusufpapurcu/wmi v1.2.4 // indirect
4647
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
4748
golang.org/x/net v0.29.0 // indirect
48-
golang.org/x/term v0.29.0 // indirect
4949
golang.org/x/text v0.22.0 // indirect
5050
)

internal/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func (m *Manager) LookupSdkWithInstall(name string, autoConfirm bool) (*Sdk, err
271271
if errors.As(err, &NotFoundError{}) {
272272
if autoConfirm {
273273
fmt.Printf("[%s] not added yet, automatically proceeding with installation.\n", pterm.LightBlue(name))
274-
} else if !IsInteractiveTerminal() {
274+
} else if util.IsNonInteractiveTerminal() {
275275
return nil, cli.Exit(fmt.Sprintf("Plugin %s is not installed. Use the -y flag to automatically install plugins in non-interactive environments", name), 1)
276276
} else {
277277
fmt.Printf("[%s] not added yet, confirm that you want to use [%s]? \n", pterm.LightBlue(name), pterm.LightRed(name))

internal/ci.go renamed to internal/util/ci.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal
1+
package util
22

33
import (
44
"os"
@@ -48,13 +48,13 @@ func isCI() bool {
4848
return false
4949
}
5050

51-
// IsInteractiveTerminal checks if the current environment supports interactive terminal operations.
52-
// Returns false if running in CI or if stdout is not a terminal (e.g., piped output).
53-
func IsInteractiveTerminal() bool {
51+
// IsNonInteractiveTerminal checks if the current environment is non-interactive.
52+
// Returns true if running in CI or if stdout is not a terminal (e.g., piped output).
53+
func IsNonInteractiveTerminal() bool {
5454
if isCI() {
55-
return false
55+
return true
5656
}
57-
return true
57+
return !IsTTY()
5858
}
5959

6060
func isTruthyEnv(value string) bool {

internal/ci_test.go renamed to internal/util/ci_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package internal
1+
package util
22

33
import "testing"
44

0 commit comments

Comments
 (0)