Skip to content

Commit

Permalink
packer: make Ui implementations match new SDK's
Browse files Browse the repository at this point in the history
The SDK's Ui interface has had some additions recently for supporting
format-strings and their arguments as paramereters to Say, Ask and
Error.

These changes to the interface means that all the implementations of
that interface need to match the definition, so in this case the
implementations we had in packer/ui.go need to be updated to support
those functions.
  • Loading branch information
lbajolet-hashicorp committed Apr 16, 2024
1 parent 0b00f52 commit 33d9550
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/hashicorp/hcl/v2 v2.19.1
github.com/hashicorp/hcp-sdk-go v0.90.0
github.com/hashicorp/packer-plugin-amazon v1.2.1
github.com/hashicorp/packer-plugin-sdk v0.5.2
github.com/hashicorp/packer-plugin-sdk v0.5.3
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/pgzip v1.2.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR
github.com/hashicorp/memberlist v0.5.0/go.mod h1:yvyXLpo0QaGE59Y7hDTsTzDD25JYBZ4mHgHUZ8lrOI0=
github.com/hashicorp/packer-plugin-amazon v1.2.1 h1:0Xqr8KsTJJhIo0vvjqPYrVMgyVxNRuYH4DeB5m/WAtw=
github.com/hashicorp/packer-plugin-amazon v1.2.1/go.mod h1:qlp0h5TWVGgcPzN9mSxPiEAwOUOW3XU/zep0pGd0ZsM=
github.com/hashicorp/packer-plugin-sdk v0.5.2 h1:N+9qzkZLkjR0bgY+l5wFvrTN0G/Mf9f7g2yLsrDjmFI=
github.com/hashicorp/packer-plugin-sdk v0.5.2/go.mod h1:0LRUBS6CPClHvq6zPoGRi7C+cZDAIEaW4CeH4LhWWuc=
github.com/hashicorp/packer-plugin-sdk v0.5.3 h1:rWQuRgUUnkf1O3UYymQZz/nJErLh4NFkuarOVEQK+Rs=
github.com/hashicorp/packer-plugin-sdk v0.5.3/go.mod h1:ntdZSJnc1LGGdMEXsYOxopZL1311QOTIbzVZH2EbN0U=
github.com/hashicorp/serf v0.10.1 h1:Z1H2J60yRKvfDYAOZLd2MU0ND4AH/WDz7xYHDWQsIPY=
github.com/hashicorp/serf v0.10.1/go.mod h1:yL2t6BqATOLGc5HF7qbFkTfXoPIY0WZdWHfEvMqbG+4=
github.com/hashicorp/vault/api v1.10.0 h1:/US7sIjWN6Imp4o/Rj1Ce2Nr5bki/AXi9vAW3p2tOJQ=
Expand Down
48 changes: 48 additions & 0 deletions packer/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,18 @@ func (u *ColoredUi) Ask(query string) (string, error) {
return u.Ui.Ask(u.colorize(query, u.Color, true))
}

func (u *ColoredUi) Askf(query string, vals ...any) (string, error) {
return u.Ask(fmt.Sprintf(query, vals...))
}

func (u *ColoredUi) Say(message string) {
u.Ui.Say(u.colorize(message, u.Color, true))
}

func (u *ColoredUi) Sayf(message string, vals ...any) {
u.Say(fmt.Sprintf(message, vals...))
}

func (u *ColoredUi) Message(message string) {
u.Ui.Message(u.colorize(message, u.Color, false))
}
Expand All @@ -64,6 +72,10 @@ func (u *ColoredUi) Error(message string) {
u.Ui.Error(u.colorize(message, color, true))
}

func (u *ColoredUi) Errorf(message string, vals ...any) {
u.Error(fmt.Sprintf(message, vals...))
}

func (u *ColoredUi) Machine(t string, args ...string) {
// Don't colorize machine-readable output
u.Ui.Machine(t, args...)
Expand Down Expand Up @@ -121,10 +133,18 @@ func (u *TargetedUI) Ask(query string) (string, error) {
return u.Ui.Ask(u.prefixLines(true, query))
}

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

func (u *TargetedUI) Say(message string) {
u.Ui.Say(u.prefixLines(true, message))
}

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

func (u *TargetedUI) Message(message string) {
u.Ui.Message(u.prefixLines(false, message))
}
Expand All @@ -133,6 +153,10 @@ func (u *TargetedUI) Error(message string) {
u.Ui.Error(u.prefixLines(true, message))
}

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

func (u *TargetedUI) Machine(t string, args ...string) {
// Prefix in the target, then pass through
u.Ui.Machine(fmt.Sprintf("%s,%s", u.Target, t), args...)
Expand Down Expand Up @@ -170,10 +194,18 @@ func (u *MachineReadableUi) Ask(query string) (string, error) {
return "", errors.New("machine-readable UI can't ask")
}

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

func (u *MachineReadableUi) Say(message string) {
u.Machine("ui", "say", message)
}

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

func (u *MachineReadableUi) Message(message string) {
u.Machine("ui", "message", message)
}
Expand All @@ -182,6 +214,10 @@ func (u *MachineReadableUi) Error(message string) {
u.Machine("ui", "error", message)
}

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

func (u *MachineReadableUi) Machine(category string, args ...string) {
now := time.Now().UTC()

Expand Down Expand Up @@ -232,10 +268,18 @@ func (u *TimestampedUi) Ask(query string) (string, error) {
return u.Ui.Ask(query)
}

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

func (u *TimestampedUi) Say(message string) {
u.Ui.Say(u.timestampLine(message))
}

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

func (u *TimestampedUi) Message(message string) {
u.Ui.Message(u.timestampLine(message))
}
Expand All @@ -244,6 +288,10 @@ func (u *TimestampedUi) Error(message string) {
u.Ui.Error(u.timestampLine(message))
}

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

func (u *TimestampedUi) Machine(message string, args ...string) {
u.Ui.Machine(message, args...)
}
Expand Down

0 comments on commit 33d9550

Please sign in to comment.