mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 17:59:28 +08:00
refactor: streamline key retrieval logic in agent.go
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"io/ioutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -22,19 +21,19 @@ func main() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
var pubKey []byte
|
// Try to get the key from the KEY environment variable.
|
||||||
if pubKeyEnv, exists := os.LookupEnv("KEY"); exists {
|
pubKey := []byte(os.Getenv("KEY"))
|
||||||
pubKey = []byte(pubKeyEnv)
|
|
||||||
} else {
|
// If KEY is not set, try to read the key from the file specified by KEY_FILE.
|
||||||
keyFile := os.Getenv("KEY_FILE")
|
if len(pubKey) == 0 {
|
||||||
if keyFile != "" {
|
keyFile, exists := os.LookupEnv("KEY_FILE")
|
||||||
if keyData, err := ioutil.ReadFile(keyFile); err == nil {
|
if !exists {
|
||||||
pubKey = keyData
|
log.Fatal("Must set KEY or KEY_FILE environment variable")
|
||||||
} else {
|
}
|
||||||
log.Fatalf("Failed to read key from file '%s': %v", keyFile, err)
|
var err error
|
||||||
}
|
pubKey, err = os.ReadFile(keyFile)
|
||||||
} else {
|
if err != nil {
|
||||||
log.Fatal("KEY environment variable is not set, and KEY_FILE environment variable is not set")
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user