Skip to content

Commit 1a3e579

Browse files
committed
feat: enhance configuration handling and validation
- Added error logging for missing specified config files in `initConfig`. - Updated `ps` command to load and validate configuration from a specified file, defaulting to `lxc-compose.yml`. - Included configuration validation in the `up` command to ensure proper setup before container management. - Enhanced integration tests to check for invalid configuration handling. - Updated example `lxc-compose.yml` to include versioning for better compatibility.
1 parent ca29345 commit 1a3e579

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

cmd/lxc-compose/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func initConfig() {
5353

5454
if err := viper.ReadInConfig(); err == nil {
5555
logging.Info("Using config file", "path", viper.ConfigFileUsed())
56+
} else if cfgFile != "" {
57+
// If a specific config file was requested but not found, that's an error
58+
logging.Error("Failed to read specified config file", "path", cfgFile, "error", err)
59+
os.Exit(1)
5660
}
5761
}
5862

cmd/lxc-compose/ps.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"text/tabwriter"
77

8+
"github.com/larkinwc/proxmox-lxc-compose/pkg/config"
89
"github.com/larkinwc/proxmox-lxc-compose/pkg/container"
910

1011
"github.com/spf13/cobra"
@@ -14,7 +15,26 @@ func init() {
1415
var psCmd = &cobra.Command{
1516
Use: "ps",
1617
Short: "List containers",
17-
RunE: func(_ *cobra.Command, _ []string) error {
18+
RunE: func(cmd *cobra.Command, _ []string) error {
19+
// Get config file from flag or use default
20+
configFile := cmd.Flag("config").Value.String()
21+
if configFile == "" {
22+
configFile = "lxc-compose.yml"
23+
}
24+
25+
// Load and validate configuration if file exists
26+
if _, err := os.Stat(configFile); err == nil {
27+
cfg, err := config.Load(configFile)
28+
if err != nil {
29+
return fmt.Errorf("failed to load config: %w", err)
30+
}
31+
32+
// Validate configuration
33+
if err := config.ValidateConfig(cfg); err != nil {
34+
return fmt.Errorf("configuration validation failed: %w", err)
35+
}
36+
}
37+
1838
// Create container manager
1939
manager, err := container.NewLXCManager("/var/lib/lxc")
2040
if err != nil {

cmd/lxc-compose/up.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func upCmdRunE(_ *cobra.Command, args []string) error {
3636
return fmt.Errorf("failed to load config: %w", err)
3737
}
3838

39+
// Validate configuration
40+
if err := config.ValidateConfig(cfg); err != nil {
41+
return fmt.Errorf("configuration validation failed: %w", err)
42+
}
43+
3944
// Create container manager
4045
manager, err := container.NewLXCManager("/var/lib/lxc")
4146
if err != nil {

integration-test/docker-lxc/advanced-test.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ fi
3232

3333
# Test invalid config (if exists)
3434
if [ -f "invalid-config.yml" ]; then
35+
echo "Testing with invalid-config.yml:"
36+
cat invalid-config.yml | head -3
37+
echo "..."
3538
if /var/tmp/lxc-compose --config invalid-config.yml ps >/dev/null 2>&1; then
3639
echo "⚠️ Invalid configuration accepted (should fail)"
3740
else

integration-test/docker-lxc/test-data/lxc-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
version: "3"
12
services:
23
web:
34
image: "ubuntu:20.04"

pkg/container/manager.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,21 @@ func (m *LXCManager) execLXCCommand(name string, args ...string) error {
9090
}
9191

9292
if err != nil {
93-
logging.Error("Command failed",
94-
"command", name,
95-
"args", args,
96-
"output", string(output),
97-
"error", err,
98-
)
93+
// Use debug level for expected failures like container non-existence
94+
if name == "lxc-info" && strings.Contains(string(output), "doesn't exist") {
95+
logging.Debug("Container does not exist (expected)",
96+
"command", name,
97+
"args", args,
98+
"output", string(output),
99+
)
100+
} else {
101+
logging.Error("Command failed",
102+
"command", name,
103+
"args", args,
104+
"output", string(output),
105+
"error", err,
106+
)
107+
}
99108
return fmt.Errorf("command failed: %w", err)
100109
}
101110
return nil

0 commit comments

Comments
 (0)