go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/cli/cli_options.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package cli
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/docker/cli/cli/streams"
    21  	"github.com/moby/term"
    22  
    23  	"go.ligato.io/vpp-agent/v3/cmd/agentctl/client"
    24  )
    25  
    26  // AgentCliOption applies a modification on a AgentCli.
    27  type AgentCliOption func(cli *AgentCli) error
    28  
    29  // WithStandardStreams sets a cli in, out and err streams with the standard streams.
    30  func WithStandardStreams() AgentCliOption {
    31  	return func(cli *AgentCli) error {
    32  		// Set terminal emulation based on platform as required.
    33  		stdin, stdout, stderr := term.StdStreams()
    34  		cli.in = streams.NewIn(stdin)
    35  		cli.out = streams.NewOut(stdout)
    36  		cli.err = stderr
    37  		return nil
    38  	}
    39  }
    40  
    41  // WithCombinedStreams uses the same stream for the output and error streams.
    42  func WithCombinedStreams(combined io.Writer) AgentCliOption {
    43  	return func(cli *AgentCli) error {
    44  		cli.out = streams.NewOut(combined)
    45  		cli.err = combined
    46  		return nil
    47  	}
    48  }
    49  
    50  // WithInputStream sets a cli input stream.
    51  func WithInputStream(in io.ReadCloser) AgentCliOption {
    52  	return func(cli *AgentCli) error {
    53  		cli.in = streams.NewIn(in)
    54  		return nil
    55  	}
    56  }
    57  
    58  // WithOutputStream sets a cli output stream.
    59  func WithOutputStream(out io.Writer) AgentCliOption {
    60  	return func(cli *AgentCli) error {
    61  		cli.out = streams.NewOut(out)
    62  		return nil
    63  	}
    64  }
    65  
    66  // WithErrorStream sets a cli error stream.
    67  func WithErrorStream(err io.Writer) AgentCliOption {
    68  	return func(cli *AgentCli) error {
    69  		cli.err = err
    70  		return nil
    71  	}
    72  }
    73  
    74  // WithClient sets an APIClient.
    75  func WithClient(c client.APIClient) AgentCliOption {
    76  	return func(cli *AgentCli) error {
    77  		cli.client = c
    78  		return nil
    79  	}
    80  }