mirror of
https://github.com/hamburghammer/gstat.git
synced 2024-05-25 16:10:37 +02:00
Augusto Dwenger J.
f004c93773
This implementation still isn't perfect. One of the encoutered problems is that it does not omit Memory sturcts that are technicly empty. Resulting in JSON with empty objects. This should be a problem for other parsers but it doesn't look good.
65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/hamburghammer/gstat/args"
|
|
"github.com/hamburghammer/gstat/commands"
|
|
"github.com/hamburghammer/gstat/output"
|
|
)
|
|
|
|
func main() {
|
|
args := args.Parse()
|
|
|
|
output, err := newOutput(args)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
fmt.Println(string(output))
|
|
}
|
|
|
|
func oldOutput(args args.Arguments) string {
|
|
result := commands.NewResult(args)
|
|
executs := []commands.Executor{
|
|
commands.NewDate(),
|
|
commands.NewCPU(),
|
|
commands.NewMem(),
|
|
commands.NewDisk(),
|
|
commands.NewProcesses(),
|
|
}
|
|
output := result.ExecCommands(executs)
|
|
|
|
return formatToJSON(output.Collection.Results)
|
|
}
|
|
|
|
func formatToJSON(strings []string) string {
|
|
stringBuilder := "{"
|
|
elements := len(strings)
|
|
|
|
for i, s := range strings {
|
|
stringBuilder = stringBuilder + s
|
|
if i != (elements - 1) {
|
|
stringBuilder = stringBuilder + ","
|
|
}
|
|
}
|
|
stringBuilder = stringBuilder + "}"
|
|
|
|
return stringBuilder
|
|
}
|
|
|
|
func newOutput(args args.Arguments) ([]byte, error) {
|
|
result := output.NewResult(args)
|
|
col, errs := result.GatherResults()
|
|
if len(errs) != 0 {
|
|
for _, err := range errs {
|
|
fmt.Println(err)
|
|
}
|
|
return []byte{}, fmt.Errorf("Collection the stats produced an error")
|
|
}
|
|
|
|
return json.Marshal(col)
|
|
}
|