go.ligato.io/vpp-agent/v3@v3.5.0/cmd/agentctl/commands/vpp.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 commands
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	agentcli "go.ligato.io/vpp-agent/v3/cmd/agentctl/cli"
    25  )
    26  
    27  func NewVppCommand(cli agentcli.Cli) *cobra.Command {
    28  	cmd := &cobra.Command{
    29  		Use:   "vpp",
    30  		Short: "Manage VPP instance",
    31  	}
    32  	cmd.AddCommand(
    33  		newVppCliCommand(cli),
    34  		newVppInfoCommand(cli),
    35  	)
    36  	return cmd
    37  }
    38  
    39  func newVppCliCommand(cli agentcli.Cli) *cobra.Command {
    40  	cmd := &cobra.Command{
    41  		Use:     "cli",
    42  		Aliases: []string{"c"},
    43  		Short:   "Execute VPP CLI command",
    44  		Example: `
    45  # Execute VPP CLI command 'show version'
    46  {{.CommandPath}} vpp cli show version
    47  
    48  # Print info about VPP
    49  {{.CommandPath}} vpp info
    50  `,
    51  		Args: cobra.MinimumNArgs(1),
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			vppcmd := strings.Join(args, " ")
    54  			return runVppCli(cli, vppcmd)
    55  		},
    56  		SilenceUsage: true,
    57  	}
    58  	return cmd
    59  }
    60  
    61  func runVppCli(cli agentcli.Cli, vppcmd string) error {
    62  	ctx, cancel := context.WithCancel(context.Background())
    63  	defer cancel()
    64  
    65  	fmt.Fprintf(cli.Out(), "vpp# %s\n", vppcmd)
    66  
    67  	reply, err := cli.Client().VppRunCli(ctx, vppcmd)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	fmt.Fprintf(cli.Out(), "%s", reply)
    73  	return nil
    74  }
    75  
    76  func newVppInfoCommand(cli agentcli.Cli) *cobra.Command {
    77  	cmd := &cobra.Command{
    78  		Use:     "info",
    79  		Aliases: []string{"i"},
    80  		Short:   "Retrieve info about VPP",
    81  		Args:    cobra.NoArgs,
    82  		RunE: func(cmd *cobra.Command, args []string) error {
    83  			return runVppInfo(cli)
    84  		},
    85  		SilenceUsage: true,
    86  	}
    87  	return cmd
    88  }
    89  
    90  func runVppInfo(cli agentcli.Cli) error {
    91  	ctx, cancel := context.WithCancel(context.Background())
    92  	defer cancel()
    93  
    94  	version, err := cli.Client().VppRunCli(ctx, "show version verbose")
    95  	if err != nil {
    96  		return err
    97  	}
    98  	fmt.Fprintf(cli.Out(), "VERSION:\n%s\n", version)
    99  
   100  	config, err := cli.Client().VppRunCli(ctx, "show version cmdline")
   101  	if err != nil {
   102  		return err
   103  	}
   104  	fmt.Fprintf(cli.Out(), "CONFIG:\n%s\n", config)
   105  
   106  	return nil
   107  }