• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / cli / 9411926749

07 Jun 2024 04:42AM UTC coverage: 60.071% (-0.009%) from 60.08%
9411926749

Pull #2393

github

nyannyacha
fix: bump edge-runtime to 1.53.4
Pull Request #2393: fix: bump edge-runtime to 1.53.4

6895 of 11478 relevant lines covered (60.07%)

634.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.16
/internal/utils/console.go
1
package utils
2

3
import (
4
        "bufio"
5
        "context"
6
        "fmt"
7
        "io"
8
        "os"
9
        "strings"
10
        "time"
11

12
        "github.com/go-errors/errors"
13
        "golang.org/x/term"
14
)
15

16
type Console struct {
17
        IsTTY  bool
18
        stdin  *bufio.Scanner
19
        logger io.Writer
20
        token  chan string
21
}
22

23
func NewConsole() Console {
34✔
24
        c := Console{
34✔
25
                IsTTY:  term.IsTerminal(int(os.Stdin.Fd())),
34✔
26
                stdin:  bufio.NewScanner(os.Stdin),
34✔
27
                logger: GetDebugLogger(),
34✔
28
                token:  make(chan string),
34✔
29
        }
34✔
30
        go func() {
68✔
31
                // Scan line by line from input or file
34✔
32
                for c.stdin.Scan() {
46✔
33
                        c.token <- strings.TrimSpace(c.stdin.Text())
12✔
34
                }
12✔
35
                if err := c.stdin.Err(); err != nil {
33✔
36
                        fmt.Fprintln(c.logger, err)
×
37
                }
×
38
                close(c.token)
33✔
39
        }()
40
        return c
34✔
41
}
42

43
// PromptYesNo asks yes/no questions using the label.
44
func (c Console) PromptYesNo(ctx context.Context, label string, def bool) (bool, error) {
39✔
45
        choices := "Y/n"
39✔
46
        if !def {
69✔
47
                choices = "y/N"
30✔
48
        }
30✔
49
        labelWithChoice := fmt.Sprintf("%s [%s] ", label, choices)
39✔
50
        // Any error will be handled as default value
39✔
51
        input, err := c.PromptText(ctx, labelWithChoice)
39✔
52
        if len(input) > 0 {
51✔
53
                if answer := parseYesNo(input); answer != nil {
24✔
54
                        return *answer, nil
12✔
55
                }
12✔
56
        }
57
        return def, err
27✔
58
}
59

60
func parseYesNo(s string) *bool {
12✔
61
        s = strings.ToLower(s)
12✔
62
        if s == "y" || s == "yes" {
23✔
63
                return Ptr(true)
11✔
64
        }
11✔
65
        if s == "n" || s == "no" {
2✔
66
                return Ptr(false)
1✔
67
        }
1✔
68
        return nil
×
69
}
70

71
// Prevent interactive terminals from hanging more than 10 minutes
72
const ttyTimeout = time.Minute * 10
73

74
// PromptText asks for input using the label.
75
func (c Console) PromptText(ctx context.Context, label string) (string, error) {
41✔
76
        fmt.Fprint(os.Stderr, label)
41✔
77
        // Wait a few ms for input
41✔
78
        timeout := time.Millisecond
41✔
79
        if c.IsTTY {
41✔
80
                timeout = ttyTimeout
×
81
        }
×
82
        timer := time.NewTimer(timeout)
41✔
83
        defer timer.Stop()
41✔
84
        // Read from stdin
41✔
85
        var input string
41✔
86
        select {
41✔
87
        case input = <-c.token:
40✔
88
        case <-ctx.Done():
×
89
        case <-timer.C:
1✔
90
        }
91
        // Echo to stderr for non-interactive terminals
92
        if !c.IsTTY {
82✔
93
                fmt.Fprintln(os.Stderr, input)
41✔
94
        }
41✔
95
        if err := ctx.Err(); err != nil {
42✔
96
                return "", errors.New(err)
1✔
97
        }
1✔
98
        return input, nil
40✔
99
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc