func MakeCliApp( diegoVersion string, latticeVersion string, ltcConfigRoot string, exitHandler exit_handler.ExitHandler, config *config.Config, logger lager.Logger, receptorClientCreator receptor_client.Creator, targetVerifier target_verifier.TargetVerifier, cliStdout io.Writer, ) *cli.App { config.Load() app := cli.NewApp() app.Name = AppName app.Author = latticeCliAuthor app.Version = defaultVersion(diegoVersion, latticeVersion) app.Usage = LtcUsage app.Email = "*****@*****.**" ui := terminal.NewUI(os.Stdin, cliStdout, password_reader.NewPasswordReader(exitHandler)) app.Writer = ui app.Before = func(context *cli.Context) error { args := context.Args() command := app.Command(args.First()) if command == nil { return nil } if _, ok := nonTargetVerifiedCommandNames[command.Name]; ok || len(args) == 0 { return nil } if receptorUp, authorized, err := targetVerifier.VerifyTarget(config.Receptor()); !receptorUp { ui.SayLine(fmt.Sprintf("Error connecting to the receptor. Make sure your lattice target is set, and that lattice is up and running.\n\tUnderlying error: %s", err.Error())) return err } else if !authorized { ui.SayLine("Could not authenticate with the receptor. Please run ltc target with the correct credentials.") return errors.New("Could not authenticate with the receptor.") } return nil } app.Action = defaultAction app.CommandNotFound = func(c *cli.Context, command string) { ui.SayLine(fmt.Sprintf(unknownCommand, command)) exitHandler.Exit(1) } app.Commands = cliCommands(ltcConfigRoot, exitHandler, config, logger, receptorClientCreator, targetVerifier, ui) return app }
func cliCommands(ltcConfigRoot string, exitHandler exit_handler.ExitHandler, config *config.Config, logger lager.Logger, targetVerifier target_verifier.TargetVerifier, ui terminal.UI) []cli.Command { receptorClient := receptor.NewClient(config.Receptor()) noaaConsumer := noaa.NewConsumer(LoggregatorUrl(config.Loggregator()), nil, nil) appRunner := app_runner.New(receptorClient, config.Target()) clock := clock.NewClock() logReader := logs.NewLogReader(noaaConsumer) tailedLogsOutputter := console_tailed_logs_outputter.NewConsoleTailedLogsOutputter(ui, logReader) taskExaminer := task_examiner.New(receptorClient) taskExaminerCommandFactory := task_examiner_command_factory.NewTaskExaminerCommandFactory(taskExaminer, ui, exitHandler) taskRunner := task_runner.New(receptorClient, taskExaminer) taskRunnerCommandFactory := task_runner_command_factory.NewTaskRunnerCommandFactory(taskRunner, ui, exitHandler) appExaminer := app_examiner.New(receptorClient, app_examiner.NewNoaaConsumer(noaaConsumer)) graphicalVisualizer := graphical.NewGraphicalVisualizer(appExaminer) appExaminerCommandFactory := app_examiner_command_factory.NewAppExaminerCommandFactory(appExaminer, ui, clock, exitHandler, graphicalVisualizer, taskExaminer, config.Target()) appRunnerCommandFactoryConfig := app_runner_command_factory.AppRunnerCommandFactoryConfig{ AppRunner: appRunner, AppExaminer: appExaminer, UI: ui, Domain: config.Target(), Env: os.Environ(), Clock: clock, Logger: logger, TailedLogsOutputter: tailedLogsOutputter, ExitHandler: exitHandler, } appRunnerCommandFactory := app_runner_command_factory.NewAppRunnerCommandFactory(appRunnerCommandFactoryConfig) dockerRunnerCommandFactoryConfig := docker_runner_command_factory.DockerRunnerCommandFactoryConfig{ AppRunner: appRunner, AppExaminer: appExaminer, UI: ui, Domain: config.Target(), Env: os.Environ(), Clock: clock, Logger: logger, ExitHandler: exitHandler, TailedLogsOutputter: tailedLogsOutputter, DockerMetadataFetcher: docker_metadata_fetcher.New(docker_metadata_fetcher.NewDockerSessionFactory()), } dockerRunnerCommandFactory := docker_runner_command_factory.NewDockerRunnerCommandFactory(dockerRunnerCommandFactoryConfig) logsCommandFactory := logs_command_factory.NewLogsCommandFactory(appExaminer, taskExaminer, ui, tailedLogsOutputter, exitHandler) clusterTestRunner := cluster_test.NewClusterTestRunner(config, ltcConfigRoot) clusterTestCommandFactory := cluster_test_command_factory.NewClusterTestCommandFactory(clusterTestRunner) blobStore := blob_store.New(config) blobStoreVerifier := blob_store.NewVerifier(config) dropletRunner := droplet_runner.New(appRunner, taskRunner, config, blobStore, appExaminer) cfIgnore := cf_ignore.New() zipper := &zipper_package.DropletArtifactZipper{} dropletRunnerCommandFactory := droplet_runner_command_factory.NewDropletRunnerCommandFactory(*appRunnerCommandFactory, blobStoreVerifier, taskExaminer, dropletRunner, cfIgnore, zipper, config) configCommandFactory := config_command_factory.NewConfigCommandFactory(config, ui, targetVerifier, blobStoreVerifier, exitHandler) helpCommand := cli.Command{ Name: "help", Aliases: []string{"h"}, Usage: "Shows a list of commands or help for one command", Description: "ltc help", Action: defaultAction, } return []cli.Command{ appExaminerCommandFactory.MakeCellsCommand(), dockerRunnerCommandFactory.MakeCreateAppCommand(), appRunnerCommandFactory.MakeSubmitLrpCommand(), logsCommandFactory.MakeDebugLogsCommand(), appExaminerCommandFactory.MakeListAppCommand(), logsCommandFactory.MakeLogsCommand(), appRunnerCommandFactory.MakeRemoveAppCommand(), appRunnerCommandFactory.MakeScaleAppCommand(), appExaminerCommandFactory.MakeStatusCommand(), taskRunnerCommandFactory.MakeSubmitTaskCommand(), configCommandFactory.MakeTargetCommand(), taskExaminerCommandFactory.MakeTaskCommand(), taskRunnerCommandFactory.MakeDeleteTaskCommand(), taskRunnerCommandFactory.MakeCancelTaskCommand(), clusterTestCommandFactory.MakeClusterTestCommand(), appRunnerCommandFactory.MakeUpdateRoutesCommand(), appRunnerCommandFactory.MakeUpdateCommand(), appExaminerCommandFactory.MakeVisualizeCommand(), dropletRunnerCommandFactory.MakeBuildDropletCommand(), dropletRunnerCommandFactory.MakeListDropletsCommand(), dropletRunnerCommandFactory.MakeLaunchDropletCommand(), dropletRunnerCommandFactory.MakeRemoveDropletCommand(), dropletRunnerCommandFactory.MakeImportDropletCommand(), dropletRunnerCommandFactory.MakeExportDropletCommand(), helpCommand, } }
fakeTargetVerifier.VerifyTargetReturns(true, false, nil) fakeBlobStoreVerifier.VerifyReturns(true, nil) fakePasswordReader.PromptForPasswordReturns("testpassword") }) It("prompts for credentials and stores them in the config", func() { doneChan := test_helpers.AsyncExecuteCommandWithArgs(targetCommand, []string{"myapi.com"}) Eventually(outputBuffer).Should(test_helpers.Say("Username: "******"testusername\n")) Eventually(doneChan, 3).Should(BeClosed()) Expect(config.Target()).To(Equal("myapi.com")) Expect(config.Receptor()).To(Equal("http://*****:*****@receptor.myapi.com")) Expect(outputBuffer).To(test_helpers.SayLine("API location set.")) Expect(fakePasswordReader.PromptForPasswordCallCount()).To(Equal(1)) Expect(fakePasswordReader.PromptForPasswordArgsForCall(0)).To(Equal("Password")) Expect(fakeTargetVerifier.VerifyTargetCallCount()).To(Equal(2)) Expect(fakeTargetVerifier.VerifyTargetArgsForCall(0)).To(Equal("http://receptor.myapi.com")) Expect(fakeTargetVerifier.VerifyTargetArgsForCall(1)).To(Equal("http://*****:*****@receptor.myapi.com")) }) Context("when provided receptor credentials are invalid", func() { It("does not save the config", func() { fakePasswordReader.PromptForPasswordReturns("some-invalid-password") doneChan := test_helpers.AsyncExecuteCommandWithArgs(targetCommand, []string{"newtarget.com"})
BeforeEach(func() { stdinReader, stdinWriter = io.Pipe() outputBuffer = gbytes.NewBuffer() fakeExitHandler = &fake_exit_handler.FakeExitHandler{} fakePasswordReader = &fake_password_reader.FakePasswordReader{} terminalUI = terminal.NewUI(stdinReader, outputBuffer, fakePasswordReader) fakeTargetVerifier = &fake_target_verifier.FakeTargetVerifier{} config = config_package.New(persister.NewMemPersister()) }) Describe("TargetCommand", func() { var targetCommand cli.Command verifyOldTargetStillSet := func() { config.Load() Expect(config.Receptor()).To(Equal("http://*****:*****@receptor.oldtarget.com")) } BeforeEach(func() { commandFactory := command_factory.NewConfigCommandFactory(config, terminalUI, fakeTargetVerifier, fakeExitHandler) targetCommand = commandFactory.MakeTargetCommand() }) JustBeforeEach(func() { config.SetTarget("oldtarget.com") config.SetLogin("olduser", "oldpass") config.Save() }) Context("displaying the target", func() { It("outputs the current target", func() {
func cliCommands(ltcConfigRoot string, exitHandler exit_handler.ExitHandler, config *config.Config, logger lager.Logger, receptorClientCreator receptor_client.Creator, targetVerifier target_verifier.TargetVerifier, ui terminal.UI) []cli.Command { receptorClient := receptorClientCreator.CreateReceptorClient(config.Receptor()) noaaConsumer := noaa.NewConsumer(LoggregatorUrl(config.Loggregator()), nil, nil) appRunner := app_runner.New(receptorClient, config.Target(), &keygen_package.KeyGenerator{RandReader: rand.Reader}) clock := clock.NewClock() logReader := logs.NewLogReader(noaaConsumer) tailedLogsOutputter := console_tailed_logs_outputter.NewConsoleTailedLogsOutputter(ui, logReader) taskExaminer := task_examiner.New(receptorClient) taskExaminerCommandFactory := task_examiner_command_factory.NewTaskExaminerCommandFactory(taskExaminer, ui, exitHandler) taskRunner := task_runner.New(receptorClient, taskExaminer) taskRunnerCommandFactory := task_runner_command_factory.NewTaskRunnerCommandFactory(taskRunner, ui, exitHandler) appExaminer := app_examiner.New(receptorClient, app_examiner.NewNoaaConsumer(noaaConsumer)) graphicalVisualizer := graphical.NewGraphicalVisualizer(appExaminer) dockerTerminal := &app_examiner_command_factory.DockerTerminal{} appExaminerCommandFactory := app_examiner_command_factory.NewAppExaminerCommandFactory(appExaminer, ui, dockerTerminal, clock, exitHandler, graphicalVisualizer, taskExaminer, config.Target()) appRunnerCommandFactoryConfig := app_runner_command_factory.AppRunnerCommandFactoryConfig{ AppRunner: appRunner, AppExaminer: appExaminer, UI: ui, Domain: config.Target(), Env: os.Environ(), Clock: clock, Logger: logger, TailedLogsOutputter: tailedLogsOutputter, ExitHandler: exitHandler, } appRunnerCommandFactory := app_runner_command_factory.NewAppRunnerCommandFactory(appRunnerCommandFactoryConfig) dockerRunnerCommandFactoryConfig := docker_runner_command_factory.DockerRunnerCommandFactoryConfig{ AppRunner: appRunner, AppExaminer: appExaminer, UI: ui, Domain: config.Target(), Env: os.Environ(), Clock: clock, Logger: logger, ExitHandler: exitHandler, TailedLogsOutputter: tailedLogsOutputter, DockerMetadataFetcher: docker_metadata_fetcher.New(docker_metadata_fetcher.NewDockerSessionFactory()), } dockerRunnerCommandFactory := docker_runner_command_factory.NewDockerRunnerCommandFactory(dockerRunnerCommandFactoryConfig) logsCommandFactory := logs_command_factory.NewLogsCommandFactory(appExaminer, taskExaminer, ui, tailedLogsOutputter, exitHandler) clusterTestRunner := cluster_test.NewClusterTestRunner(config, ltcConfigRoot) clusterTestCommandFactory := cluster_test_command_factory.NewClusterTestCommandFactory(clusterTestRunner) blobStore := blob_store.New(config) blobStoreVerifier := blob_store.BlobStoreVerifier{ DAVBlobStoreVerifier: dav_blob_store.Verifier{}, S3BlobStoreVerifier: s3_blob_store.Verifier{}, } httpProxyConfReader := &droplet_runner.HTTPProxyConfReader{ URL: fmt.Sprintf("http://%s:8444/proxyconf.json", config.Target()), } dropletRunner := droplet_runner.New(appRunner, taskRunner, config, blobStore, appExaminer, httpProxyConfReader) cfIgnore := cf_ignore.New() zipper := &zipper_package.DropletArtifactZipper{} dropletRunnerCommandFactory := droplet_runner_command_factory.NewDropletRunnerCommandFactory(*appRunnerCommandFactory, blobStoreVerifier, taskExaminer, dropletRunner, cfIgnore, zipper, config) configCommandFactory := config_command_factory.NewConfigCommandFactory(config, ui, targetVerifier, blobStoreVerifier, exitHandler) secureDialer := &secure_shell.SecureDialer{DialFunc: ssh.Dial} secureTerm := &secure_shell.SecureTerm{} keepaliveInterval := 30 * time.Second secureShell := &secure_shell.SecureShell{ Dialer: secureDialer, Term: secureTerm, Clock: clock, Ticker: clock.NewTicker(keepaliveInterval), } sshCommandFactory := ssh_command_factory.NewSSHCommandFactory(config, ui, exitHandler, appExaminer, secureShell) helpCommand := cli.Command{ Name: "help", Aliases: []string{"h"}, Usage: "Shows a list of commands or help for one command", Description: "ltc help", Action: defaultAction, } return []cli.Command{ appExaminerCommandFactory.MakeCellsCommand(), dockerRunnerCommandFactory.MakeCreateAppCommand(), appRunnerCommandFactory.MakeSubmitLrpCommand(), logsCommandFactory.MakeDebugLogsCommand(), appExaminerCommandFactory.MakeListAppCommand(), logsCommandFactory.MakeLogsCommand(), appRunnerCommandFactory.MakeRemoveAppCommand(), appRunnerCommandFactory.MakeScaleAppCommand(), appExaminerCommandFactory.MakeStatusCommand(), taskRunnerCommandFactory.MakeSubmitTaskCommand(), configCommandFactory.MakeTargetCommand(), taskExaminerCommandFactory.MakeTaskCommand(), taskRunnerCommandFactory.MakeDeleteTaskCommand(), taskRunnerCommandFactory.MakeCancelTaskCommand(), clusterTestCommandFactory.MakeClusterTestCommand(), appRunnerCommandFactory.MakeUpdateRoutesCommand(), appRunnerCommandFactory.MakeUpdateCommand(), appExaminerCommandFactory.MakeVisualizeCommand(), dropletRunnerCommandFactory.MakeBuildDropletCommand(), dropletRunnerCommandFactory.MakeListDropletsCommand(), dropletRunnerCommandFactory.MakeLaunchDropletCommand(), dropletRunnerCommandFactory.MakeRemoveDropletCommand(), dropletRunnerCommandFactory.MakeImportDropletCommand(), dropletRunnerCommandFactory.MakeExportDropletCommand(), sshCommandFactory.MakeSSHCommand(), helpCommand, } }
func cliCommands(ltcConfigRoot string, exitHandler exit_handler.ExitHandler, config *config.Config, logger lager.Logger, targetVerifier target_verifier.TargetVerifier, ui terminal.UI) []cli.Command { receptorClient := receptor.NewClient(config.Receptor()) noaaConsumer := noaa.NewConsumer(LoggregatorUrl(config.Loggregator()), nil, nil) appRunner := app_runner.New(receptorClient, config.Target()) clock := clock.NewClock() logReader := logs.NewLogReader(noaaConsumer) tailedLogsOutputter := console_tailed_logs_outputter.NewConsoleTailedLogsOutputter(ui, logReader) taskExaminer := task_examiner.New(receptorClient) taskExaminerCommandFactory := task_examiner_command_factory.NewTaskExaminerCommandFactory(taskExaminer, ui, exitHandler) taskRunner := task_runner.New(receptorClient, taskExaminer) taskRunnerCommandFactory := task_runner_command_factory.NewTaskRunnerCommandFactory(taskRunner, ui, exitHandler) appExaminer := app_examiner.New(receptorClient, app_examiner.NewNoaaConsumer(noaaConsumer)) graphicalVisualizer := graphical.NewGraphicalVisualizer(appExaminer) appExaminerCommandFactory := app_examiner_command_factory.NewAppExaminerCommandFactory(appExaminer, ui, clock, exitHandler, graphicalVisualizer, taskExaminer) appRunnerCommandFactoryConfig := app_runner_command_factory.AppRunnerCommandFactoryConfig{ AppRunner: appRunner, AppExaminer: appExaminer, UI: ui, Domain: config.Target(), Env: os.Environ(), Clock: clock, Logger: logger, TailedLogsOutputter: tailedLogsOutputter, ExitHandler: exitHandler, } appRunnerCommandFactory := app_runner_command_factory.NewAppRunnerCommandFactory(appRunnerCommandFactoryConfig) dockerRunnerCommandFactoryConfig := docker_runner_command_factory.DockerRunnerCommandFactoryConfig{ AppRunner: appRunner, AppExaminer: appExaminer, UI: ui, Domain: config.Target(), Env: os.Environ(), Clock: clock, Logger: logger, ExitHandler: exitHandler, TailedLogsOutputter: tailedLogsOutputter, DockerMetadataFetcher: docker_metadata_fetcher.New(docker_metadata_fetcher.NewDockerSessionFactory()), } dockerRunnerCommandFactory := docker_runner_command_factory.NewDockerRunnerCommandFactory(dockerRunnerCommandFactoryConfig) logsCommandFactory := logs_command_factory.NewLogsCommandFactory(appExaminer, ui, tailedLogsOutputter, exitHandler) configCommandFactory := config_command_factory.NewConfigCommandFactory(config, ui, targetVerifier, exitHandler) testRunner := integration_test.NewIntegrationTestRunner(config, ltcConfigRoot) integrationTestCommandFactory := integration_test_command_factory.NewIntegrationTestCommandFactory(testRunner) s3Auth := aws.Auth{ AccessKey: config.BlobTarget().AccessKey, SecretKey: config.BlobTarget().SecretKey, } s3S3 := s3.New(s3Auth, awsRegion, &http.Client{ Transport: &http.Transport{ Proxy: config.BlobTarget().Proxy(), Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).Dial, TLSHandshakeTimeout: 10 * time.Second, }, }) blobStore := blob_store.NewBlobStore(config, s3S3) blobBucket := blobStore.Bucket(config.BlobTarget().BucketName) dropletRunner := droplet_runner.New(appRunner, taskRunner, config, blobStore, blobBucket, targetVerifier, appExaminer) cfIgnore := cf_ignore.New() dropletRunnerCommandFactory := droplet_runner_command_factory.NewDropletRunnerCommandFactory(*appRunnerCommandFactory, taskExaminer, dropletRunner, cfIgnore) helpCommand := cli.Command{ Name: "help", Aliases: []string{"h"}, Usage: "Shows a list of commands or help for one command", Description: "ltc help", Action: defaultAction, } return []cli.Command{ appExaminerCommandFactory.MakeCellsCommand(), dockerRunnerCommandFactory.MakeCreateAppCommand(), appRunnerCommandFactory.MakeSubmitLrpCommand(), logsCommandFactory.MakeDebugLogsCommand(), appExaminerCommandFactory.MakeListAppCommand(), logsCommandFactory.MakeLogsCommand(), appRunnerCommandFactory.MakeRemoveAppCommand(), appRunnerCommandFactory.MakeScaleAppCommand(), appExaminerCommandFactory.MakeStatusCommand(), taskRunnerCommandFactory.MakeSubmitTaskCommand(), configCommandFactory.MakeTargetCommand(), configCommandFactory.MakeTargetBlobCommand(), taskExaminerCommandFactory.MakeTaskCommand(), taskRunnerCommandFactory.MakeDeleteTaskCommand(), taskRunnerCommandFactory.MakeCancelTaskCommand(), integrationTestCommandFactory.MakeIntegrationTestCommand(), appRunnerCommandFactory.MakeUpdateRoutesCommand(), appExaminerCommandFactory.MakeVisualizeCommand(), dropletRunnerCommandFactory.MakeBuildDropletCommand(), dropletRunnerCommandFactory.MakeListDropletsCommand(), dropletRunnerCommandFactory.MakeLaunchDropletCommand(), dropletRunnerCommandFactory.MakeRemoveDropletCommand(), helpCommand, } }
}) Describe("Username", func() { It("sets the target", func() { testConfig.SetLogin("ausername", "apassword") Expect(testConfig.Username()).To(Equal("ausername")) }) }) Describe("Receptor", func() { It("returns the Receptor with a username and password", func() { testConfig.SetTarget("mynewapi.com") testConfig.SetLogin("testusername", "testpassword") Expect(testConfig.Receptor()).To(Equal("http://*****:*****@receptor.mynewapi.com")) }) It("returns a Receptor without a username and password", func() { testConfig.SetTarget("mynewapi.com") testConfig.SetLogin("", "") Expect(testConfig.Receptor()).To(Equal("http://receptor.mynewapi.com")) }) }) Describe("Loggregator", func() { It("provides the loggregator doppler path", func() { testConfig.SetTarget("mytestapi.com") Expect(testConfig.Loggregator()).To(Equal("doppler.mytestapi.com"))