-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
124 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ import ( | |
) | ||
|
||
const ( | ||
version = "v0.0.7" | ||
version = "v0.0.8" | ||
) | ||
|
||
var cfgFile string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package config | ||
|
||
import "certalert/internal/certificates" | ||
|
||
// DeepCopy returns a deep copy of the config | ||
func (c Config) DeepCopy() Config { | ||
// Copying basic types (like string, int, bool) | ||
// is straightforward since they don't contain internal references. | ||
var newConfig Config | ||
newConfig.Server = Server{ | ||
Hostname: c.Server.Hostname, | ||
Port: c.Server.Port, | ||
} | ||
newConfig.Pushgateway = Pushgateway{ | ||
Address: c.Pushgateway.Address, | ||
InsecureSkipVerify: c.Pushgateway.InsecureSkipVerify, | ||
Job: c.Pushgateway.Job, | ||
Auth: Auth{ | ||
Basic: Basic{ | ||
Username: c.Pushgateway.Auth.Basic.Username, | ||
Password: c.Pushgateway.Auth.Basic.Password, | ||
}, | ||
Bearer: Bearer{ | ||
Token: c.Pushgateway.Auth.Bearer.Token, | ||
}, | ||
}, | ||
} | ||
|
||
// For slices, you'll want to ensure you're creating a new slice | ||
// and copying each element (especially if they are structs). | ||
newCerts := make([]certificates.Certificate, len(c.Certs)) | ||
for i, cert := range c.Certs { | ||
newCerts[i] = certificates.Certificate{ | ||
Name: cert.Name, | ||
Enabled: cert.Enabled, // Assuming bool pointers are okay to copy directly | ||
Path: cert.Path, | ||
Password: cert.Password, | ||
Type: cert.Type, | ||
} | ||
} | ||
newConfig.Certs = newCerts | ||
|
||
return newConfig | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"certalert/internal/certificates" | ||
"testing" | ||
) | ||
|
||
func TestDeepCopy(t *testing.T) { | ||
// Sample configuration for testing. | ||
original := Config{ | ||
Server: Server{ | ||
Hostname: "localhost", | ||
Port: 8080, | ||
}, | ||
Pushgateway: Pushgateway{ | ||
Address: "http://pushgateway.address", | ||
Job: "testJob", | ||
Auth: Auth{ | ||
Basic: Basic{ | ||
Username: "user", | ||
Password: "pass", | ||
}, | ||
Bearer: Bearer{ | ||
Token: "token", | ||
}, | ||
}, | ||
}, | ||
Certs: []certificates.Certificate{ | ||
{ | ||
Name: "cert1", | ||
Path: "/path/to/cert1", | ||
Password: "cert1password", | ||
Type: "jks", | ||
}, | ||
{ | ||
Name: "cert2", | ||
Path: "/path/to/cert2", | ||
Password: "cert2password", | ||
Type: "pem", | ||
}, | ||
}, | ||
} | ||
|
||
copy := original.DeepCopy() | ||
|
||
// Check that the copy is not the same as the original. | ||
if © == &original { | ||
t.Errorf("Copy is the same as the original") | ||
} | ||
|
||
// modify the copy and check that the original is not modified | ||
copy.Server.Hostname = "modified" | ||
if original.Server.Hostname == copy.Server.Hostname { | ||
t.Errorf("Original is modified") | ||
} | ||
copy.Certs[0].Name = "modified" | ||
if original.Certs[0].Name == copy.Certs[0].Name { | ||
t.Errorf("Original is modified") | ||
} | ||
|
||
//modify the original and check that the copy is not modified | ||
original.Server.Hostname = "original" | ||
if original.Server.Hostname == copy.Server.Hostname { | ||
t.Errorf("Copy is modified") | ||
} | ||
original.Certs[0].Name = "original" | ||
if original.Certs[0].Name == copy.Certs[0].Name { | ||
t.Errorf("Copy is modified") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters