Skip to content

Commit 6913884

Browse files
committed
feat: vps command implementation
1 parent 3a5e3aa commit 6913884

File tree

15 files changed

+314
-23
lines changed

15 files changed

+314
-23
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/hapi
1+
.idea/
2+
hapi

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func init() {
4343
return validFormats, cobra.ShellCompDirectiveNoFileComp
4444
})
4545

46-
RootCmd.AddCommand(vps.VpsGroupCmd)
46+
RootCmd.AddCommand(vps.GroupCmd)
4747
}
4848

4949
func initConfig() {

cmd/vps/recovery/recovery.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package recovery
2+
3+
import "github.com/spf13/cobra"
4+
5+
var GroupCmd = &cobra.Command{
6+
Use: "recovery",
7+
Short: "Recovery mode management",
8+
Long: `Initiate or stop recovery mode to perform system rescue operations. This category enables you to boot a virtual machine into a state suitable for repairing file systems or recovering data.`,
9+
}
10+
11+
func init() {
12+
GroupCmd.AddCommand(StartCmd)
13+
GroupCmd.AddCommand(StopCmd)
14+
}

cmd/vps/recovery/start.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package recovery
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/client"
7+
"github.com/hostinger/api-cli/output"
8+
"github.com/hostinger/api-cli/utils"
9+
"github.com/spf13/cobra"
10+
"log"
11+
)
12+
13+
var StartCmd = &cobra.Command{
14+
Use: "start <virtual machine ID>",
15+
Short: "Start recovery mode",
16+
Long: `This endpoint initiates the recovery mode for a specified virtual machine.
17+
Recovery mode is a special state that allows users to perform system rescue operations, such as repairing file systems,
18+
recovering data, or troubleshooting issues that prevent the virtual machine from booting normally.
19+
20+
Virtual machine will boot recovery disk image and original disk image will be mounted in /mnt directory.`,
21+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
22+
Run: func(cmd *cobra.Command, args []string) {
23+
r, err := api.Request().VPSStartRecoveryModeV1WithResponse(context.TODO(), utils.StringToInt(args[0]), startRequestParameters(cmd))
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
output.Format(cmd, r.Body, r.StatusCode())
29+
},
30+
}
31+
32+
func init() {
33+
StartCmd.Flags().StringP("password", "", "", "Temporary root password for recovery mode")
34+
35+
StartCmd.MarkFlagRequired("password")
36+
}
37+
38+
func startRequestParameters(cmd *cobra.Command) client.VPSStartRecoveryModeV1JSONRequestBody {
39+
password, _ := cmd.Flags().GetString("password")
40+
41+
return client.VPSStartRecoveryModeV1JSONRequestBody{
42+
RootPassword: password,
43+
}
44+
}

cmd/vps/recovery/stop.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package recovery
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/output"
7+
"github.com/hostinger/api-cli/utils"
8+
"github.com/spf13/cobra"
9+
"log"
10+
)
11+
12+
var StopCmd = &cobra.Command{
13+
Use: "stop <virtual machine ID>",
14+
Short: "Stop recovery mode",
15+
Long: `This endpoint stops the recovery mode for a specified virtual machine.
16+
If virtual machine is not in recovery mode, this operation will fail.`,
17+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
18+
Run: func(cmd *cobra.Command, args []string) {
19+
r, err := api.Request().VPSStopRecoveryModeV1WithResponse(context.TODO(), utils.StringToInt(args[0]))
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
24+
output.Format(cmd, r.Body, r.StatusCode())
25+
},
26+
}

cmd/vps/snapshots/create.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package snapshots
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/output"
7+
"github.com/hostinger/api-cli/utils"
8+
"github.com/spf13/cobra"
9+
"log"
10+
)
11+
12+
var CreateCmd = &cobra.Command{
13+
Use: "create <virtual machine ID>",
14+
Short: "Create snapshot",
15+
Long: `This endpoint creates a snapshot of a specified virtual machine. A snapshot captures the state and data of
16+
the virtual machine at a specific point in time, allowing users to restore the virtual machine to that state if needed.
17+
This operation is useful for backup purposes, system recovery, and testing changes without affecting the current state
18+
of the virtual machine.
19+
20+
Creating new snapshot will overwrite the existing snapshot!`,
21+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
22+
Run: func(cmd *cobra.Command, args []string) {
23+
r, err := api.Request().VPSCreateSnapshotV1WithResponse(context.TODO(), utils.StringToInt(args[0]))
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
output.Format(cmd, r.Body, r.StatusCode())
29+
},
30+
}

cmd/vps/snapshots/delete.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package snapshots
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/output"
7+
"github.com/hostinger/api-cli/utils"
8+
"github.com/spf13/cobra"
9+
"log"
10+
)
11+
12+
var DeleteCmd = &cobra.Command{
13+
Use: "delete <virtual machine ID>",
14+
Short: "Delete snapshot",
15+
Long: `This endpoint deletes a snapshot of a specified virtual machine.`,
16+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
17+
Run: func(cmd *cobra.Command, args []string) {
18+
r, err := api.Request().VPSDeleteSnapshotV1WithResponse(context.TODO(), utils.StringToInt(args[0]))
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
output.Format(cmd, r.Body, r.StatusCode())
24+
},
25+
}

cmd/vps/snapshots/item.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package snapshots
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/output"
7+
"github.com/hostinger/api-cli/utils"
8+
"github.com/spf13/cobra"
9+
"log"
10+
)
11+
12+
var ItemCmd = &cobra.Command{
13+
Use: "get <virtual machine ID>",
14+
Short: "Get snapshot",
15+
Long: `This endpoint retrieves a snapshot for a specified virtual machine.`,
16+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
17+
Run: func(cmd *cobra.Command, args []string) {
18+
r, err := api.Request().VPSGetSnapshotV1WithResponse(context.TODO(), utils.StringToInt(args[0]))
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
output.Format(cmd, r.Body, r.StatusCode())
24+
},
25+
}

cmd/vps/snapshots/restore.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package snapshots
2+
3+
import (
4+
"context"
5+
"github.com/hostinger/api-cli/api"
6+
"github.com/hostinger/api-cli/output"
7+
"github.com/hostinger/api-cli/utils"
8+
"github.com/spf13/cobra"
9+
"log"
10+
)
11+
12+
var RestoreCmd = &cobra.Command{
13+
Use: "restore <virtual machine ID>",
14+
Short: "Restore snapshot",
15+
Long: `This endpoint restores a specified virtual machine to a previous state using a snapshot. Restoring from a
16+
snapshot allows users to revert the virtual machine to that state, which is useful for system recovery,
17+
undoing changes, or testing.`,
18+
Args: cobra.MatchAll(cobra.ExactArgs(1)),
19+
Run: func(cmd *cobra.Command, args []string) {
20+
r, err := api.Request().VPSRestoreSnapshotV1WithResponse(context.TODO(), utils.StringToInt(args[0]))
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
output.Format(cmd, r.Body, r.StatusCode())
26+
},
27+
}

cmd/vps/snapshots/snapshots.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package snapshots
2+
3+
import "github.com/spf13/cobra"
4+
5+
var GroupCmd = &cobra.Command{
6+
Use: "snapshots",
7+
Short: "Snapshot management",
8+
Long: `Create, restore, or delete snapshots that capture the state of your virtual machines at a given point,
9+
allowing you to quickly recover or test changes without affecting current operations.`,
10+
}
11+
12+
func init() {
13+
GroupCmd.AddCommand(ItemCmd)
14+
GroupCmd.AddCommand(CreateCmd)
15+
GroupCmd.AddCommand(DeleteCmd)
16+
GroupCmd.AddCommand(RestoreCmd)
17+
}

0 commit comments

Comments
 (0)