github.com/coreos/mantle@v0.13.0/cmd/kolet/kolet.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     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 main
    16  
    17  import (
    18  	"os"
    19  
    20  	"github.com/coreos/pkg/capnslog"
    21  	"github.com/spf13/cobra"
    22  
    23  	"github.com/coreos/mantle/cli"
    24  	"github.com/coreos/mantle/kola/register"
    25  
    26  	// Register any tests that we may wish to execute in kolet.
    27  	_ "github.com/coreos/mantle/kola/registry"
    28  )
    29  
    30  var (
    31  	plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "kolet")
    32  
    33  	root = &cobra.Command{
    34  		Use:   "kolet run [test] [func]",
    35  		Short: "Native code runner for kola",
    36  		Run:   run,
    37  	}
    38  
    39  	cmdRun = &cobra.Command{
    40  		Use:   "run [test] [func]",
    41  		Short: "Run a given test's native function",
    42  		Run:   run,
    43  	}
    44  )
    45  
    46  func run(cmd *cobra.Command, args []string) {
    47  	cmd.Usage()
    48  	os.Exit(2)
    49  }
    50  
    51  func main() {
    52  	for testName, testObj := range register.Tests {
    53  		if len(testObj.NativeFuncs) == 0 {
    54  			continue
    55  		}
    56  		testCmd := &cobra.Command{
    57  			Use: testName + " [func]",
    58  			Run: run,
    59  		}
    60  		for nativeName := range testObj.NativeFuncs {
    61  			nativeFunc := testObj.NativeFuncs[nativeName]
    62  			nativeRun := func(cmd *cobra.Command, args []string) {
    63  				if len(args) != 0 {
    64  					cmd.Usage()
    65  					os.Exit(2)
    66  				}
    67  				if err := nativeFunc(); err != nil {
    68  					plog.Fatal(err)
    69  				}
    70  				// Explicitly exit successfully.
    71  				os.Exit(0)
    72  			}
    73  			nativeCmd := &cobra.Command{
    74  				Use: nativeName,
    75  				Run: nativeRun,
    76  			}
    77  			testCmd.AddCommand(nativeCmd)
    78  		}
    79  		cmdRun.AddCommand(testCmd)
    80  	}
    81  	root.AddCommand(cmdRun)
    82  
    83  	cli.Execute(root)
    84  }