Skip to content

Commit b4329b7

Browse files
authored
Add ha os config swap command set (#555)
Add commands interfacing with the swap API added in home-assistant/supervisor#5770
1 parent bb2322d commit b4329b7

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

cmd/os_config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var osConfigCmd = &cobra.Command{
8+
Use: "config",
9+
Aliases: []string{"conf", "cfg"},
10+
Short: "Show or change Home Assistant OS settings",
11+
Long: `
12+
This command allows you to show or change settings of Home Assistant OS.`,
13+
Example: `
14+
ha os config swap`,
15+
}
16+
17+
func init() {
18+
osCmd.AddCommand(osConfigCmd)
19+
}

cmd/os_config_swap.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var osConfigSwapCmd = &cobra.Command{
8+
Use: "swap",
9+
Aliases: []string{"sw"},
10+
Short: "Show or change Home Assistant OS swap settings",
11+
Long: `
12+
This command allows you to show or change current swap configuration
13+
of Home Assistant OS.`,
14+
Example: `
15+
ha os config swap info`,
16+
}
17+
18+
func init() {
19+
osConfigCmd.AddCommand(osConfigSwapCmd)
20+
}

cmd/os_config_swap_info.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
helper "github.com/home-assistant/cli/client"
6+
log "github.com/sirupsen/logrus"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var osConfigSwapInfoCmd = &cobra.Command{
11+
Use: "info",
12+
Aliases: []string{"in", "info"},
13+
Short: "Show HAOS swap settings",
14+
Long: `
15+
This command allows you to see how swap is used by the Home Assistant OS.`,
16+
Example: `
17+
ha os config swap info`,
18+
ValidArgsFunction: cobra.NoFileCompletions,
19+
Args: cobra.NoArgs,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
log.WithField("args", args).Debug("os config swap info")
22+
23+
section := "os"
24+
command := "config/swap"
25+
26+
resp, err := helper.GenericJSONGet(section, command)
27+
if err != nil {
28+
fmt.Println(err)
29+
ExitWithError = true
30+
} else {
31+
ExitWithError = !helper.ShowJSONResponse(resp)
32+
}
33+
},
34+
}
35+
36+
func init() {
37+
osConfigSwapCmd.AddCommand(osConfigSwapInfoCmd)
38+
}

cmd/os_config_swap_options.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
helper "github.com/home-assistant/cli/client"
6+
log "github.com/sirupsen/logrus"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var osConfigSwapOptionsCmd = &cobra.Command{
11+
Use: "options",
12+
Aliases: []string{"option", "opt", "opts", "op"},
13+
Short: "Change HAOS swap settings",
14+
Long: `
15+
This command allows you to override how the Home Assistant OS uses swap.`,
16+
Example: `
17+
ha os config swap options --swap-size=2G --swappiness=10`,
18+
ValidArgsFunction: cobra.NoFileCompletions,
19+
Args: cobra.NoArgs,
20+
Run: func(cmd *cobra.Command, args []string) {
21+
log.WithField("args", args).Debug("os config swap options")
22+
23+
section := "os"
24+
command := "config/swap"
25+
26+
options := make(map[string]any)
27+
28+
swapSize, err := cmd.Flags().GetString("swap-size")
29+
if err == nil && cmd.Flags().Changed("swap-size") {
30+
options["swap_size"] = swapSize
31+
}
32+
33+
swappiness, err := cmd.Flags().GetInt("swappiness")
34+
if err == nil && cmd.Flags().Changed("swappiness") {
35+
options["swappiness"] = swappiness
36+
}
37+
38+
resp, err := helper.GenericJSONPost(section, command, options)
39+
if err != nil {
40+
fmt.Println(err)
41+
ExitWithError = true
42+
} else {
43+
ExitWithError = !helper.ShowJSONResponse(resp)
44+
}
45+
},
46+
}
47+
48+
func init() {
49+
osConfigSwapOptionsCmd.Flags().String("swap-size", "", "Swap size in bytes with optional units (K/M/G)")
50+
osConfigSwapOptionsCmd.Flags().Int("swappiness", 1, "Kernel swappiness value (0-200)")
51+
52+
osConfigSwapCmd.AddCommand(osConfigSwapOptionsCmd)
53+
}

0 commit comments

Comments
 (0)