github.com/oam-dev/kubevela@v1.9.11/pkg/utils/util/cmd.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  )
    25  
    26  // IOStreams provides the standard names for iostreams.  This is useful for embedding and for unit testing.
    27  // Inconsistent and different names make it hard to read and review code
    28  type IOStreams struct {
    29  	// In think, os.Stdin
    30  	In io.Reader
    31  	// Out think, os.Stdout
    32  	Out io.Writer
    33  	// ErrOut think, os.Stderr
    34  	ErrOut io.Writer
    35  }
    36  
    37  // Infonln compared to Info(), won't print new line
    38  func (i *IOStreams) Infonln(a ...interface{}) {
    39  	_, _ = fmt.Fprint(i.Out, a...)
    40  }
    41  
    42  // Info print info with new line
    43  func (i *IOStreams) Info(a ...interface{}) {
    44  	_, _ = fmt.Fprintln(i.Out, a...)
    45  }
    46  
    47  // Infof print info in a specified format
    48  func (i *IOStreams) Infof(format string, a ...interface{}) {
    49  	_, _ = fmt.Fprintf(i.Out, format, a...)
    50  }
    51  
    52  // Errorf print error info in a specified format
    53  func (i *IOStreams) Errorf(format string, a ...interface{}) {
    54  	_, _ = fmt.Fprintf(i.ErrOut, format, a...)
    55  }
    56  
    57  // Error print error info
    58  func (i *IOStreams) Error(a ...interface{}) {
    59  	_, _ = fmt.Fprintln(i.ErrOut, a...)
    60  }
    61  
    62  // NewDefaultIOStreams return IOStreams with standard input/output/error
    63  func NewDefaultIOStreams() IOStreams {
    64  	return IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr}
    65  }
    66  
    67  // NewTestIOStreams return IOStreams with empty input and combined buffered output
    68  func NewTestIOStreams() (IOStreams, *bytes.Buffer) {
    69  	var buf bytes.Buffer
    70  	return IOStreams{In: &bytes.Buffer{}, Out: &buf, ErrOut: &buf}, &buf
    71  }