Beispiel #1
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)
	})
}
Beispiel #2
0
func handleInit(ctx *cli.Context) error {
	ID, err := id.Cast(ctx.Args().First())
	if err != nil {
		return ExitCode{
			BadArgs,
			fmt.Sprintf("Bad ID: %v", err),
		}
	}

	// Extract the folder from the resource name by default:
	folder := ctx.GlobalString("path")
	fmt.Println("Folder:", folder)
	if folder == "." {
		folder = ID.AsPath()
	}

	pwd := ctx.GlobalString("password")
	fmt.Println(pwd)
	if pwd == "" {
		// TODO: Lower this or make it at least configurable
		pwdBytes, err := pwdutil.PromptNewPassword(40.0)
		if err != nil {
			return ExitCode{BadPassword, err.Error()}
		}

		pwd = string(pwdBytes)
	}

	log.Warning("============================================")
	log.Warning("Please make sure to remember the passphrase!")
	log.Warning("Your data will not be accessible without it.")
	log.Warning("============================================")

	repo, err := repo.NewRepository(string(ID), pwd, folder)
	if err != nil {
		return ExitCode{UnknownError, err.Error()}
	}

	if err := repo.Close(); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("close: %v", err),
		}
	}

	if !ctx.GlobalBool("nodaemon") {
		port, err := repo.Config.Int("daemon.port")
		if err != nil {
			return ExitCode{UnknownError, "Unable to find out port"}
		}

		if _, err := daemon.Reach(string(pwd), folder, port); err != nil {
			return ExitCode{
				DaemonNotResponding,
				fmt.Sprintf("Unable to start daemon: %v", err),
			}
		}
	}

	return nil
}