示例#1
0
// BindFlags adds any flags that are common to all kubectl sub commands.
func (f *Factory) BindFlags(flags *pflag.FlagSet) {
	// any flags defined by external projects (not part of pflags)
	util.AddFlagSetToPFlagSet(flag.CommandLine, flags)

	// This is necessary as github.com/spf13/cobra doesn't support "global"
	// pflags currently.  See https://github.com/spf13/cobra/issues/44.
	util.AddPFlagSetToPFlagSet(pflag.CommandLine, flags)

	// Hack for global access to validation flag.
	// TODO: Refactor out after configuration flag overhaul.
	if f.flags.Lookup("validate") == nil {
		f.flags.Bool("validate", false, "If true, use a schema to validate the input before sending it")
	}

	// Merge factory's flags
	util.AddPFlagSetToPFlagSet(f.flags, flags)

	// Globally persistent flags across all subcommands.
	// TODO Change flag names to consts to allow safer lookup from subcommands.
	// TODO Add a verbose flag that turns on glog logging. Probably need a way
	// to do that automatically for every subcommand.
	flags.BoolVar(&f.clients.matchVersion, FlagMatchBinaryVersion, false, "Require server version to match client version")

	// Normalize all flags that are coming from other packages or pre-configurations
	// a.k.a. change all "_" to "-". e.g. glog package
	flags.SetNormalizeFunc(util.WordSepNormalizeFunc)
}
示例#2
0
// Flags returns a flagset for "global" flags.
func (hk *HyperKube) Flags() *pflag.FlagSet {
	if hk.baseFlags == nil {
		hk.baseFlags = pflag.NewFlagSet(hk.Name, pflag.ContinueOnError)
		hk.baseFlags.SetOutput(ioutil.Discard)
		hk.baseFlags.BoolVarP(&hk.helpFlagVal, "help", "h", false, "help for "+hk.Name)

		// These will add all of the "global" flags (defined with both the
		// flag and pflag packages) to the new flag set we have.
		util.AddFlagSetToPFlagSet(flag.CommandLine, hk.baseFlags)
		util.AddPFlagSetToPFlagSet(pflag.CommandLine, hk.baseFlags)

	}
	return hk.baseFlags
}
示例#3
0
func main() {
	flags := pflag.NewFlagSet("openshift-cucumber", pflag.ExitOnError)
	printVersion := flags.BoolP("version", "v", false, "print version")
	reporterName := flags.StringP("reporter", "r", "", "reporter (junit)")
	outputFile := flags.StringP("output", "o", "", "output file")
	util.AddFlagSetToPFlagSet(flag.CommandLine, flags) // import glog flags
	flags.Parse(os.Args[1:])

	if *printVersion {
		if len(gitCommit) == 0 {
			fmt.Printf("openshift-cucumber - DEV\n")
		} else if len(buildNumber) == 0 {
			fmt.Printf("openshift-cucumber - Commit %s\n", gitCommit)
		} else {
			fmt.Printf("openshift-cucumber - Commit %s Build #%s\n", gitCommit, buildNumber)
		}
		os.Exit(0)
	}

	if len(os.Args) == 1 {
		usage := `%[1]s will run tests for all the cucumber '*.feature' files found in the provided paths.

Usage:
%[1]s /path/to/directory-with-cucumber-feature-files
`
		fmt.Printf(usage, os.Args[0])
		os.Exit(1)
	}

	features := []string{}
	for _, dir := range os.Args[1:] {
		firstLevelFiles, _ := filepath.Glob(filepath.Join(dir, "*.feature"))
		subLevelsFiles, _ := filepath.Glob(filepath.Join(dir, "**", "*.feature"))

		features = append(features, firstLevelFiles...)
		features = append(features, subLevelsFiles...)
	}

	c := steps.NewContext(&gucumber.GlobalContext)
	runner, err := c.RunFiles(features)
	if err != nil {
		log.Fatalf("Got error %v\n", err)
	}

	if *reporterName == "junit" {
		junit := &reporter.JunitReporter{}
		f, err := os.Create(*outputFile)
		if err != nil {
			log.Fatalf("Failed to create output file %s: %v", *outputFile, err)
		} else {
			defer f.Close()

			w := bufio.NewWriter(f)
			if err = junit.GenerateReport(runner.Results, w); err != nil {
				log.Fatalf("Failed to generate JUnit Report to %s: %v", *outputFile, err)
			}
		}

	}

}