forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
182 lines (155 loc) · 4.33 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package commands
import (
"bytes"
"errors"
"fmt"
"io"
cmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/updates"
)
type UpdateOutput struct {
OldVersion string
NewVersion string
}
var UpdateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Downloads and installs updates for IPFS (disabled)",
ShortDescription: `ipfs update is disabled until we can deploy the binaries to you over ipfs itself.
please use 'go get -u github.com/ipfs/go-ipfs/cmd/ipfs' until then.`,
},
}
// TODO: unexported until we can deploy the binaries over ipfs
var updateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Downloads and installs updates for IPFS",
ShortDescription: "ipfs update is a utility command used to check for updates and apply them.",
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output, err := updateApply(n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(output)
},
Type: UpdateOutput{},
Subcommands: map[string]*cmds.Command{
"check": UpdateCheckCmd,
"log": UpdateLogCmd,
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*UpdateOutput)
buf := new(bytes.Buffer)
if v.NewVersion != v.OldVersion {
buf.WriteString(fmt.Sprintf("Successfully updated to IPFS version '%s' (from '%s')\n",
v.NewVersion, v.OldVersion))
} else {
buf.WriteString(fmt.Sprintf("Already updated to latest version ('%s')\n", v.NewVersion))
}
return buf, nil
},
},
}
var UpdateCheckCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Checks if updates are available",
ShortDescription: "'ipfs update check' checks if any updates are available for IPFS.\nNothing will be downloaded or installed.",
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output, err := updateCheck(n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(output)
},
Type: UpdateOutput{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*UpdateOutput)
buf := new(bytes.Buffer)
if v.NewVersion != v.OldVersion {
buf.WriteString(fmt.Sprintf("A new version of IPFS is available ('%s', currently running '%s')\n",
v.NewVersion, v.OldVersion))
} else {
buf.WriteString(fmt.Sprintf("Already updated to latest version ('%s')\n", v.NewVersion))
}
return buf, nil
},
},
}
var UpdateLogCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List the changelog for the latest versions of IPFS",
ShortDescription: "This command is not yet implemented.",
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
output, err := updateLog(n)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(output)
},
}
// updateApply applies an update of the ipfs binary and shuts down the node if successful
func updateApply(n *core.IpfsNode) (*UpdateOutput, error) {
// TODO: 'force bool' param that stops the daemon (if running) before update
output := &UpdateOutput{
OldVersion: updates.Version,
}
u, err := updates.CheckForUpdate()
if err != nil {
return nil, err
}
if u == nil {
output.NewVersion = updates.Version
return output, nil
}
output.NewVersion = u.Version
if n.OnlineMode() {
return nil, errors.New(`You must stop the IPFS daemon before updating.`)
}
if err = updates.Apply(u); err != nil {
return nil, err
}
return output, nil
}
// updateCheck checks wether there is an update available
func updateCheck(n *core.IpfsNode) (*UpdateOutput, error) {
output := &UpdateOutput{
OldVersion: updates.Version,
}
u, err := updates.CheckForUpdate()
if err != nil {
return nil, err
}
if u == nil {
output.NewVersion = updates.Version
return output, nil
}
output.NewVersion = u.Version
return output, nil
}
// updateLog lists the version available online
func updateLog(n *core.IpfsNode) (interface{}, error) {
// TODO
return nil, errors.New("Not yet implemented")
}