Skip to content

Commit 26dd0ca

Browse files
Consolidate IsTerminal functions
1 parent 1221c03 commit 26dd0ca

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ module github.com/timescale/tiger-cli
33
go 1.25.1
44

55
require (
6+
github.com/Masterminds/semver/v3 v3.4.0
67
github.com/charmbracelet/bubbletea v1.3.10
8+
github.com/cli/safeexec v1.0.1
9+
github.com/fatih/color v1.18.0
710
github.com/google/jsonschema-go v0.2.3
811
github.com/jackc/pgx/v5 v5.7.5
912
github.com/modelcontextprotocol/go-sdk v0.5.0
@@ -28,7 +31,6 @@ require (
2831
al.essio.dev/pkg/shellescape v1.6.0 // indirect
2932
github.com/1password/onepassword-sdk-go v0.3.1 // indirect
3033
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
31-
github.com/Masterminds/semver/v3 v3.4.0 // indirect
3234
github.com/Microsoft/go-winio v0.6.2 // indirect
3335
github.com/adrg/xdg v0.5.3 // indirect
3436
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
@@ -39,7 +41,6 @@ require (
3941
github.com/charmbracelet/x/ansi v0.10.1 // indirect
4042
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
4143
github.com/charmbracelet/x/term v0.2.1 // indirect
42-
github.com/cli/safeexec v1.0.1 // indirect
4344
github.com/containerd/errdefs v1.0.0 // indirect
4445
github.com/containerd/errdefs/pkg v0.3.0 // indirect
4546
github.com/containerd/stargz-snapshotter/estargz v0.16.3 // indirect
@@ -57,7 +58,6 @@ require (
5758
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
5859
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
5960
github.com/extism/go-sdk v1.7.0 // indirect
60-
github.com/fatih/color v1.18.0 // indirect
6161
github.com/felixge/httpsnoop v1.0.4 // indirect
6262
github.com/fsnotify/fsnotify v1.9.0 // indirect
6363
github.com/fxamacker/cbor/v2 v2.9.0 // indirect

internal/tiger/cmd/auth.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"fmt"
66
"os"
77
"strings"
8-
"syscall"
98

109
"github.com/spf13/cobra"
1110
"golang.org/x/term"
1211

1312
"github.com/timescale/tiger-cli/internal/tiger/api"
1413
"github.com/timescale/tiger-cli/internal/tiger/config"
14+
"github.com/timescale/tiger-cli/internal/tiger/util"
1515
)
1616

1717
// validateAPIKeyForLogin can be overridden for testing
@@ -215,7 +215,7 @@ func flagOrEnvVar(flagVal, envVarName string) string {
215215
// promptForCredentials prompts the user to enter any missing credentials
216216
func promptForCredentials(consoleURL string, creds credentials) (credentials, error) {
217217
// Check if we're in a terminal for interactive input
218-
if !term.IsTerminal(int(syscall.Stdin)) {
218+
if !util.IsTerminal(os.Stdin) {
219219
return credentials{}, fmt.Errorf("TTY not detected - credentials required. Use flags (--public-key, --secret-key, --project-id) or environment variables (TIGER_PUBLIC_KEY, TIGER_SECRET_KEY, TIGER_PROJECT_ID)")
220220
}
221221

@@ -236,7 +236,7 @@ func promptForCredentials(consoleURL string, creds credentials) (credentials, er
236236
// Prompt for secret key if missing
237237
if creds.secretKey == "" {
238238
fmt.Print("Enter your secret key: ")
239-
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
239+
bytePassword, err := term.ReadPassword(os.Stdin.Fd())
240240
if err != nil {
241241
return credentials{}, err
242242
}

internal/tiger/cmd/spinner.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package cmd
33
import (
44
"fmt"
55
"io"
6-
"os"
76
"time"
87

98
tea "github.com/charmbracelet/bubbletea"
10-
"golang.org/x/term"
9+
"github.com/timescale/tiger-cli/internal/tiger/util"
1110
)
1211

1312
// spinnerFrames defines the animation frames for the spinner
@@ -28,21 +27,12 @@ type Spinner struct {
2827
// message on a new line without animation. The message parameter supports
2928
// fmt.Sprintf-style formatting with optional args.
3029
func NewSpinner(output io.Writer, message string, args ...any) *Spinner {
31-
if isTerminal(output) {
30+
if util.IsTerminal(output) {
3231
return newAnimatedSpinner(output, message, args...)
3332
}
3433
return newManualSpinner(output, message, args...)
3534
}
3635

37-
// isTerminal is a helper method for detecting whether an [io.Writer] is a
38-
// terminal.
39-
func isTerminal(w io.Writer) bool {
40-
if f, ok := w.(*os.File); ok {
41-
return term.IsTerminal(int(f.Fd()))
42-
}
43-
return false
44-
}
45-
4636
func newAnimatedSpinner(output io.Writer, message string, args ...any) *Spinner {
4737
program := tea.NewProgram(
4838
spinnerModel{

internal/tiger/util/environment.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package util
22

33
import (
4+
"io"
45
"os"
56

6-
"github.com/mattn/go-isatty"
7+
"golang.org/x/term"
78
)
89

9-
// IsTerminal determines if a file descriptor is an interactive terminal / TTY.
10-
func IsTerminal(f *os.File) bool {
11-
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
10+
// IsTerminal is a helper method for detecting whether an [io.Writer] is a
11+
// interactive terminal / TTY.
12+
func IsTerminal(w io.Writer) bool {
13+
if f, ok := w.(*os.File); ok {
14+
return term.IsTerminal(int(f.Fd()))
15+
}
16+
return false
1217
}
1318

1419
// IsCI determines if the current execution context is within a known CI/CD system.

0 commit comments

Comments
 (0)