2023-07-01 12:41:59 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HostLogsResult struct {
|
|
|
|
Total int32 `json:"total"`
|
|
|
|
Logs []string `json:"logs"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) HostLogs(hostKey, filter string) (result HostLogsResult) {
|
|
|
|
client, ctx, close, err := a.connectToHost(hostKey)
|
|
|
|
if err != nil {
|
|
|
|
result.Error = "Could not connect to host"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
var res bson.M
|
|
|
|
err = client.Database("admin").RunCommand(ctx, bson.M{"getLog": filter}).Decode(&res)
|
|
|
|
if err != nil {
|
|
|
|
runtime.LogWarningf(a.ctx, "Could not get %s logs for %s: %s", filter, hostKey, err.Error())
|
|
|
|
result.Error = err.Error()
|
2023-07-01 13:04:00 +00:00
|
|
|
return
|
2023-07-01 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
2023-07-01 13:03:37 +00:00
|
|
|
if res["totalLinesWritten"] != nil {
|
|
|
|
result.Total = res["totalLinesWritten"].(int32)
|
|
|
|
} else {
|
|
|
|
result.Total = 0
|
|
|
|
}
|
|
|
|
|
2023-07-01 12:41:59 +00:00
|
|
|
result.Logs = make([]string, 0)
|
|
|
|
|
2023-07-01 13:03:37 +00:00
|
|
|
switch res["log"].(type) {
|
|
|
|
case bson.A:
|
|
|
|
for _, v := range res["log"].(bson.A) {
|
|
|
|
result.Logs = append(result.Logs, v.(string))
|
|
|
|
}
|
2023-07-01 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|