Exemple #1
0
func handleDaemonWait(ctx *cli.Context) error {
	port := guessPort()

	for {
		client, err := daemon.Dial(port)
		if err == nil {
			client.Close()
			return nil
		}

		time.Sleep(500 * time.Millisecond)
	}
}
Exemple #2
0
func withDaemon(handler cmdHandlerWithClient, startNew bool) func(*cli.Context) {
	// If not, make sure we start a new one:
	return withExit(func(ctx *cli.Context) error {
		port := guessPort()

		// Check if the daemon is running:
		client, err := daemon.Dial(port)
		if err == nil {
			return handler(ctx, client)
		}

		if !startNew {
			// Daemon was not running and we may not start a new one.
			return ExitCode{DaemonNotResponding, "Daemon not running"}
		}

		// Check if the password was supplied via a commandline flag.
		pwd := ctx.String("password")
		if pwd == "" {
			// Prompt the user:
			var cmdPwd string

			cmdPwd, err = readPassword()
			if err != nil {
				return ExitCode{
					BadPassword,
					fmt.Sprintf("Could not read password: %v", pwd),
				}
			}

			pwd = cmdPwd
		}

		// Start the dameon & pass the password:
		client, err = daemon.Reach(pwd, guessRepoFolder(), port)
		if err != nil {
			return ExitCode{
				DaemonNotResponding,
				fmt.Sprintf("Unable to start daemon: %v", err),
			}
		}

		// Run the actual handler:
		return handler(ctx, client)
	})
}