github.com/mweagle/Sparta@v1.15.0/doc_parseoptions_test.go (about)

     1  package sparta
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  	validator "gopkg.in/go-playground/validator.v9"
    10  )
    11  
    12  var exampleValidator *validator.Validate
    13  
    14  // NOTE: your application MUST use `package main` and define a `main()` function.  The
    15  // example text is to make the documentation compatible with godoc.
    16  // Should be main() in your application
    17  
    18  // Additional command line options used for both the provision
    19  // and CLI commands
    20  type optionsStruct struct {
    21  	Username   string `validate:"required"`
    22  	Password   string `validate:"required"`
    23  	SSHKeyName string `validate:"-"`
    24  }
    25  
    26  var options optionsStruct
    27  
    28  // Common function to register shared command line flags
    29  // across multiple Sparta commands
    30  func registerSpartaCommandLineFlags(command *cobra.Command) {
    31  	command.Flags().StringVarP(&options.Username,
    32  		"username",
    33  		"u",
    34  		"",
    35  		"HTTP Basic Auth username")
    36  	command.Flags().StringVarP(&options.Password,
    37  		"password",
    38  		"p",
    39  		"",
    40  		"HTTP Basic Auth password")
    41  }
    42  
    43  func ExampleParseOptions() {
    44  	//////////////////////////////////////////////////////////////////////////////
    45  	// Add the custom command to run the sync loop
    46  	syncCommand := &cobra.Command{
    47  		Use:   "sync",
    48  		Short: "Periodically perform a task",
    49  		RunE: func(cmd *cobra.Command, args []string) error {
    50  			fmt.Printf("Sync command!\n")
    51  			return nil
    52  		},
    53  	}
    54  	// Include the basic auth flags for the sync command
    55  	registerSpartaCommandLineFlags(syncCommand)
    56  	CommandLineOptions.Root.AddCommand(syncCommand)
    57  
    58  	//////////////////////////////////////////////////////////////////////////////
    59  	// Register custom flags for pre-existing Sparta commands
    60  	registerSpartaCommandLineFlags(CommandLineOptions.Provision)
    61  	CommandLineOptions.Provision.Flags().StringVarP(&options.SSHKeyName,
    62  		"key",
    63  		"k",
    64  		"",
    65  		"SSH Key Name to use for EC2 instances")
    66  
    67  	//////////////////////////////////////////////////////////////////////////////
    68  	// Define a validation hook s.t. we can validate the CLI user input
    69  	validationHook := func(command *cobra.Command) error {
    70  		if command.Name() == "provision" && len(options.SSHKeyName) <= 0 {
    71  			return errors.Errorf("SSHKeyName option is required")
    72  		}
    73  		fmt.Printf("Command: %s\n", command.Name())
    74  		switch command.Name() {
    75  		case "provision",
    76  			"sync":
    77  			validationErr := exampleValidator.Struct(options)
    78  			return errors.Wrapf(validationErr, "Validating input")
    79  		default:
    80  			return nil
    81  		}
    82  	}
    83  	// If the validation hooks failed, exit the application
    84  	parseErr := ParseOptions(validationHook)
    85  	if nil != parseErr {
    86  		os.Exit(3)
    87  	}
    88  	//////////////////////////////////////////////////////////////////////////////
    89  	//
    90  	// Standard Sparta application
    91  	// ...
    92  }