Ejemplo n.º 1
0
func handleDebugExport(ctx *cli.Context, client *daemon.Client) error {
	who := ctx.Args().First()

	var whoID id.ID

	if who != "" {
		validID, err := id.Cast(who)
		if err != nil {
			return err
		}

		whoID = validID
	}

	data, err := client.Export(whoID)
	if err != nil {
		return err
	}

	if _, err := os.Stdout.Write(data); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 2
0
func handleCommit(ctx *cli.Context, client *daemon.Client) error {
	message := ctx.String("message")
	if message == "" {
		message = fmt.Sprintf("Update on %s", time.Now().String())
	}
	status, err := client.Status()
	if err != nil {
		return err
	}

	statusCmt := status.Commit
	if statusCmt == nil {
		return fmt.Errorf("Empty status commit in response.")
	}

	commitCnt := len(statusCmt.Changeset)
	if commitCnt == 0 {
		fmt.Println("Nothing to commit.")
		return nil
	}
	if commitCnt != 1 {
		fmt.Printf("%d changes commited.\n", commitCnt)
	} else {
		fmt.Printf("%d change commited.\n", commitCnt)
	}

	return client.MakeCommit(message)
}
Ejemplo n.º 3
0
func handleLog(ctx *cli.Context, client *daemon.Client) error {
	log, err := client.Log(nil, nil)
	if err != nil {
		return err
	}

	for _, pnode := range log.Nodes {
		commitMH, err := multihash.Cast(pnode.Hash)
		if err != nil {
			return err
		}

		pcmt := pnode.Commit
		if pcmt == nil {
			return fmt.Errorf("Empty commit in log-commit")
		}

		rootMH, err := multihash.Cast(pcmt.Root)
		if err != nil {
			return err
		}

		fmt.Printf(
			"%s/%s by %s, %s\n",
			colors.Colorize(commitMH.B58String()[:10], colors.Green),
			colors.Colorize(rootMH.B58String()[:10], colors.Magenta),
			pcmt.Author,
			pcmt.Message,
		)
	}
	return nil
}
Ejemplo n.º 4
0
func doMount(ctx *cli.Context, client *daemon.Client, mount bool) error {
	mountPath := ""
	if len(ctx.Args()) > 0 {
		mountPath = ctx.Args().First()
	} else {
		mountPath = filepath.Join(guessRepoFolder(), "mount")
		if err := os.Mkdir(mountPath, 0744); err != nil {
			return err
		}
	}

	var err error

	if mount {
		err = client.Mount(mountPath)
	} else {
		err = client.Unmount(mountPath)
	}

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

	return nil
}
Ejemplo n.º 5
0
func handleStage(ctx *cli.Context, client *daemon.Client) error {
	filePath, err := filepath.Abs(ctx.Args().First())
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to make abs path: %v: %v", filePath, err),
		}
	}

	// Assume "/file.png" for file.png as repo path, if none given.
	repoPath := "/" + filepath.Base(filePath)
	if ctx.NArg() > 1 {
		repoPath = ctx.Args()[1]
	}

	if err := client.Stage(filePath, repoPath); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Could not stage file: %v: %v", filePath, err),
		}
	}

	fmt.Println(repoPath)
	return nil
}
Ejemplo n.º 6
0
func handleDebugImport(ctx *cli.Context, client *daemon.Client) error {
	buf := &bytes.Buffer{}

	if _, err := io.Copy(buf, os.Stdin); err != nil {
		return err
	}

	return client.Import(buf.Bytes())
}
Ejemplo n.º 7
0
func handleMv(ctx *cli.Context, client *daemon.Client) error {
	source, dest := prefixSlash(ctx.Args().First()), prefixSlash(ctx.Args().Get(1))

	if err := client.Move(source, dest); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("move failed: %v", err),
		}
	}

	return nil
}
Ejemplo n.º 8
0
func handleIsOnline(ctx *cli.Context, client *daemon.Client) error {
	status, err := client.IsOnline()
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Failed to check online-status: %v", err),
		}
	}

	fmt.Println(status)
	return nil
}
Ejemplo n.º 9
0
func handleRm(ctx *cli.Context, client *daemon.Client) error {
	repoPath := prefixSlash(ctx.Args().First())

	if err := client.Remove(repoPath, ctx.Bool("recursive")); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Could not remove file: `%s`: %v", repoPath, err),
		}
	}

	return nil
}
Ejemplo n.º 10
0
func handleRemoteSelf(ctx *cli.Context, client *daemon.Client) error {
	re, err := client.RemoteSelf()
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to list remote self information: %v", err),
		}
	}

	printRemoteEntry(re)
	return nil
}
Ejemplo n.º 11
0
func handleRemoteList(ctx *cli.Context, client *daemon.Client) error {
	data, err := client.RemoteList()
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to list remotes: %v", err),
		}
	}
	for _, entry := range data {
		printRemoteEntry(entry)
	}
	return nil
}
Ejemplo n.º 12
0
func handleList(ctx *cli.Context, client *daemon.Client) error {
	path := "/"
	if ctx.NArg() > 0 {
		path = prefixSlash(ctx.Args().First())
	}

	depth := ctx.Int("depth")
	if ctx.Bool("recursive") {
		depth = -1
	}

	entries, err := client.List(path, depth)
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("ls: %v", err),
		}
	}

	for _, entry := range entries {
		modTime := time.Time{}
		if err := modTime.UnmarshalText(entry.ModTime); err != nil {
			log.Warningf("Could not parse mtime (%s): %v", entry.ModTime, err)
			continue
		}

		fmt.Printf(
			"%s\t%s\t%s\n",
			colors.Colorize(
				humanize.Bytes(uint64(entry.NodeSize)),
				colors.Green,
			),
			colors.Colorize(
				humanize.Time(modTime),
				colors.Cyan,
			),
			colors.Colorize(
				entry.Path,
				colors.Magenta,
			),
		)
	}

	return nil
}
Ejemplo n.º 13
0
func handleSyncSingle(ctx *cli.Context, client *daemon.Client, idStr string) error {
	ID, err := id.Cast(idStr)

	if err != nil {
		return ExitCode{
			BadArgs,
			fmt.Sprintf("Bad ID: %v", err),
		}
	}

	log.Infof("Syncing with %s", ID)
	if err := client.Sync(ID); err != nil {
		log.Errorf("Failed: %v", err)
		return ExitCode{UnknownError, err.Error()}
	}

	return nil
}
Ejemplo n.º 14
0
func handleRemoteRemove(ctx *cli.Context, client *daemon.Client) error {
	idString := ctx.Args()[0]
	id, err := id.Cast(idString)
	if err != nil {
		return ExitCode{
			BadArgs,
			fmt.Sprintf("Invalid ID: %v", err),
		}
	}

	if client.RemoteRemove(id) != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to remove remote: %v", err),
		}
	}

	return nil
}
Ejemplo n.º 15
0
func handleHistory(ctx *cli.Context, client *daemon.Client) error {
	repoPath := prefixSlash(ctx.Args().First())

	history, err := client.History(repoPath)
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to retrieve history: %v", err),
		}
	}

	fmt.Println(colors.Colorize(repoPath, colors.Magenta))
	for idx := range history {
		checkpoint := history[len(history)-idx-1]
		printCheckpoint(checkpoint, idx, len(history))

	}
	return nil
}
Ejemplo n.º 16
0
func handleMkdir(ctx *cli.Context, client *daemon.Client) error {
	path := prefixSlash(ctx.Args().First())

	var err error

	if ctx.Bool("parents") {
		err = client.MkdirAll(path)
	} else {
		err = client.Mkdir(path)
	}

	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("mkdir failed: %v", err),
		}
	}

	return nil
}
Ejemplo n.º 17
0
func handleRemoteAdd(ctx *cli.Context, client *daemon.Client) error {
	idString, hash := ctx.Args()[0], ctx.Args()[1]

	id, err := id.Cast(idString)
	if err != nil {
		return ExitCode{
			BadArgs,
			fmt.Sprintf("Invalid ID: %v", err),
		}
	}

	if err := client.RemoteAdd(id, hash); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to add remote: %v", err),
		}
	}

	return nil
}
Ejemplo n.º 18
0
func handleStatus(ctx *cli.Context, client *daemon.Client) error {
	status, err := client.Status()
	if err != nil {
		return err
	}

	statusCmt := status.Commit
	if statusCmt == nil {
		return fmt.Errorf("Empty status commit in response.")
	}

	checkpoints := statusCmt.Checkpoints
	if len(checkpoints) == 0 {
		fmt.Println("Nothing to commit.")
		return nil
	}

	userToChanges := make(map[id.ID]changeByType)
	for _, pckp := range checkpoints {
		byAuthor, ok := userToChanges[id.ID(pckp.Author)]
		if !ok {
			byAuthor = make(changeByType)
			userToChanges[id.ID(pckp.Author)] = byAuthor
		}

		byAuthor[pckp.Change] = append(byAuthor[pckp.Change], pckp)
	}

	for user, changesByuser := range userToChanges {
		fmt.Printf(
			"Changes by %s:\n",
			colors.Colorize(string(user), colors.Magenta),
		)
		printChangesByUser(changesByuser)
	}

	return nil
}
Ejemplo n.º 19
0
func handleRemoteLocate(ctx *cli.Context, client *daemon.Client) error {
	id, err := id.Cast(ctx.Args()[0])
	if err != nil {
		return ExitCode{
			BadArgs,
			fmt.Sprintf("Invalid ID: %v", err),
		}
	}

	hashes, err := client.RemoteLocate(id, 10, 50000)
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to locate ipfs peers: %v", err),
		}
	}

	for _, hash := range hashes {
		fmt.Println(hash)
	}

	return nil
}
Ejemplo n.º 20
0
func handleOnline(ctx *cli.Context, client *daemon.Client) error {
	status, err := client.IsOnline()
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Failed to check online-status: %v", err),
		}
	}

	if status {
		log.Infof("Already online.")
		return nil
	}

	if err := client.Online(); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Failed to go online: %v", err),
		}
	}

	return nil
}
Ejemplo n.º 21
0
func handleSync(ctx *cli.Context, client *daemon.Client) error {
	// With no arguments, we should sync with everyone around.
	if len(ctx.Args()) != 0 {
		// Pass over to single ID handling:
		return handleSyncSingle(ctx, client, ctx.Args().First())
	}

	// Iterate over all remotes brigd knows about:
	data, err := client.RemoteList()
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Unable to list remotes: %v", err),
		}
	}

	for _, entry := range data {
		if err := handleSyncSingle(ctx, client, entry.Ident); err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 22
0
func handlePin(ctx *cli.Context, client *daemon.Client) error {
	path := ctx.Args().First()

	if ctx.Bool("is-pinned") {
		isPinned, err := client.IsPinned(path)
		if err != nil {
			return err
		}

		fmt.Printf("%s: %t\n", path, isPinned)
		return nil
	}

	fn := client.Pin
	if ctx.Bool("unpin") {
		fn = client.Unpin
	}

	if err := fn(path); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 23
0
func handleTree(ctx *cli.Context, client *daemon.Client) error {
	path := "/"
	if ctx.NArg() > 0 {
		path = prefixSlash(ctx.Args().First())
	}

	depth := ctx.Int("depth")
	dirlist, err := client.List(path, depth)
	if err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("ls: %v", err),
		}
	}

	if err := showTree(dirlist, depth); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Printing tree failed: %v", err),
		}
	}

	return nil
}
Ejemplo n.º 24
0
func handleDaemonPing(ctx *cli.Context, client *daemon.Client) error {
	for i := 0; i < 100; i++ {
		before := time.Now()
		symbol := colors.Colorize("✔", colors.Green)
		if !client.Ping() {
			symbol = colors.Colorize("✘", colors.Red)
		}

		delay := time.Since(before)

		fmt.Printf("#%02d %s ➔ %s: %s (%v)\n",
			i+1,
			client.LocalAddr().String(),
			client.RemoteAddr().String(),
			symbol, delay)

		time.Sleep(1 * time.Second)
	}

	return nil
}
Ejemplo n.º 25
0
func handleDaemonQuit(ctx *cli.Context, client *daemon.Client) error {
	client.Exorcise()
	return nil
}
Ejemplo n.º 26
0
func handleCat(ctx *cli.Context, client *daemon.Client) error {
	repoPath := prefixSlash(ctx.Args().First())

	filePath := ""
	isStdoutMode := ctx.NArg() < 2

	if isStdoutMode {
		tmpFile, err := ioutil.TempFile("", ".brig-tmp-")
		if err != nil {
			return ExitCode{
				UnknownError,
				fmt.Sprintf("Unable to create temp file: %v", err),
			}
		}

		filePath = tmpFile.Name()
		defer util.Closer(tmpFile)
		defer func() {
			if err := os.Remove(filePath); err != nil {
				log.Warningf("Cannot remove temp-file: %v", err)
			}
		}()
	} else {
		absPath, err := filepath.Abs(ctx.Args()[1])
		if err != nil {
			return ExitCode{
				UnknownError,
				fmt.Sprintf("Unable to make abs path: %v: %v", filePath, err),
			}
		}

		filePath = absPath
	}

	if err := client.Cat(repoPath, filePath); err != nil {
		return ExitCode{
			UnknownError,
			fmt.Sprintf("Could not cat file: %v: %v", repoPath, err),
		}
	}

	if isStdoutMode {
		fd, err := os.Open(filePath)
		if err != nil {
			return ExitCode{
				UnknownError,
				"Could not open temp file",
			}
		}

		if _, err := io.Copy(os.Stdout, fd); err != nil {
			return ExitCode{
				UnknownError,
				fmt.Sprintf("Cannot copy to stdout: %v", err),
			}
		}

		if err := fd.Close(); err != nil {
			log.Warningf("Unable to close tmpfile handle: %v", err)
		}
	}

	return nil
}
Ejemplo n.º 27
0
func handleUnpin(ctx *cli.Context, client *daemon.Client) error {
	return client.Unpin(ctx.Args().First())
}