Skip to content

Instantly share code, notes, and snippets.

@markbates
Created March 4, 2022 21:41
Show Gist options
  • Save markbates/2052b33342cb94f6a6bcbfac16f5c69b to your computer and use it in GitHub Desktop.
Save markbates/2052b33342cb94f6a6bcbfac16f5c69b to your computer and use it in GitHub Desktop.

Revisions

  1. markbates created this gist Mar 4, 2022.
    43 changes: 43 additions & 0 deletions io.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    package cli

    import (
    "io"
    "os"
    )

    // IO represents the standard input, output, and error stream.
    type IO struct {
    In io.Reader // standard input
    Out io.Writer // standard output
    Err io.Writer // standard error
    }

    // Stdout returns IO.In.
    // Defaults to os.Stdout.
    func (oi IO) Stdout() io.Writer {
    if oi.Out == nil {
    return os.Stdout
    }

    return oi.Out
    }

    // Stderr returns IO.Err.
    // Defaults to os.Stderr.
    func (oi IO) Stderr() io.Writer {
    if oi.Err == nil {
    return os.Stderr
    }

    return oi.Err
    }

    // Stdin returns IO.In.
    // Defaults to os.Stdin.
    func (oi IO) Stdin() io.Reader {
    if oi.In == nil {
    return os.Stdin
    }

    return oi.In
    }