-
Notifications
You must be signed in to change notification settings - Fork 92
/
banner.go
170 lines (150 loc) · 4.43 KB
/
banner.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
170
package cmd
import (
"fmt"
"strings"
"time"
helper "github.com/home-assistant/cli/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const haBanner = `
| | | | /\ (_) | | | |
| |__| | ___ _ __ ___ ___ / \ ___ ___ _ ___| |_ __ _ _ __ | |_
| __ |/ _ \| '_ \ _ \ / _ \ / /\ \ / __/ __| / __| __/ _\ | '_ \| __|
| | | | (_) | | | | | | __/ / ____ \\__ \__ \ \__ \ || (_| | | | | |_
|_| |_|\___/|_| |_| |_|\___| /_/ \_\___/___/_|___/\__\__,_|_| |_|\__|
Welcome to the Home Assistant command line.
`
func supervisorGet(section string, command string) (outdata *(map[string]interface{}), err error) {
resp, err := helper.GenericJSONGet(section, command)
if err != nil {
return nil, err
}
var data *helper.Response
if resp.IsSuccess() {
data = resp.Result().(*helper.Response)
} else {
data = resp.Error().(*helper.Response)
}
if data.Result == "ok" {
if len(data.Data) > 0 {
outdata = &(data.Data)
}
} else {
return nil, fmt.Errorf("Error returned from Supervisor: %s", data.Message)
}
return outdata, nil
}
func getAddresses(addresses []interface{}) string {
addresses_str := make([]string, len(addresses))
for i, v := range addresses {
addresses_str[i] = fmt.Sprint(v)
}
return strings.Join(addresses_str, ", ")
}
var bannerCmd = &cobra.Command{
Use: "banner",
Aliases: []string{"ba"},
Short: "Prints the CLI Home Assistant banner along with some useful information",
Example: `
ha banner
`,
ValidArgsFunction: cobra.NoFileCompletions,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
log.WithField("args", args).Debug("info")
fmt.Print(haBanner)
fmt.Println()
nowait, err := cmd.Flags().GetBool("no-wait")
if err != nil {
fmt.Println(err)
ExitWithError = true
}
if !nowait {
for i := 0; i < 60; i++ {
// We could use ping here, but Supervisor loads networking data asynchronously.
// Networking info are very useful, so wait until networking data have been
// loaded...
netinfo, err := supervisorGet("network", "info")
if err == nil && netinfo != nil {
netifaces, exist := (*netinfo)["interfaces"]
if exist && len(netifaces.([]interface{})) > 0 {
break
}
}
if i == 0 {
fmt.Println("Waiting for Supervisor to startup...")
}
time.Sleep(1 * time.Second)
}
}
fmt.Println("System information")
netinfo, err := supervisorGet("network", "info")
if err != nil {
fmt.Println(err)
ExitWithError = true
return
}
if netinfo == nil {
return
}
// Print network address information
netifaces, exist := (*netinfo)["interfaces"]
if exist {
for _, netiface := range netifaces.([]interface{}) {
nf := netiface.(map[string]interface{})
title_ipv4 := fmt.Sprintf("IPv4 addresses for %s:", nf["interface"])
title_ipv6 := fmt.Sprintf("IPv6 addresses for %s:", nf["interface"])
if nf["ipv4"] == nil {
fmt.Printf(" %-25s (no address)\n", title_ipv4)
} else {
ipv4 := nf["ipv4"].(map[string]interface{})
fmt.Printf(" %-25s %s\n", title_ipv4, getAddresses(ipv4["address"].([]interface{})))
}
if nf["ipv6"] != nil {
ipv6 := nf["ipv6"].(map[string]interface{})
ipv6_addresses := ipv6["address"].([]interface{})
if len(ipv6_addresses) > 0 {
fmt.Printf(" %-25s %s\n", title_ipv6, getAddresses(ipv6_addresses))
}
}
}
} else {
fmt.Printf(" (no networking information)")
}
fmt.Println()
// Print Host URL
hostinfo, err := supervisorGet("host", "info")
if err != nil {
fmt.Println(err)
ExitWithError = true
return
}
if hostinfo == nil {
return
}
coreinfo, err := supervisorGet("core", "info")
if err != nil {
fmt.Println(err)
ExitWithError = true
return
}
if coreinfo == nil {
return
}
protocol := "http"
if (*coreinfo)["ssl"] == "true" {
protocol = "https"
}
port, _ := (*coreinfo)["port"].(float64)
fmt.Printf(" %-25s %s\n", "OS Version:", (*hostinfo)["operating_system"])
fmt.Printf(" %-25s %s\n", "Home Assistant Core:", (*coreinfo)["version"])
fmt.Println()
fmt.Printf(" %-25s %s://%s.local:%d\n", "Home Assistant URL:", protocol, (*hostinfo)["hostname"], int(port))
fmt.Printf(" %-25s http://%s.local:%d\n", "Observer URL:", (*hostinfo)["hostname"], 4357)
},
}
func init() {
rootCmd.AddCommand(bannerCmd)
bannerCmd.Flags().Bool("no-wait", false, "Don't wait until Supervisor is started")
}