core/commands2: Added 'update' command
Matt Bell committed
Nov 7, 2014 at 15:41 UTC
36dfb7bc43eaadb295ba7c9741077480433de411
2 files changed
+156
core/commands2/root.go
+1
@@ -70,6 +70,7 @@ var rootSubcommands = map[string]*cmds.Command{
70
"bootstrap": bootstrapCmd,
71
"mount": mountCmd,
72
"block": blockCmd,
73
+ "update": updateCmd,
74
75
// test subcommands
76
// TODO: remove these when we don't need them anymore
core/commands2/update.go
new
+155
@@ -0,0 +1,155 @@
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
+ Help: `ipfs update - check for updates and apply them
19
+
20
+ ipfs update - apply
21
+ ipfs update check - just check
22
+ ipfs update log - list the changelogs
23
+
24
+ipfs update is a utility command used to check for updates and apply them.
25
+`,
26
+ Run: func(res cmds.Response, req cmds.Request) {
27
+ n := req.Context().Node
28
+
29
+ output, err := updateApply(n)
30
+ if err != nil {
31
+ res.SetError(err, cmds.ErrNormal)
32
+ return
33
+ }
34
+
35
+ res.SetOutput(output)
36
+ },
37
+ Type: &UpdateOutput{},
38
+ Subcommands: map[string]*cmds.Command{
39
+ "check": updateCheckCmd,
40
+ "log": updateLogCmd,
41
+ },
42
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
43
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
44
+ v := res.Output().(*UpdateOutput)
45
+ s := ""
46
+ if v.NewVersion != v.OldVersion {
47
+ s = fmt.Sprintf("Successfully updated to IPFS version '%s' (from '%s')",
48
+ v.NewVersion, v.OldVersion)
49
+ } else {
50
+ s = fmt.Sprintf("Already updated to latest version ('%s')", v.NewVersion)
51
+ }
52
+ return []byte(s), nil
53
+ },
54
+ },
55
+}
56
+
57
+var updateCheckCmd = &cmds.Command{
58
+ Help: `ipfs update check <key>`,
59
+ Run: func(res cmds.Response, req cmds.Request) {
60
+ n := req.Context().Node
61
+
62
+ output, err := updateCheck(n)
63
+ if err != nil {
64
+ res.SetError(err, cmds.ErrNormal)
65
+ return
66
+ }
67
+
68
+ res.SetOutput(output)
69
+ },
70
+ Type: &UpdateOutput{},
71
+ Marshallers: map[cmds.EncodingType]cmds.Marshaller{
72
+ cmds.Text: func(res cmds.Response) ([]byte, error) {
73
+ v := res.Output().(*UpdateOutput)
74
+ s := ""
75
+ if v.NewVersion != v.OldVersion {
76
+ s = fmt.Sprintf("A new version of IPFS is available ('%s', currently running '%s')",
77
+ v.NewVersion, v.OldVersion)
78
+ } else {
79
+ s = fmt.Sprintf("Already updated to latest version ('%s')", v.NewVersion)
80
+ }
81
+ return []byte(s), nil
82
+ },
83
+ },
84
+}
85
+
86
+var updateLogCmd = &cmds.Command{
87
+ Help: `ipfs update log - list the last versions and their changelog`,
88
+ Run: func(res cmds.Response, req cmds.Request) {
89
+ n := req.Context().Node
90
+
91
+ output, err := updateLog(n)
92
+ if err != nil {
93
+ res.SetError(err, cmds.ErrNormal)
94
+ return
95
+ }
96
+
97
+ res.SetOutput(output)
98
+ },
99
+}
100
+
101
+// updateApply applies an update of the ipfs binary and shuts down the node if successful
102
+func updateApply(n *core.IpfsNode) (*UpdateOutput, error) {
103
+ // TODO: 'force bool' param that stops the daemon (if running) before update
104
+
105
+ output := &UpdateOutput{
106
+ OldVersion: updates.Version,
107
+ }
108
+
109
+ u, err := updates.CheckForUpdate()
110
+ if err != nil {
111
+ return nil, err
112
+ }
113
+
114
+ if u == nil {
115
+ output.NewVersion = updates.Version
116
+ return output, nil
117
+ }
118
+
119
+ output.NewVersion = u.Version
120
+
121
+ if n.OnlineMode() {
122
+ return nil, errors.New(`You must stop the IPFS daemon before updating.`)
123
+ }
124
+
125
+ if err = updates.Apply(u); err != nil {
126
+ return nil, err
127
+ }
128
+
129
+ return output, nil
130
+}
131
+
132
+// updateCheck checks wether there is an update available
133
+func updateCheck(n *core.IpfsNode) (*UpdateOutput, error) {
134
+ output := &UpdateOutput{
135
+ OldVersion: updates.Version,
136
+ }
137
+
138
+ u, err := updates.CheckForUpdate()
139
+ if err != nil {
140
+ return nil, err
141
+ }
142
+
143
+ if u == nil {
144
+ output.NewVersion = updates.Version
145
+ return output, nil
146
+ }
147
+
148
+ output.NewVersion = u.Version
149
+ return output, nil
150
+}
151
+
152
+// updateLog lists the version available online
153
+func updateLog(n *core.IpfsNode) (interface{}, error) {
154
+ return nil, errors.New("Not yet implemented")
155
+}