Add terminal mode
This commit is contained in:
parent
d88c91c2d4
commit
c61688e51d
10
functions.go
10
functions.go
@ -90,3 +90,13 @@ func renderTemplate(w http.ResponseWriter, statuses []WebsiteStatus) {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func maxLength(strings ...string) int {
|
||||
maxLen := 0
|
||||
for _, str := range strings {
|
||||
if len(str) > maxLen {
|
||||
maxLen = len(str)
|
||||
}
|
||||
}
|
||||
return maxLen
|
||||
}
|
||||
|
69
main.go
69
main.go
@ -8,6 +8,17 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
var Bold = "\033[1m"
|
||||
var Reset = "\033[0m"
|
||||
var Red = "\033[31m"
|
||||
var Green = "\033[32m"
|
||||
var Yellow = "\033[33m"
|
||||
var Blue = "\033[34m"
|
||||
var Purple = "\033[35m"
|
||||
var Cyan = "\033[36m"
|
||||
var Gray = "\033[37m"
|
||||
var White = "\033[97m"
|
||||
|
||||
type WebsiteStatus struct {
|
||||
WebsiteURL string
|
||||
Description string
|
||||
@ -21,6 +32,7 @@ type Config struct {
|
||||
ListenAddress string
|
||||
Port int
|
||||
Verbose bool
|
||||
Color bool
|
||||
}
|
||||
|
||||
var config Config
|
||||
@ -38,7 +50,18 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
runWebsite(fmt.Sprintf("%s:%d", config.ListenAddress, config.Port), websites)
|
||||
switch config.Mode {
|
||||
case "terminal":
|
||||
if config.Verbose {
|
||||
fmt.Printf("Running in terminal mode\n")
|
||||
}
|
||||
runTerminal(websites)
|
||||
case "server":
|
||||
if config.Verbose {
|
||||
fmt.Printf("Running in server mode\n")
|
||||
}
|
||||
runWebsite(fmt.Sprintf("%s:%d", config.ListenAddress, config.Port), websites)
|
||||
}
|
||||
}
|
||||
|
||||
func parseFlags() Config {
|
||||
@ -47,7 +70,8 @@ func parseFlags() Config {
|
||||
verboseFlag := flag.Bool("verbose", false, "Enable verbose mode")
|
||||
listenAddressFlag := flag.String("address", "localhost", "The address the server will listen on")
|
||||
portFlag := flag.Int("port", 8080, "The port the server will listen on")
|
||||
modeFlag := flag.String("mode", "server", "The mode the program will run in. Options are \"cmd\" and \"server\"")
|
||||
modeFlag := flag.String("mode", "server", "The mode the program will run in. Options are \"terminal\" and \"server\"")
|
||||
colorFlag := flag.Bool("color", true, "Use terminal color")
|
||||
|
||||
// Parse command-line flags
|
||||
flag.Parse()
|
||||
@ -59,6 +83,7 @@ func parseFlags() Config {
|
||||
ListenAddress: *listenAddressFlag,
|
||||
Port: *portFlag,
|
||||
Mode: *modeFlag,
|
||||
Color: *colorFlag,
|
||||
}
|
||||
|
||||
success := true
|
||||
@ -74,9 +99,9 @@ func parseFlags() Config {
|
||||
fmt.Printf("Port '%d' is out of range for a port, must be between 0 and 65535\n", config.Port)
|
||||
}
|
||||
|
||||
if config.Mode != "cmd" && config.Mode != "server" {
|
||||
if config.Mode != "terminal" && config.Mode != "server" {
|
||||
success = false
|
||||
fmt.Printf("Mode has to be either cmd or server, but was '%s'\n", config.Mode)
|
||||
fmt.Printf("Mode has to be either terminal or server, but was '%s'\n", config.Mode)
|
||||
}
|
||||
|
||||
if !success {
|
||||
@ -87,6 +112,42 @@ func parseFlags() Config {
|
||||
return config
|
||||
}
|
||||
|
||||
func runTerminal(websites []WebsiteStatus) {
|
||||
var status = make(map[string]string)
|
||||
for _, website := range websites {
|
||||
isUp := ""
|
||||
|
||||
if config.Color {
|
||||
isUp = Bold + Red + "down" + Reset
|
||||
} else {
|
||||
isUp = "down"
|
||||
}
|
||||
|
||||
if checkWebsite(website.WebsiteURL) == nil {
|
||||
if config.Color {
|
||||
isUp = Bold + Green + "up" + Reset
|
||||
} else {
|
||||
isUp = "up"
|
||||
}
|
||||
}
|
||||
|
||||
status[website.WebsiteURL] = isUp
|
||||
|
||||
}
|
||||
|
||||
labelWidth := 0
|
||||
for _, website := range websites {
|
||||
if len(website.Description) > labelWidth {
|
||||
labelWidth = len(website.Description)
|
||||
}
|
||||
}
|
||||
labelWidth += 1
|
||||
|
||||
for _, website := range websites {
|
||||
fmt.Printf("%-*s %s\n", labelWidth, website.Description+":", status[website.WebsiteURL])
|
||||
}
|
||||
}
|
||||
|
||||
func runWebsite(url string, websites []WebsiteStatus) {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
renderTemplate(w, websites)
|
||||
|
Loading…
x
Reference in New Issue
Block a user