github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/core/commands2/update.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	cmds "github.com/jbenet/go-ipfs/commands"
     8  	"github.com/jbenet/go-ipfs/core"
     9  	"github.com/jbenet/go-ipfs/updates"
    10  )
    11  
    12  type UpdateOutput struct {
    13  	OldVersion string
    14  	NewVersion string
    15  }
    16  
    17  var updateCmd = &cmds.Command{
    18  	Description: "Downloads and installs updates for IPFS",
    19  	Help: `ipfs update is a utility command used to check for updates and apply them.
    20  `,
    21  
    22  	Run: func(req cmds.Request) (interface{}, error) {
    23  		n := req.Context().Node
    24  		return updateApply(n)
    25  	},
    26  	Type: &UpdateOutput{},
    27  	Subcommands: map[string]*cmds.Command{
    28  		"check": updateCheckCmd,
    29  		"log":   updateLogCmd,
    30  	},
    31  	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
    32  		cmds.Text: func(res cmds.Response) ([]byte, error) {
    33  			v := res.Output().(*UpdateOutput)
    34  			s := ""
    35  			if v.NewVersion != v.OldVersion {
    36  				s = fmt.Sprintf("Successfully updated to IPFS version '%s' (from '%s')",
    37  					v.NewVersion, v.OldVersion)
    38  			} else {
    39  				s = fmt.Sprintf("Already updated to latest version ('%s')", v.NewVersion)
    40  			}
    41  			return []byte(s), nil
    42  		},
    43  	},
    44  }
    45  
    46  var updateCheckCmd = &cmds.Command{
    47  	Description: "Checks if updates are available",
    48  	Help: `'ipfs update check' checks if any updates are available for IPFS.
    49  
    50  Nothing will be downloaded or installed.
    51  `,
    52  
    53  	Run: func(req cmds.Request) (interface{}, error) {
    54  		n := req.Context().Node
    55  		return updateCheck(n)
    56  	},
    57  	Type: &UpdateOutput{},
    58  	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
    59  		cmds.Text: func(res cmds.Response) ([]byte, error) {
    60  			v := res.Output().(*UpdateOutput)
    61  			s := ""
    62  			if v.NewVersion != v.OldVersion {
    63  				s = fmt.Sprintf("A new version of IPFS is available ('%s', currently running '%s')",
    64  					v.NewVersion, v.OldVersion)
    65  			} else {
    66  				s = fmt.Sprintf("Already updated to latest version ('%s')", v.NewVersion)
    67  			}
    68  			return []byte(s), nil
    69  		},
    70  	},
    71  }
    72  
    73  var updateLogCmd = &cmds.Command{
    74  	Description: "List the changelog for the latest versions of IPFS",
    75  	Help: `This command is not yet implemented.
    76  `,
    77  
    78  	Run: func(req cmds.Request) (interface{}, error) {
    79  		n := req.Context().Node
    80  		return updateLog(n)
    81  	},
    82  }
    83  
    84  // updateApply applies an update of the ipfs binary and shuts down the node if successful
    85  func updateApply(n *core.IpfsNode) (*UpdateOutput, error) {
    86  	// TODO: 'force bool' param that stops the daemon (if running) before update
    87  
    88  	output := &UpdateOutput{
    89  		OldVersion: updates.Version,
    90  	}
    91  
    92  	u, err := updates.CheckForUpdate()
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	if u == nil {
    98  		output.NewVersion = updates.Version
    99  		return output, nil
   100  	}
   101  
   102  	output.NewVersion = u.Version
   103  
   104  	if n.OnlineMode() {
   105  		return nil, errors.New(`You must stop the IPFS daemon before updating.`)
   106  	}
   107  
   108  	if err = updates.Apply(u); err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	return output, nil
   113  }
   114  
   115  // updateCheck checks wether there is an update available
   116  func updateCheck(n *core.IpfsNode) (*UpdateOutput, error) {
   117  	output := &UpdateOutput{
   118  		OldVersion: updates.Version,
   119  	}
   120  
   121  	u, err := updates.CheckForUpdate()
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	if u == nil {
   127  		output.NewVersion = updates.Version
   128  		return output, nil
   129  	}
   130  
   131  	output.NewVersion = u.Version
   132  	return output, nil
   133  }
   134  
   135  // updateLog lists the version available online
   136  func updateLog(n *core.IpfsNode) (interface{}, error) {
   137  	// TODO
   138  	return nil, errors.New("Not yet implemented")
   139  }