Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): Implement terminal wrapping for advisory note input #1430

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions pkg/cli/advisory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"bufio"
"fmt"
"os"
"sort"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/wolfi-dev/wolfictl/pkg/distro"
"github.com/wolfi-dev/wolfictl/pkg/versions"
"github.com/wolfi-dev/wolfictl/pkg/vuln"
"golang.org/x/term"
)

const (
Expand Down Expand Up @@ -90,6 +92,54 @@ func resolveTimestamp(ts string) (v2.Timestamp, error) {
return v2.Timestamp(t), nil
}

// getMultiLineInput is a helper function to get multi-line input from the user
func getMultiLineInput(prompt string) (string, error) {
fmt.Print(prompt)

// Get terminal width
width, _, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
width = 80 // fallback width
}

reader := bufio.NewReader(os.Stdin)
var input strings.Builder
var currentLine strings.Builder
promptWidth := len(prompt)
maxWidth := width - promptWidth

for {
char, _, err := reader.ReadRune()
if err != nil {
break
}

if char == '\n' {
break
}

currentLine.WriteRune(char)

// If we reach the width limit, wrap to next line
if currentLine.Len() >= maxWidth {
input.WriteString(currentLine.String())
input.WriteRune('\n')
fmt.Printf("\n%s", strings.Repeat(" ", promptWidth))
currentLine.Reset()
} else {
fmt.Printf("%c", char)
}
}

// Add any remaining content
if currentLine.Len() > 0 {
input.WriteString(currentLine.String())
}

fmt.Println() // Final newline
return strings.TrimSpace(input.String()), nil
}

type advisoryRequestParams struct {
packageName, vuln, eventType, truePositiveNote, falsePositiveNote, falsePositiveType, timestamp, fixedVersion, note string
}
Expand All @@ -115,6 +165,15 @@ func (p *advisoryRequestParams) advisoryRequest() (advisory.Request, error) {
return advisory.Request{}, fmt.Errorf("unable to process timestamp: %w", err)
}

// If note is empty and we need input, get it via terminal
if p.note == "" {
note, err := getMultiLineInput("Note: ")
if err != nil {
return advisory.Request{}, fmt.Errorf("failed to get note input: %w", err)
}
p.note = note
}

req := advisory.Request{
Package: p.packageName,
Event: v2.Event{
Expand Down