Skip to content

Commit

Permalink
remove the Output struct
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jul 24, 2024
1 parent cd819e0 commit adeaf74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 29 deletions.
28 changes: 4 additions & 24 deletions cli/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,26 @@ import (
)

var (
defaultOutput = NewOutput()
defaultWriter = os.Stdout
defaultStdoutWriter = os.Stdout
defaultStderrWriter = os.Stderr
)

func DefaultWriter() io.Writer {
return defaultWriter
return defaultStdoutWriter
}

func DefaultStderrWriter() io.Writer {
return defaultStderrWriter
}

func Print(w io.Writer, a ...interface{}) (n int, err error) {
return defaultOutput.Print(w, a...)
}

func Println(w io.Writer, a ...interface{}) (n int, err error) {
return defaultOutput.Println(w, a...)
}

func Printf(w io.Writer, format string, args ...interface{}) (n int, err error) {
return defaultOutput.Printf(w, format, args...)
}

func NewOutput() *Output {
return &Output{}
}

type Output struct {
}

func (o *Output) Print(w io.Writer, a ...interface{}) (n int, err error) {
return fmt.Fprint(w, a...)
}

func (o *Output) Println(w io.Writer, a ...interface{}) (n int, err error) {
func Println(w io.Writer, a ...interface{}) (n int, err error) {
return fmt.Fprintln(w, a...)
}

func (o *Output) Printf(w io.Writer, format string, a ...interface{}) (n int, err error) {
func Printf(w io.Writer, format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(w, format, a...)
}
9 changes: 4 additions & 5 deletions cli/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -44,20 +44,19 @@ func TestDefaultWriter(t *testing.T) {

func TestOutput(t *testing.T) {
w := new(bytes.Buffer)
output := new(Output)
index, err := output.Print(w, "who are you")
index, err := Print(w, "who are you")
assert.Equal(t, 11, index)
assert.Nil(t, err)
assert.Equal(t, "who are you", w.String())

w.Reset()
index, err = output.Println(w, "I am MrX")
index, err = Println(w, "I am MrX")
assert.Equal(t, 9, index)
assert.Nil(t, err)
assert.Equal(t, "I am MrX\n", w.String())

w.Reset()
index, err = output.Printf(w, "and you%s", "?")
index, err = Printf(w, "and you%s", "?")
assert.Equal(t, 8, index)
assert.Nil(t, err)
assert.Equal(t, "and you?", w.String())
Expand Down

0 comments on commit adeaf74

Please sign in to comment.