update.go 1.83 KB
Newer Older
Henry's avatar
Henry committed
1 2 3 4
package commands

import (
	"errors"
5
	"fmt"
Henry's avatar
Henry committed
6
	"io"
7
	"os"
Henry's avatar
Henry committed
8 9

	"github.com/jbenet/go-ipfs/core"
10
	"github.com/jbenet/go-ipfs/updates"
Henry's avatar
Henry committed
11 12 13 14
)

// UpdateApply applys an update of the ipfs binary and shuts down the node if successful
func UpdateApply(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
15 16 17 18 19 20 21 22 23 24 25 26
	fmt.Fprintln(out, "Current Version:", updates.Version)
	u, err := updates.CheckForUpdate()
	if err != nil {
		return err
	}

	if u == nil {
		fmt.Fprintln(out, "No update available")
		return nil
	}
	fmt.Fprintln(out, "New Version:", u.Version)

27
	_, onDaemon := opts["onDaemon"]
28
	force := opts["force"].(bool)
29 30 31 32 33
	if onDaemon && !force {
		return fmt.Errorf(`Error: update must stop running ipfs service.
You may want to abort the update, or shut the service down manually.
To shut it down automatically, run:

34
  ipfs update --force
35 36 37
`)
	}

Henry's avatar
Henry committed
38
	if err = updates.Apply(u); err != nil {
39
		fmt.Fprint(out, err.Error())
Henry's avatar
Henry committed
40
		return fmt.Errorf("Couldn't apply update: %v", err)
41 42
	}

43 44 45 46 47 48 49 50 51 52
	fmt.Fprintln(out, "Updated applied!")
	if onDaemon {
		if force {
			fmt.Fprintln(out, "Shutting down ipfs service.")
			os.Exit(1) // is there a cleaner shutdown routine?
		} else {
			fmt.Fprintln(out, "You can now restart the ipfs service.")
		}
	}

53
	return nil
Henry's avatar
Henry committed
54 55 56 57
}

// UpdateCheck checks wether there is an update available
func UpdateCheck(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
58 59 60 61 62 63 64 65 66 67 68 69 70
	fmt.Fprintln(out, "Current Version:", updates.Version)
	u, err := updates.CheckForUpdate()
	if err != nil {
		return err
	}

	if u == nil {
		fmt.Fprintln(out, "No update available")
		return nil
	}

	fmt.Fprintln(out, "New Version:", u.Version)
	return nil
Henry's avatar
Henry committed
71 72
}

73
// UpdateLog lists the version available online
Henry's avatar
Henry committed
74
func UpdateLog(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
75
	return errors.New("Not yet implemented")
Henry's avatar
Henry committed
76
}