Example #1
0
func TestHelp(t *testing.T) {
	RegisterFailHandler(Fail)

	commands_loader.Load()

	RunSpecs(t, "Help Suite")
}
Example #2
0
func TestApp(t *testing.T) {
	config := configuration.NewRepositoryWithDefaults()
	i18n.T = i18n.Init(config, &detection.JibberJabberDetector{})

	//init all commands
	commands_loader.Load()

	RegisterFailHandler(Fail)
	plugin_builder.BuildTestBinary(filepath.Join("..", "..", "fixtures", "plugins"), "test_1")
	plugin_builder.BuildTestBinary(filepath.Join("..", "..", "fixtures", "plugins"), "test_2")
	RunSpecs(t, "App Suite")
}
Example #3
0
func main() {
	commands_loader.Load()

	defer handlePanics(deps.TeePrinter)
	defer deps.Config.Close()

	//handles `cf` | `cf -h` || `cf -help`
	if len(os.Args) == 1 || os.Args[1] == "--help" || os.Args[1] == "-help" ||
		os.Args[1] == "--h" || os.Args[1] == "-h" {
		help.ShowHelp(help.GetHelpTemplate())
		os.Exit(0)
	}

	//handle `cf -v` for cf version
	if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "--version") {
		deps.Ui.Say(os.Args[0] + " version " + cf.Version + "-" + cf.BuiltOnDate)
		os.Exit(0)
	}

	//handle `cf --build`
	if len(os.Args) == 2 && (os.Args[1] == "--build" || os.Args[1] == "-b") {
		deps.Ui.Say(T("{{.CFName}} was built with Go version: {{.GoVersion}}",
			map[string]interface{}{
				"CFName":    os.Args[0],
				"GoVersion": runtime.Version(),
			}))
		os.Exit(0)
	}

	//handles `cf [COMMAND] -h ...`
	//rearrage args to `cf help COMMAND` and let `command help` to print out usage
	if requestHelp(os.Args[2:]) {
		os.Args[2] = os.Args[1]
		os.Args[1] = "help"
	}

	//run core command
	cmd := os.Args[1]

	if cmdRegistry.CommandExists(cmd) {
		meta := cmdRegistry.FindCommand(os.Args[1]).MetaData()
		fc := flags.NewFlagContext(meta.Flags)
		fc.SkipFlagParsing(meta.SkipFlagParsing)

		err := fc.Parse(os.Args[2:]...)
		if err != nil {
			deps.Ui.Failed("Incorrect Usage\n\n" + err.Error() + "\n\n" + cmdRegistry.CommandUsage(cmd))
		}

		cmdRegistry.SetCommand(cmdRegistry.FindCommand(cmd).SetDependency(deps, false))
		cfCmd := cmdRegistry.FindCommand(cmd)

		reqs, err := cfCmd.Requirements(requirements.NewFactory(deps.Ui, deps.Config, deps.RepoLocator), fc)
		if err != nil {
			deps.Ui.Failed(err.Error())
		}

		for _, r := range reqs {
			if !r.Execute() {
				os.Exit(1)
			}
		}

		cfCmd.Execute(fc)
		os.Exit(0)
	}

	//non core command, try plugin command
	rpcService := newCliRpcServer(deps.TeePrinter, deps.TeePrinter)

	pluginsConfig := plugin_config.NewPluginConfig(func(err error) { deps.Ui.Failed(fmt.Sprintf("Error read/writing plugin config: %s, ", err.Error())) })
	pluginList := pluginsConfig.Plugins()

	ran := rpc.RunMethodIfExists(rpcService, os.Args[1:], pluginList)

	if !ran {
		deps.Ui.Say("'" + os.Args[1] + T("' is not a registered command. See 'cf help'"))
		os.Exit(1)
	}
}
Example #4
0
import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/cloudfoundry/cli/cf/command_registry"
	"github.com/cloudfoundry/cli/commands_loader"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("CommandsLoader", func() {
	It("references all command packages so all commands can be registered in command_registry", func() {
		commands_loader.Load()

		count := walkDirAndCountCommand("../cf/commands")
		Ω(command_registry.Commands.TotalCommands()).To(Equal(count))
	})
})

func walkDirAndCountCommand(path string) int {
	cmdCount := 0

	filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
		if err != nil {
			fmt.Println("Error walking commands directories:", err)
			return err
		}
Example #5
0
File: main.go Project: exg77/cli
func main() {
	commands_loader.Load()

	defer handlePanics(deps.TeePrinter)
	defer deps.Config.Close()

	//////////////// non-codegangsta path  ///////////////////////
	//handles `cf` | `cf -h` || `cf -help`
	if len(os.Args) == 1 || os.Args[1] == "--help" || os.Args[1] == "-help" ||
		os.Args[1] == "--h" || os.Args[1] == "-h" {
		help.ShowHelp(help.GetHelpTemplate())
		os.Exit(0)
	}

	//handle `cf -v` for cf version
	if len(os.Args) == 2 && os.Args[1] == "-v" {
		deps.Ui.Say(os.Args[0] + " version " + cf.Version + "-" + cf.BuiltOnDate)
		os.Exit(0)
	}

	//handles `cf [COMMAND] -h ...`
	//rearrage args to `cf help COMMAND` and let `command help` to print out usage
	if requestHelp(os.Args[2:]) {
		os.Args[2] = os.Args[1]
		os.Args[1] = "help"
	}

	if len(os.Args) > 1 {
		cmd := os.Args[1]

		if cmdRegistry.CommandExists(cmd) {
			meta := cmdRegistry.FindCommand(os.Args[1]).MetaData()
			fc := flags.NewFlagContext(meta.Flags)
			fc.SkipFlagParsing(meta.SkipFlagParsing)

			if requestHelp(os.Args[2:]) {
				deps.Ui.Say(cmdRegistry.CommandUsage(cmd))
				os.Exit(0)
			} else {
				err := fc.Parse(os.Args[2:]...)
				if err != nil {
					deps.Ui.Failed("Incorrect Usage\n\n" + err.Error() + "\n\n" + cmdRegistry.CommandUsage(cmd))
				}
			}
			cmdRegistry.SetCommand(cmdRegistry.FindCommand(cmd).SetDependency(deps, false))

			cfCmd := cmdRegistry.FindCommand(cmd)
			reqs, err := cfCmd.Requirements(requirements.NewFactory(deps.Ui, deps.Config, deps.RepoLocator), fc)
			if err != nil {
				deps.Ui.Failed(err.Error())
			}

			for _, r := range reqs {
				if !r.Execute() {
					os.Exit(1)
				}
			}

			cfCmd.Execute(fc)
			os.Exit(0)
		}
	}
	//////////////////////////////////////////

	rpcService := newCliRpcServer(deps.TeePrinter, deps.TeePrinter)

	cmdFactory := command_factory.NewFactory(deps.Ui, deps.Config, deps.ManifestRepo, deps.RepoLocator, deps.PluginConfig, rpcService)
	requirementsFactory := requirements.NewFactory(deps.Ui, deps.Config, deps.RepoLocator)
	cmdRunner := command_runner.NewRunner(cmdFactory, requirementsFactory, deps.Ui)
	pluginsConfig := plugin_config.NewPluginConfig(func(err error) { deps.Ui.Failed(fmt.Sprintf("Error read/writing plugin config: %s, ", err.Error())) })
	pluginList := pluginsConfig.Plugins()

	var badFlags string
	metaDatas := cmdFactory.CommandMetadatas()
	//return only metadata for current command
	metaDatas = mergePluginMetaData(metaDatas, pluginList)

	if len(os.Args) > 1 {
		flags := cmdFactory.GetCommandFlags(os.Args[1])
		totalArgs, _ := cmdFactory.GetCommandTotalArgs(os.Args[1])

		if args2skip := totalArgs + 2; len(os.Args) >= args2skip {
			badFlags = matchArgAndFlags(flags, os.Args[args2skip:])
		}

		if badFlags != "" {
			badFlags = badFlags + "\n\n"
		}
	}

	injectHelpTemplate(badFlags)

	theApp := app.NewApp(cmdRunner, metaDatas...)
	rpcService.SetTheApp(theApp)

	if cmdFactory.CheckIfCoreCmdExists(os.Args[1]) {
		callCoreCommand(os.Args[0:], theApp)
	} else {
		// run each plugin and find the method/
		// run method if exist
		ran := rpc.RunMethodIfExists(rpcService, os.Args[1:], pluginList)
		if !ran {
			deps.Ui.Say("'" + os.Args[1] + "' is not a registered command. See 'cf help'")
			os.Exit(1)
		}
	}
}