/
status.go
62 lines (52 loc) · 1.45 KB
/
status.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
package cmd
import (
"context"
"fmt"
"os"
"github.com/alexandrevilain/devpod-provider-ovhcloud/pkg/options"
"github.com/alexandrevilain/devpod-provider-ovhcloud/pkg/ovhcloud"
"github.com/loft-sh/devpod/pkg/client"
"github.com/loft-sh/devpod/pkg/log"
"github.com/spf13/cobra"
)
// StatusCmd holds the "status" command flags.
type StatusCmd struct{}
// NewStatusCmd creates the "status" command.
func NewStatusCmd() *cobra.Command {
cmd := &StatusCmd{}
statusCmd := &cobra.Command{
Use: "status",
Short: "Retrieve the status of an instance",
RunE: func(_ *cobra.Command, args []string) error {
options, err := options.FromEnv(false)
if err != nil {
return err
}
return cmd.Run(context.Background(), options, log.Default)
},
}
return statusCmd
}
// Run runs the "status" command logic.
func (cmd *StatusCmd) Run(ctx context.Context, options *options.Options, log log.Logger) error {
client, err := ovhcloud.NewClient(options.Authentication)
if err != nil {
return err
}
status, err := client.GetInstanceStatus(ctx, options.MachineID)
if err != nil {
return err
}
_, err = fmt.Fprint(os.Stdout, ovhCloudStatusToDevPodStatus(status))
return err
}
func ovhCloudStatusToDevPodStatus(status ovhcloud.InstanceStatus) string {
switch status {
case ovhcloud.InstanceActive:
return client.StatusRunning
case ovhcloud.InstanceStopped, ovhcloud.InstanceShutOff:
return client.StatusStopped
default:
return client.StatusBusy
}
}