Skip to content

Commit

Permalink
fix: also change comment
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Möller <jmoller@redhat.com>
  • Loading branch information
jakobmoellerdev committed Aug 5, 2024
1 parent 397f91c commit 8d47b6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,15 @@ func updateConfig(ctx context.Context, v any, rw io.ReadWriteSeeker) error {
fieldRegexes := make([]fieldRegex, 0, len(fieldsForConfigQuery))
for _, field := range fieldsForConfigQuery {
// The pattern is a regex that matches the field name and its value if uncommented
// It also matches the comment that indicates the field was edited by the client.
// Valid:
// field = value
// field = "value"
// Invalid:
// # field = value
// # field = "value"
// It incorporates various tabbing and spacing configurations as well
pattern := fmt.Sprintf(`(?m)^\s*%s\s*=\s*(\".*?\"|\d+)?$`, field.name)
pattern := fmt.Sprintf(`(?m)((\t# .*?\n)*|)^\s%s\s*=\s*(\".*?\"|\d+)?$`, field.name)
re, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("failed to compile regexp: %v", err)
Expand Down Expand Up @@ -354,7 +355,7 @@ func updateConfig(ctx context.Context, v any, rw io.ReadWriteSeeker) error {
if diff := len(raw) - offset; diff < 0 {
// If the old configuration is smaller than the new configuration, we need to append the difference
// with empty bytes to ensure we do not have leftover data from the old configuration
raw = append(raw, make([]byte, diff)...)
raw = append(raw, make([]byte, -diff)...)
}

// We want to write from the start, so seek back to the start of the configuration
Expand All @@ -369,8 +370,7 @@ func updateConfig(ctx context.Context, v any, rw io.ReadWriteSeeker) error {
// generateLVMConfigEditComment generates a comment to be added to the configuration file
// This comment is used to indicate that the field was edited by the client.
func generateLVMConfigEditComment() string {
return fmt.Sprintf(`
# This field was edited by %s at %s
return fmt.Sprintf(` # This field was edited by %s at %s
# Proceed carefully when editing as it can have unintended consequences with code relying on this field.
`, ModuleID(), time.Now().Format(time.RFC3339))
}
Expand Down
31 changes: 29 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestUpdateGlobalConfig(t *testing.T) {
} `lvm:"config"`
}{}

expectedPreamble := "\n\t# Proceed carefully when editing as it can have unintended consequences with code relying on this field.\n\t"
expectedPreamble := "# Proceed carefully when editing as it can have unintended consequences with code relying on this field.\n\t"

cfg.Config.ProfileDir = "mynewprofiledir"

Expand All @@ -340,6 +340,33 @@ func TestUpdateGlobalConfig(t *testing.T) {
t.Fatalf("expected field to be modified, but it was not")
}

t.Log(LVMGlobalConfiguration)
cfg.Config.ProfileDir = "mynewprofiledir2"

if err := clnt.UpdateGlobalConfig(context.Background(), &cfg); err != nil {
t.Fatalf("failed to update global config: %v", err)
}

println(control().String())

if containsModifiedField = bytes.Contains(control().Bytes(), []byte(fmt.Sprintf(
"%sprofile_dir = %q\n",
expectedPreamble,
"mynewprofiledir2",
))); !containsModifiedField {
t.Fatalf("expected field to be modified, but it was not")
}

cfg.Config.ProfileDir = "mynewprofiledir3"

if err := clnt.UpdateGlobalConfig(context.Background(), &cfg); err != nil {
t.Fatalf("failed to update global config: %v", err)
}

if containsModifiedField = bytes.Contains(control().Bytes(), []byte(fmt.Sprintf(
"%sprofile_dir = %q\n",
expectedPreamble,
"mynewprofiledir3",
))); !containsModifiedField {
t.Fatalf("expected field to be modified, but it was not")
}
}

0 comments on commit 8d47b6c

Please sign in to comment.