Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions cmd/proxsave/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/tis24dev/proxsave/pkg/utils"
)

type configStatusLogger interface {
Expand Down Expand Up @@ -47,27 +49,44 @@ func ensureConfigExists(path string, logger configStatusLogger) error {
}

func setEnvValue(template, key, value string) string {
target := key + "="
lines := strings.Split(template, "\n")
replaced := false
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, target) {
if utils.IsComment(trimmed) {
continue
}

parts := strings.SplitN(trimmed, "=", 2)
if len(parts) >= 1 && strings.TrimSpace(parts[0]) == key {
// Found match!
// We try to preserve the indentation and comments from the original line.
leadingLen := len(line) - len(strings.TrimLeft(line, " \t"))
leading := ""
if leadingLen > 0 {
leading = line[:leadingLen]
}
rest := line[leadingLen:]
commentSpacing := ""

// Extract comment if present in the original line logic
// The original logic extracted comment from 'rest' after target match.
// Here we can re-parse the line specifically for comment.
comment := ""
if idx := strings.Index(rest, "#"); idx >= 0 {
before := rest[:idx]
comment = rest[idx:]
trimmedBefore := strings.TrimRight(before, " \t")
commentSpacing = before[len(trimmedBefore):]
rest = trimmedBefore
commentSpacing := ""

if idx := strings.Index(line, "#"); idx >= 0 {
// Verify # is not part of the key or value?
// Assuming standard comment
commentPart := line[idx:]
// Ensure it's not inside quotes? The original logic didn't check quotes carefully but let's be safe(r).
// For setEnvValue we are replacing the value, so we just want to keep the comment at the end.
comment = commentPart

// Find spacing before comment
beforeComment := line[:idx]
trimmedBefore := strings.TrimRight(beforeComment, " \t")
commentSpacing = beforeComment[len(trimmedBefore):]
}
Comment on lines +76 to 88
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment extraction logic doesn't properly handle quoted values containing "#". For example, if a line is KEY="value#with#hash", the code will incorrectly treat #with#hash" as a comment. The logic should parse the value part first (respecting quotes) before looking for comments, similar to how splitKeyValueRaw handles it in upgrade.go.

Copilot uses AI. Check for mistakes.

newLine := leading + key + "=" + value
if comment != "" {
spacing := commentSpacing
Expand All @@ -78,6 +97,9 @@ func setEnvValue(template, key, value string) string {
}
lines[i] = newLine
replaced = true
// We stop after first match? Original code didn't break, but typically keys are unique.
// Let's break to avoid multiple replacements if file is messy (or continue to fix all?)
// Original didn't break. Let's not break to match behavior.
}
}
if !replaced {
Expand Down
12 changes: 12 additions & 0 deletions cmd/proxsave/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@
bootstrap.Error("ERROR: Failed to plan configuration upgrade: %v", err)
return types.ExitConfigError.Int()
}
if len(result.Warnings) > 0 {
bootstrap.Warning("Config upgrade warnings (%d):", len(result.Warnings))
for _, warning := range result.Warnings {
bootstrap.Warning(" - %s", warning)
}
}
if !result.Changed {
bootstrap.Println("Configuration is already up to date with the embedded template; no changes are required.")
return types.ExitSuccess.Int()
Expand Down Expand Up @@ -437,6 +443,12 @@
bootstrap.Error("ERROR: Failed to upgrade configuration: %v", err)
return types.ExitConfigError.Int()
}
if len(result.Warnings) > 0 {
bootstrap.Warning("Config upgrade warnings (%d):", len(result.Warnings))
for _, warning := range result.Warnings {
bootstrap.Warning(" - %s", warning)
}
}
if !result.Changed {
bootstrap.Println("Configuration is already up to date with the embedded template; no changes were made.")
return types.ExitSuccess.Int()
Expand Down Expand Up @@ -1549,7 +1561,7 @@
}
fmt.Printf("\r Remaining: %ds ", int(remaining.Seconds()))

select {

Check failure on line 1564 in cmd/proxsave/main.go

View workflow job for this annotation

GitHub Actions / security

should use a simple channel send/receive instead of select with a single case (S1000)

Check failure on line 1564 in cmd/proxsave/main.go

View workflow job for this annotation

GitHub Actions / security

should use a simple channel send/receive instead of select with a single case (S1000)

Check failure on line 1564 in cmd/proxsave/main.go

View workflow job for this annotation

GitHub Actions / security

should use a simple channel send/receive instead of select with a single case (S1000)
case <-ticker.C:
continue
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/proxsave/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,14 @@ func printUpgradeFooter(upgradeErr error, version, configPath, baseDir, telegram
}
}

if cfgUpgradeResult != nil && len(cfgUpgradeResult.Warnings) > 0 {
fmt.Printf("Configuration warnings (%d):\n", len(cfgUpgradeResult.Warnings))
for _, warning := range cfgUpgradeResult.Warnings {
fmt.Printf(" - %s\n", warning)
}
fmt.Println()
}

fmt.Println("Next steps:")
if strings.TrimSpace(configPath) != "" {
fmt.Printf("1. Verify configuration: %s\n", configPath)
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,9 @@ func parseEnvFile(path string) (map[string]string, error) {
terminated = true
break
}
blockLines = append(blockLines, next)
if !utils.IsComment(strings.TrimSpace(next)) {
blockLines = append(blockLines, next)
}
}
if !terminated {
return nil, fmt.Errorf("unterminated multi-line value for %s", key)
Expand Down
Loading
Loading