-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv.go
More file actions
132 lines (111 loc) · 3.15 KB
/
env.go
File metadata and controls
132 lines (111 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"runtime"
"time"
"github.com/joho/godotenv"
)
const (
defaultConfig = `# NetWatcher Agent Configuration
# See: https://docs.netwatcher.io/agent/configuration
# Controller host (host:port or domain, no protocol needed)
CONTROLLER_HOST=api.netwatcher.io
# Enable SSL (true = HTTPS/WSS, false = HTTP/WS)
CONTROLLER_SSL=true
# Agent credentials (provided during agent creation in the panel)
WORKSPACE_ID=
AGENT_ID=
AGENT_PIN=
# Optional: PSK is saved here after initial bootstrap
# AGENT_PSK=
`
)
var (
// These will be set at build time using -ldflags
VERSION string = "dev" // Git tag or version
buildDate string = "unknown" // Build date
gitCommit string = "unknown" // Git commit hash
)
// getExecutableHash returns the SHA256 hash of the running executable
// This is useful for verifying the binary integrity
func getExecutableHash() (string, error) {
exePath, err := os.Executable()
if err != nil {
return "", err
}
fileBytes, err := os.ReadFile(exePath)
if err != nil {
return "", err
}
hash := sha256.Sum256(fileBytes)
return hex.EncodeToString(hash[:8]), nil // First 8 bytes (16 hex chars)
}
// getVersionInfo returns formatted version information
func getVersionInfo() string {
if VERSION == "dev" {
// Development build - show runtime hash
if hash, err := getExecutableHash(); err == nil {
return fmt.Sprintf("dev-%s", hash)
}
return "dev-unknown"
}
// Release build - use build-time version
return VERSION
}
// getBuildInfo returns detailed build information
func getBuildInfo() string {
info := fmt.Sprintf("Version: %s\n", getVersionInfo())
info += fmt.Sprintf("Build Date: %s\n", buildDate)
info += fmt.Sprintf("Git Commit: %s\n", gitCommit)
info += fmt.Sprintf("Go Version: %s\n", runtime.Version())
info += fmt.Sprintf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
// Add runtime hash for verification
if hash, err := getExecutableHash(); err == nil {
info += fmt.Sprintf("Binary Hash: %s\n", hash)
}
return info
}
func loadConfig(configFile string) error {
// Display version information
fmt.Printf("NetWatcher %s - Copyright (c) 2024-%d Shaun Agostinho\n",
getVersionInfo(), time.Now().Year())
// In verbose mode or development, show full build info
if os.Getenv("VERBOSE") == "true" || VERSION == "dev" {
fmt.Printf("\nBuild Information:\n%s\n", getBuildInfo())
}
_, err := os.Stat(configFile)
if errors.Is(err, os.ErrNotExist) {
fmt.Printf("Config file '%s' does not exist, creating one now.\n", configFile)
_, err = os.Create(configFile)
if err != nil {
return err
}
err = os.WriteFile(configFile, []byte(defaultConfig), 0644)
if err != nil {
return err
}
} else if err != nil {
return err
}
if os.Getenv("ENVIRONMENT") == "PRODUCTION" {
fmt.Printf("Running in PRODUCTION mode.\n")
} else {
fmt.Printf("Running in DEVELOPMENT mode.\n")
}
err = godotenv.Load(configFile)
if err != nil {
return err
}
return nil
}
// Add a version command handler if needed
func handleVersionFlag() {
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
fmt.Print(getBuildInfo())
os.Exit(0)
}
}