Browse Source

change * to all and add logs command

main
forest 3 months ago
parent
commit
8e9bc591bc
5 changed files with 102 additions and 4 deletions
  1. +1
    -0
      .gitignore
  2. +6
    -0
      daemon-entities.go
  3. +73
    -0
      logs.go
  4. +21
    -3
      main.go
  5. +1
    -1
      rm.go

+ 1
- 0
.gitignore View File

@ -1 +1,2 @@
greenhouse

+ 6
- 0
daemon-entities.go View File

@ -61,3 +61,9 @@ type ThresholdClientState struct {
CurrentState string
LastState string
}
type LogEntity struct {
Timestamp int64
Line string
Stderr bool
}

+ 73
- 0
logs.go View File

@ -0,0 +1,73 @@
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)
}
}

+ 21
- 3
main.go View File

@ -117,6 +117,8 @@ func main() {
ls(args)
} else if command == "rm" {
rm(args)
} else if command == "logs" {
logs(args)
} else {
fmt.Printf(`
Unknown command '%s'.
@ -130,7 +132,7 @@ func displayHelpAndExit(topic string) {
defaultHelp := `
Usage: greenhouse COMMAND [...]
Commands: status, register, tunnel, ls, rm
Commands: status, register, tunnel, ls, rm, logs
To read the instructions for a specific command, you can run for example:
@ -173,12 +175,12 @@ Usage: greenhouse ls [LS_OPTIONS]...
Usage: greenhouse rm LISTEN_URL [...]
Remove one or more tunnels by LISTEN_URL.
Specifying "*" will remove all tunnels.
Specifying "all" will remove all tunnels.
e.g:
greenhouse rm https://www.cli-user.greenhouseusers.com
greenhouse rm https://www.cli-user.greenhouseusers.com https://files.cli-user.greenhouseusers.com
greenhouse rm *
greenhouse rm all
`,
"tunnel": `
@ -195,6 +197,22 @@ Usage: greenhouse tunnel LISTEN_URL to LOCAL_URL
greenhouse tunnel https://files.cli-user.greenhouseusers.com to file:///home/cli-user/Public
greenhouse tunnel tcp://:10014 to tcp://localhost:22
greenhouse tunnel --json '{"domain": "cli-user.greenhouseusers.com", "public_port": 443, ...}'
`,
"logs": `
Usage: greenhouse logs SOURCE [OPTIONS]
Display the logs from the Greenhouse Daemon and its two underlying services, threshold and caddy.
SOURCE must be 'daemon', 'threshold', 'caddy', or 'all'
Valid options are:
-n <number> to only display the most recent <number> lines from the log.
-f to follow the logs as they are written in real time.
e.g:
greenhouse logs daemon
greenhouse logs ALL -n 100
greenhouse logs caddy -f
`,
"config": `
This greenhouse CLI accepts the following configuration environment or shell variables:


+ 1
- 1
rm.go View File

@ -27,7 +27,7 @@ func rm(args []string) {
for i, tunnel := range status.GUITunnels {
listenURL := tunnelListenURL(&tunnel)
for j, arg := range args {
if arg == "*" || listenURL == arg {
if arg == "all" || listenURL == arg {
matchedTunnelIndexes[i] = true
matchedArgumentIndexes[j] = true
}


Loading…
Cancel
Save