Skip to content

Commit

Permalink
Merge pull request #16 from ItinerisLtd/ui-io
Browse files Browse the repository at this point in the history
  • Loading branch information
tangrufus authored Dec 13, 2020
2 parents 2d73ae8 + 7c38e21 commit f64f26a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func init() {
Short: "Open SFTP connections to Trellis servers",
Args: cobra.RangeArgs(1, 2),
PreRun: func(cmd *cobra.Command, args []string) {
ui := lib.NewUiFromCobraCommand(cmd)
opener.SetUi(ui)
io := lib.NewIoFromCobraCommand(cmd)
opener.SetIo(io)
},
RunE: func(cmd *cobra.Command, args []string) error {
maybeSite := ""
Expand Down
10 changes: 5 additions & 5 deletions cyberduck/opener.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ const cyberduckBookmarkJ2 = `
`

type Opener struct {
ui *lib.Ui
io *lib.Io
}

func NewOpener() *Opener {
return &Opener{
ui: lib.NewUi(),
io: lib.NewIo(),
}
}

func (o *Opener) SetUi(ui *lib.Ui) {
o.ui = ui
func (o *Opener) SetIo(io *lib.Io) {
o.io = io
}

func (o *Opener) Open(path string, environment string, siteName string, isAdmin bool) error {
playbook := lib.NewAdHocPlaybook(map[string]string{
"cyberduck_open.yml": strings.TrimSpace(cyberduckOpenYml) + "\n",
"cyberduck_bookmark.j2": strings.TrimSpace(cyberduckBookmarkJ2) + "\n",
}, path, o.ui)
}, path, o.io)

playbookArgs := []string{
"-e", "dest=" + fmt.Sprintf("%s/cyberduck-%d.duck", path, time.Now().UnixNano()),
Expand Down
8 changes: 4 additions & 4 deletions lib/ad_hoc_playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
type AdHocPlaybook struct {
files map[string]string
root string
ui *Ui
io *Io
}

func NewAdHocPlaybook(files map[string]string, root string, ui *Ui) *AdHocPlaybook {
func NewAdHocPlaybook(files map[string]string, root string, io *Io) *AdHocPlaybook {
return &AdHocPlaybook{
files: files,
root: root,
ui: ui,
io: io,
}
}

Expand All @@ -31,7 +31,7 @@ func (p *AdHocPlaybook) Run(name string, args []string) (err error) {
return err
}

playbook := NewPlaybook(p.root, p.ui)
playbook := NewPlaybook(p.root, p.io)
return playbook.Run(name, args)
}

Expand Down
40 changes: 20 additions & 20 deletions lib/ui.go → lib/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

type Ui struct {
type Io struct {
// inReader is a reader defined by the user that replaces stdin
inReader io.Reader
// outWriter is a writer defined by the user that replaces stdout
Expand All @@ -16,16 +16,16 @@ type Ui struct {
errWriter io.Writer
}

func NewUi() *Ui {
return &Ui{
func NewIo() *Io {
return &Io{
inReader: os.Stdin,
outWriter: os.Stdout,
errWriter: os.Stderr,
}
}

func NewUiFromCobraCommand(cmd *cobra.Command) *Ui {
return &Ui{
func NewIoFromCobraCommand(cmd *cobra.Command) *Io {
return &Io{
inReader: cmd.InOrStdin(),
outWriter: cmd.OutOrStdout(),
errWriter: cmd.ErrOrStderr(),
Expand All @@ -34,42 +34,42 @@ func NewUiFromCobraCommand(cmd *cobra.Command) *Ui {

// SetOut sets the destination for usage messages.
// If newOut is nil, os.Stdout is used.
func (u *Ui) SetOut(newOut io.Writer) {
u.outWriter = newOut
func (i *Io) SetOut(newOut io.Writer) {
i.outWriter = newOut
}

// SetErr sets the destination for error messages.
// If newErr is nil, os.Stderr is used.
func (u *Ui) SetErr(newErr io.Writer) {
u.errWriter = newErr
func (i *Io) SetErr(newErr io.Writer) {
i.errWriter = newErr
}

// SetIn sets the source for input data
// If newIn is nil, os.Stdin is used.
func (u *Ui) SetIn(newIn io.Reader) {
u.inReader = newIn
func (i *Io) SetIn(newIn io.Reader) {
i.inReader = newIn
}

// OutOrStdout returns output to stdout.
func (u *Ui) OutOrStdout() io.Writer {
if u.outWriter != nil {
return u.outWriter
func (i *Io) OutOrStdout() io.Writer {
if i.outWriter != nil {
return i.outWriter
}
return os.Stdout
}

// ErrOrStderr returns output to stderr
func (u *Ui) ErrOrStderr() io.Writer {
if u.errWriter != nil {
return u.errWriter
func (i *Io) ErrOrStderr() io.Writer {
if i.errWriter != nil {
return i.errWriter
}
return os.Stderr
}

// InOrStdin returns input to stdin
func (u *Ui) InOrStdin() io.Reader {
if u.inReader != nil {
return u.inReader
func (i *Io) InOrStdin() io.Reader {
if i.inReader != nil {
return i.inReader
}
return os.Stdin
}
14 changes: 7 additions & 7 deletions lib/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

type Playbook struct {
root string
ui *Ui
io *Io
}

func NewPlaybook(root string, ui *Ui) *Playbook {
func NewPlaybook(root string, io *Io) *Playbook {
return &Playbook{
root: root,
ui: ui,
io: io,
}
}

Expand All @@ -26,13 +26,13 @@ func (p *Playbook) Run(name string, args []string) error {

command.Dir = p.root

command.Stdin = p.ui.InOrStdin()
command.Stdout = p.ui.OutOrStdout()
command.Stderr = p.ui.ErrOrStderr()
command.Stdin = p.io.InOrStdin()
command.Stdout = p.io.OutOrStdout()
command.Stderr = p.io.ErrOrStderr()

command.Env = append(os.Environ(), "ANSIBLE_RETRY_FILES_ENABLED=false")

if _, err := fmt.Fprintf(p.ui.OutOrStdout(), "Running command => ansible-playbook %s\n\n", strings.Join(commandArgs, " ")); err != nil {
if _, err := fmt.Fprintf(p.io.OutOrStdout(), "Running command => ansible-playbook %s\n\n", strings.Join(commandArgs, " ")); err != nil {
return err
}

Expand Down

0 comments on commit f64f26a

Please sign in to comment.