Skip to content

Commit

Permalink
Expose a public plugin.NewClientUI()
Browse files Browse the repository at this point in the history
This would allow `age` library users to create the needed inputs to interact with the `plugin` module.
  • Loading branch information
nicdumz committed Jan 13, 2025
1 parent b9e3d10 commit 97d00f4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
56 changes: 1 addition & 55 deletions cmd/age/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ package main

import (
"bytes"
"errors"
"fmt"
"io"
"os"

"filippo.io/age/armor"
"filippo.io/age/internal/logger"
"filippo.io/age/internal/term"
"filippo.io/age/plugin"
)
Expand All @@ -33,59 +31,7 @@ func printfToTerminal(format string, v ...interface{}) error {
})
}

var pluginTerminalUI = &plugin.ClientUI{
DisplayMessage: func(name, message string) error {
logger.Global.Printf("%s plugin: %s", name, message)
return nil
},
RequestValue: func(name, message string, _ bool) (s string, err error) {
defer func() {
if err != nil {
logger.Global.Warningf("could not read value for age-plugin-%s: %v", name, err)
}
}()
secret, err := term.ReadSecret(message)
if err != nil {
return "", err
}
return string(secret), nil
},
Confirm: func(name, message, yes, no string) (choseYes bool, err error) {
defer func() {
if err != nil {
logger.Global.Warningf("could not read value for age-plugin-%s: %v", name, err)
}
}()
if no == "" {
message += fmt.Sprintf(" (press enter for %q)", yes)
_, err := term.ReadSecret(message)
if err != nil {
return false, err
}
return true, nil
}
message += fmt.Sprintf(" (press [1] for %q or [2] for %q)", yes, no)
for {
selection, err := term.ReadCharacter(message)
if err != nil {
return false, err
}
switch selection {
case '1':
return true, nil
case '2':
return false, nil
case '\x03': // CTRL-C
return false, errors.New("user cancelled prompt")
default:
logger.Global.Warningf("reading value for age-plugin-%s: invalid selection %q", name, selection)
}
}
},
WaitTimer: func(name string) {
logger.Global.Printf("waiting on %s plugin...", name)
},
}
var pluginTerminalUI = plugin.NewClientUI()

func bufferTerminalInput(in io.Reader) (io.Reader, error) {
buf := &bytes.Buffer{}
Expand Down
59 changes: 59 additions & 0 deletions plugin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package plugin

import (
"bufio"
"errors"
"fmt"
"io"
"math/rand"
Expand All @@ -22,6 +23,8 @@ import (

"filippo.io/age"
"filippo.io/age/internal/format"
"filippo.io/age/internal/logger"
"filippo.io/age/internal/term"
)

type Recipient struct {
Expand Down Expand Up @@ -321,6 +324,62 @@ type ClientUI struct {
WaitTimer func(name string)
}

func NewClientUI() *ClientUI {
return &ClientUI{
DisplayMessage: func(name, message string) error {
logger.Global.Printf("%s plugin: %s", name, message)
return nil
},
RequestValue: func(name, message string, _ bool) (s string, err error) {
defer func() {
if err != nil {
logger.Global.Warningf("could not read value for age-plugin-%s: %v", name, err)
}
}()
secret, err := term.ReadSecret(message)
if err != nil {
return "", err
}
return string(secret), nil
},
Confirm: func(name, message, yes, no string) (choseYes bool, err error) {
defer func() {
if err != nil {
logger.Global.Warningf("could not read value for age-plugin-%s: %v", name, err)
}
}()
if no == "" {
message += fmt.Sprintf(" (press enter for %q)", yes)
_, err := term.ReadSecret(message)
if err != nil {
return false, err
}
return true, nil
}
message += fmt.Sprintf(" (press [1] for %q or [2] for %q)", yes, no)
for {
selection, err := term.ReadCharacter(message)
if err != nil {
return false, err
}
switch selection {
case '1':
return true, nil
case '2':
return false, nil
case '\x03': // CTRL-C
return false, errors.New("user cancelled prompt")
default:
logger.Global.Warningf("reading value for age-plugin-%s: invalid selection %q", name, selection)
}
}
},
WaitTimer: func(name string) {
logger.Global.Printf("waiting on %s plugin...", name)
},
}
}

func (c *ClientUI) handle(name string, conn *clientConnection, s *format.Stanza) (ok bool, err error) {
switch s.Type {
case "msg":
Expand Down

0 comments on commit 97d00f4

Please sign in to comment.