/
cmd_update.go
169 lines (149 loc) · 3.95 KB
/
cmd_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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"github.com/keybase/cli"
"github.com/keybase/client/go/install"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
)
// NewCmdUpdate are commands for supporting the updater
func NewCmdUpdate(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "update",
Usage: "The updater",
ArgumentHelp: "[arguments...]",
HideHelp: true,
Subcommands: []cli.Command{
newCmdUpdateCheck(cl, g), // Deprecated
newCmdUpdateCheckInUse(cl, g),
newCmdUpdateNotify(cl, g),
},
}
}
func newCmdUpdateCheck(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "check",
Usage: "Check for update",
Action: func(c *cli.Context) {
if libkb.IsBrewBuild {
g.Log.Errorf("\nTo update, run:\n\n\tbrew upgrade keybase")
return
}
updaterPath, err := install.UpdaterBinPath()
if err != nil {
g.Log.Errorf("Error finding updater path: %s", err)
return
}
cmd := exec.Command(updaterPath, "check")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
g.Log.Errorf("Error running %q: %s", updaterPath, err)
}
},
}
}
// newCmdUpdateCheckInUse is called by updater to see if Keybase is currently in use
func newCmdUpdateCheckInUse(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "check-in-use",
ArgumentHelp: "",
Usage: "Check if we are in use (safe for restart)",
Action: func(c *cli.Context) {
cl.SetLogForward(libcmdline.LogForwardNone)
cl.SetForkCmd(libcmdline.NoFork)
cl.ChooseCommand(newCmdUpdateCheckInUseRunner(g), "check-in-use", c)
},
}
}
type cmdUpdateCheckInUse struct {
libkb.Contextified
}
func newCmdUpdateCheckInUseRunner(g *libkb.GlobalContext) *cmdUpdateCheckInUse {
return &cmdUpdateCheckInUse{
Contextified: libkb.NewContextified(g),
}
}
func (v *cmdUpdateCheckInUse) GetUsage() libkb.Usage {
return libkb.Usage{
API: true,
Config: true,
}
}
func (v *cmdUpdateCheckInUse) ParseArgv(ctx *cli.Context) error {
return nil
}
type checkInUseResult struct {
InUse bool `json:"in_use"`
}
func (v *cmdUpdateCheckInUse) Run() error {
mountDir, err := v.G().Env.GetMountDir()
if err != nil {
return err
}
inUse := install.IsInUse(mountDir, v.G().Log)
result := checkInUseResult{InUse: inUse}
out, err := json.MarshalIndent(result, "", " ")
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "%s\n", out)
return nil
}
func newCmdUpdateNotify(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "notify",
ArgumentHelp: "<event>",
Usage: "Notify the service about an update event",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "f, force",
Usage: "Force action",
},
},
Action: func(c *cli.Context) {
cl.SetLogForward(libcmdline.LogForwardNone)
cl.SetForkCmd(libcmdline.NoFork)
cl.ChooseCommand(newCmdUpdateNotifyRunner(g), "notify", c)
},
}
}
type cmdUpdateNotify struct {
libkb.Contextified
force bool
event string
}
func newCmdUpdateNotifyRunner(g *libkb.GlobalContext) *cmdUpdateNotify {
return &cmdUpdateNotify{
Contextified: libkb.NewContextified(g),
}
}
func (v *cmdUpdateNotify) GetUsage() libkb.Usage {
return libkb.Usage{
API: true,
Config: true,
}
}
func (v *cmdUpdateNotify) ParseArgv(ctx *cli.Context) error {
v.force = ctx.Bool("force")
v.event = ctx.Args().First()
if v.event == "" {
return fmt.Errorf("No event specified")
}
return nil
}
func (v *cmdUpdateNotify) Run() error {
v.G().Log.Debug("Received event: %s", v.event)
switch v.event {
case "after-apply":
// Deprecated (no longer called by go-updater)
return nil
default:
return fmt.Errorf("Unrecognized event: %s", v.event)
}
}