moved concurrency-safe ui code to packer/ui.go

This commit is contained in:
xinau 2019-02-13 21:01:50 +00:00
parent ae59b81f44
commit b449af84ee
3 changed files with 54 additions and 95 deletions

View File

@ -343,7 +343,7 @@ func (u *MachineReadableUi) ProgressBar() ProgressBar {
return new(NoopProgressBar) return new(NoopProgressBar)
} }
// TimestampedUi is a UI that wraps another UI implementation and prefixes // TimestampedUi is a UI that wraps another UI implementation and
// prefixes each message with an RFC3339 timestamp // prefixes each message with an RFC3339 timestamp
type TimestampedUi struct { type TimestampedUi struct {
Ui Ui Ui Ui
@ -376,3 +376,48 @@ func (u *TimestampedUi) ProgressBar() ProgressBar { return u.Ui.ProgressBar() }
func (u *TimestampedUi) timestampLine(string string) string { func (u *TimestampedUi) timestampLine(string string) string {
return fmt.Sprintf("%v: %v", time.Now().Format(time.RFC3339), string) return fmt.Sprintf("%v: %v", time.Now().Format(time.RFC3339), string)
} }
// Safe is a UI that wraps another UI implementation and
// provides concurrency-safe access
type SafeUi struct {
Sem chan int
Ui Ui
}
var _ Ui = new(SafeUi)
func (u *SafeUi) Ask(s string) (string, error) {
u.Sem <- 1
ret, err := u.Ui.Ask(s)
<-u.Sem
return ret, err
}
func (u *SafeUi) Say(s string) {
u.Sem <- 1
u.Ui.Say(s)
<-u.Sem
}
func (u *SafeUi) Message(s string) {
u.Sem <- 1
u.Ui.Message(s)
<-u.Sem
}
func (u *SafeUi) Error(s string) {
u.Sem <- 1
u.Ui.Error(s)
<-u.Sem
}
func (u *SafeUi) Machine(t string, args ...string) {
u.Sem <- 1
u.Ui.Machine(t, args...)
<-u.Sem
}
func (u *SafeUi) ProgressBar() ProgressBar {
return new(NoopProgressBar)
}

View File

@ -286,7 +286,10 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return err return err
} }
ui = newUi(ui) ui = &packer.SafeUi{
Sem: make(chan int, 1),
Ui: ui,
}
p.adapter = adapter.NewAdapter(p.done, localListener, config, p.config.SFTPCmd, ui, comm) p.adapter = adapter.NewAdapter(p.done, localListener, config, p.config.SFTPCmd, ui, comm)
defer func() { defer func() {
@ -557,49 +560,3 @@ func getWinRMPassword(buildName string) string {
packer.LogSecretFilter.Set(winRMPass) packer.LogSecretFilter.Set(winRMPass)
return winRMPass return winRMPass
} }
// Ui provides concurrency-safe access to packer.Ui.
type Ui struct {
sem chan int
ui packer.Ui
}
func newUi(ui packer.Ui) packer.Ui {
return &Ui{sem: make(chan int, 1), ui: ui}
}
func (ui *Ui) Ask(s string) (string, error) {
ui.sem <- 1
ret, err := ui.ui.Ask(s)
<-ui.sem
return ret, err
}
func (ui *Ui) Say(s string) {
ui.sem <- 1
ui.ui.Say(s)
<-ui.sem
}
func (ui *Ui) Message(s string) {
ui.sem <- 1
ui.ui.Message(s)
<-ui.sem
}
func (ui *Ui) Error(s string) {
ui.sem <- 1
ui.ui.Error(s)
<-ui.sem
}
func (ui *Ui) Machine(t string, args ...string) {
ui.sem <- 1
ui.ui.Machine(t, args...)
<-ui.sem
}
func (ui *Ui) ProgressBar() packer.ProgressBar {
return new(packer.NoopProgressBar)
}

View File

@ -279,7 +279,10 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return err return err
} }
ui = newUi(ui) ui = &packer.SafeUi{
Sem: make(chan int, 1),
Ui: ui,
}
p.adapter = adapter.NewAdapter(p.done, localListener, config, "", ui, comm) p.adapter = adapter.NewAdapter(p.done, localListener, config, "", ui, comm)
defer func() { defer func() {
@ -519,49 +522,3 @@ func newSigner(privKeyFile string) (*signer, error) {
return signer, nil return signer, nil
} }
// Ui provides concurrency-safe access to packer.Ui.
type Ui struct {
sem chan int
ui packer.Ui
}
func newUi(ui packer.Ui) packer.Ui {
return &Ui{sem: make(chan int, 1), ui: ui}
}
func (ui *Ui) Ask(s string) (string, error) {
ui.sem <- 1
ret, err := ui.ui.Ask(s)
<-ui.sem
return ret, err
}
func (ui *Ui) Say(s string) {
ui.sem <- 1
ui.ui.Say(s)
<-ui.sem
}
func (ui *Ui) Message(s string) {
ui.sem <- 1
ui.ui.Message(s)
<-ui.sem
}
func (ui *Ui) Error(s string) {
ui.sem <- 1
ui.ui.Error(s)
<-ui.sem
}
func (ui *Ui) Machine(t string, args ...string) {
ui.sem <- 1
ui.ui.Machine(t, args...)
<-ui.sem
}
func (ui *Ui) ProgressBar() packer.ProgressBar {
return new(packer.NoopProgressBar)
}