feat: füge Unterstützung für das Hinzufügen und Löschen von Servern hinzu, aktualisiere die Konfiguration und verbessere die Benutzeroberfläche
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
serveractions "manage-servers/server-actions"
|
||||
"manage-servers/webserver"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func main() {
|
||||
servers, err := loadServers("./config/servers.json")
|
||||
servers, err := loadConfig()
|
||||
if err != nil {
|
||||
fmt.Printf("Error loading servers: %v\n", err)
|
||||
return
|
||||
@@ -73,18 +74,26 @@ func printUsage() {
|
||||
fmt.Println(" serve - Start a web server to manage servers")
|
||||
}
|
||||
|
||||
func loadServers(filename string) ([]serveractions.Server, error) {
|
||||
var servers []serveractions.Server
|
||||
|
||||
file, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
fmt.Println("No servers configuration found, please create a servers.json file in the config folder.")
|
||||
} else {
|
||||
func loadConfig() ([]serveractions.Server, error) {
|
||||
viper.SetConfigName("servers") // name of config file (without extension)
|
||||
viper.SetConfigType("json") // or viper.SetConfigType("YAML")
|
||||
viper.AddConfigPath("./config") // path to look for the config file in
|
||||
viper.AddConfigPath(".") // optionally look for config in the working directory
|
||||
err := viper.ReadInConfig() // Find and read the config file
|
||||
if err != nil { // Handle errors reading the config file
|
||||
// Check if the error is that the file doesn't exist
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
// Config file not found; ignore error and return empty server list
|
||||
return []serveractions.Server{}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("fatal error config file: %w", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(file, &servers)
|
||||
var servers []serveractions.Server
|
||||
err = viper.UnmarshalKey("servers", &servers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}}
|
||||
return nil, fmt.Errorf("unable to decode into struct, %v", err)
|
||||
}
|
||||
|
||||
return servers, nil
|
||||
}
|
||||
@@ -92,6 +101,6 @@ func loadServers(filename string) ([]serveractions.Server, error) {
|
||||
func listServers(servers []serveractions.Server) {
|
||||
fmt.Println("Configured Servers:")
|
||||
for _, s := range servers {
|
||||
fmt.Printf(" - %s (%s) - %s\n", s.Name, s.IP, s.Mac)
|
||||
fmt.Printf(" - %s (%s) - %s - User: %s\n", s.Name, s.IP, s.Mac, s.SSHUser)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user