Skip to content

Commit

Permalink
Ui: add format-string alternatives to functions
Browse files Browse the repository at this point in the history
The say/error/ask methods on Ui only accept formatted strings, which is
a bit cumbersome to do at callsite every time we need to print out
something formatted with dynamic information.

To reduce that cumbersomness, we add some convenience alternatives to
the Ui implementations in the SDK, so they now expose Sayf, Askf and
Errorf in addition to the rest.
  • Loading branch information
lbajolet-hashicorp committed Mar 14, 2024
1 parent f9edec7 commit d970fb9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
32 changes: 32 additions & 0 deletions packer/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ type TTY interface {
// is formatted and various levels of output.
type Ui interface {
Ask(string) (string, error)
Askf(string, ...any) (string, error)
Say(string)
Sayf(string, ...any)
Message(string)
Error(string)
Errorf(string, ...any)
Machine(string, ...string)
// TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser)
getter.ProgressTracker
Expand All @@ -52,6 +55,10 @@ type BasicUi struct {

var _ Ui = new(BasicUi)

func (rw *BasicUi) Askf(query string, args ...any) (string, error) {
return rw.Ask(fmt.Sprintf(query, args...))
}

func (rw *BasicUi) Ask(query string) (string, error) {
rw.l.Lock()
defer rw.l.Unlock()
Expand Down Expand Up @@ -99,6 +106,10 @@ func (rw *BasicUi) Ask(query string) (string, error) {
}
}

func (rw *BasicUi) Sayf(message string, args ...any) {
rw.Say(fmt.Sprintf(message, args...))
}

func (rw *BasicUi) Say(message string) {
rw.l.Lock()
defer rw.l.Unlock()
Expand Down Expand Up @@ -127,6 +138,10 @@ func (rw *BasicUi) Message(message string) {
}
}

func (rw *BasicUi) Errorf(message string, args ...any) {
rw.Error(fmt.Sprintf(message, args...))
}

func (rw *BasicUi) Error(message string) {
rw.l.Lock()
defer rw.l.Unlock()
Expand Down Expand Up @@ -164,6 +179,13 @@ type SafeUi struct {

var _ Ui = new(SafeUi)

func (u *SafeUi) Askf(s string, args ...any) (string, error) {
u.Sem <- 1
ret, err := u.Ui.Askf(s, args...)
<-u.Sem

return ret, err
}
func (u *SafeUi) Ask(s string) (string, error) {
u.Sem <- 1
ret, err := u.Ui.Ask(s)
Expand All @@ -172,6 +194,11 @@ func (u *SafeUi) Ask(s string) (string, error) {
return ret, err
}

func (u *SafeUi) Sayf(s string, args ...any) {
u.Sem <- 1
u.Ui.Sayf(s, args...)
<-u.Sem
}
func (u *SafeUi) Say(s string) {
u.Sem <- 1
u.Ui.Say(s)
Expand All @@ -184,6 +211,11 @@ func (u *SafeUi) Message(s string) {
<-u.Sem
}

func (u *SafeUi) Errorf(s string, args ...any) {
u.Sem <- 1
u.Ui.Errorf(s, args...)
<-u.Sem
}
func (u *SafeUi) Error(s string) {
u.Sem <- 1
u.Ui.Error(s)
Expand Down
10 changes: 10 additions & 0 deletions packer/ui_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package packer

import (
"bytes"
"fmt"
"io"
"testing"
"time"
Expand Down Expand Up @@ -45,12 +46,18 @@ type MockUi struct {
ProgressBarCloseCalled bool
}

func (u *MockUi) Askf(query string, args ...any) (string, error) {
return u.Ask(fmt.Sprintf(query, args...))
}
func (u *MockUi) Ask(query string) (string, error) {
u.AskCalled = true
u.AskQuery = query
return "foo", nil
}

func (u *MockUi) Errorf(message string, args ...any) {
u.Error(fmt.Sprintf(message, args...))
}
func (u *MockUi) Error(message string) {
u.ErrorCalled = true
u.ErrorMessage = message
Expand All @@ -67,6 +74,9 @@ func (u *MockUi) Message(message string) {
u.MessageMessage = message
}

func (u *MockUi) Sayf(message string, args ...any) {
u.Say(fmt.Sprintf(message, args...))
}
func (u *MockUi) Say(message string) {
u.SayCalled = true
sayMessage := SayMessage{
Expand Down
10 changes: 10 additions & 0 deletions rpc/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package rpc

import (
"fmt"
"log"

packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -31,11 +32,17 @@ type UiMachineArgs struct {
Args []string
}

func (u *Ui) Askf(query string, args ...any) (string, error) {
return u.Ask(fmt.Sprintf(query, args...))
}
func (u *Ui) Ask(query string) (result string, err error) {
err = u.client.Call("Ui.Ask", query, &result)
return
}

func (u *Ui) Errorf(message string, args ...any) {
u.Error(fmt.Sprintf(message, args...))
}
func (u *Ui) Error(message string) {
if err := u.client.Call("Ui.Error", message, new(interface{})); err != nil {
log.Printf("Error in Ui.Error RPC call: %s", err)
Expand All @@ -59,6 +66,9 @@ func (u *Ui) Message(message string) {
}
}

func (u *Ui) Sayf(message string, args ...any) {
u.Say(fmt.Sprintf(message, args...))
}
func (u *Ui) Say(message string) {
if err := u.client.Call("Ui.Say", message, new(interface{})); err != nil {
log.Printf("Error in Ui.Say RPC call: %s", err)
Expand Down
10 changes: 10 additions & 0 deletions rpc/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package rpc

import (
"bytes"
"fmt"
"io"
"reflect"
"testing"
Expand All @@ -28,12 +29,18 @@ type testUi struct {
progressBarCloseCalled bool
}

func (u *testUi) Askf(query string, args ...any) (string, error) {
return u.Ask(fmt.Sprintf(query, args...))
}
func (u *testUi) Ask(query string) (string, error) {
u.askCalled = true
u.askQuery = query
return "foo", nil
}

func (u *testUi) Errorf(message string, args ...any) {
u.Error(fmt.Sprintf(message, args...))
}
func (u *testUi) Error(message string) {
u.errorCalled = true
u.errorMessage = message
Expand All @@ -50,6 +57,9 @@ func (u *testUi) Message(message string) {
u.messageMessage = message
}

func (u *testUi) Sayf(message string, args ...any) {
u.Say(fmt.Sprintf(message, args...))
}
func (u *testUi) Say(message string) {
u.sayCalled = true
u.sayMessage = message
Expand Down

0 comments on commit d970fb9

Please sign in to comment.