/
reboot.go
68 lines (54 loc) · 1.31 KB
/
reboot.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
package webspace
import (
"context"
"errors"
"time"
"github.com/spf13/cobra"
"github.com/netsoc/cli/pkg/config"
"github.com/netsoc/cli/pkg/util"
webspaced "github.com/netsoc/webspaced/client"
)
type rebootOptions struct {
Config func() (*config.Config, error)
WebspacedClient func() (*webspaced.APIClient, error)
User string
}
// NewCmdReboot creates a new webspace reboot command
func NewCmdReboot(f *util.CmdFactory) *cobra.Command {
opts := rebootOptions{
Config: f.Config,
WebspacedClient: f.WebspacedClient,
}
cmd := &cobra.Command{
Use: "reboot",
Aliases: []string{"restart"},
Short: "Reboot webspace",
RunE: func(cmd *cobra.Command, args []string) error {
return rebootRun(opts)
},
}
util.AddOptUser(cmd, &opts.User)
return cmd
}
func rebootRun(opts rebootOptions) error {
c, err := opts.Config()
if err != nil {
return err
}
if c.Token == "" {
return errors.New("not logged in")
}
client, err := opts.WebspacedClient()
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), webspaced.ContextAccessToken, c.Token)
await, _, t := util.SimpleProgress("Rebooting webspace", 5*time.Second)
defer await()
_, err = client.StateApi.Reboot(ctx, opts.User)
t.MarkAsDone()
if err != nil {
return util.APIError(err)
}
return nil
}