🌱🏠🧑‍💻 command line greenhouse client application: an interface to the greenhouse daemon https://greenhouse.server.garden/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func logs(args []string) {
if len(args) < 1 {
fmt.Printf("\nError: SOURCE is required for the logs command\n")
displayHelpAndExit("logs")
return
}
extraParams := ""
if len(args) >= 2 {
if args[1] == "-f" {
if len(args) == 2 {
extraParams = "&follow=true"
} else {
fmt.Printf("\nError: unexpected extra argument '%s' passed to the logs %s -f command\n", args[0], args[2])
displayHelpAndExit("logs")
return
}
} else if args[1] == "-n" {
if len(args) == 3 {
extraParams = fmt.Sprintf("&count=%s", args[2])
} else {
fmt.Printf("\nError: unexpected extra argument '%s' passed to the logs %s -n %s command\n", args[0], args[3], args[4])
displayHelpAndExit("logs")
return
}
} else {
fmt.Printf("\nError: unexpected argument '%s' passed to the logs %s command. expected '-f' or '-n'\n", args[0], args[1])
displayHelpAndExit("logs")
return
}
}
logsURL := fmt.Sprintf("%s/logs?service=%s%s", baseURL, args[0], extraParams)
response, err := httpClient.Get(logsURL)
if err != nil {
fmt.Printf("\nCouldn't reach greenhouse daemon at %s: %s\n", baseURL, err)
os.Exit(1)
return
}
responseBytes, err := ioutil.ReadAll(response.Body)
responseString := "http read error, failed to read response from greenhouse"
if err == nil {
responseString = string(responseBytes)
}
if response.StatusCode != 200 {
fmt.Printf("GET %s returned HTTP %d:\n%s", logsURL, response.StatusCode, responseString)
os.Exit(1)
return
}
var logs []LogEntity
err = json.Unmarshal(responseBytes, &logs)
if err != nil {
fmt.Printf("The response from the daemon at %s could not be parsed.\nResponse text:\n%s\n\nError was: %s", baseURL, responseString, err)
os.Exit(1)
return
}
//fmt.Printf("\n%s\n\n", string(responseBytes))
for _, logEntry := range logs {
fmt.Printf("%s\n", logEntry.Line)
}
}