Skip to content

Commit

Permalink
refactor: Remove duplicate Save method and unused GetConfigDir
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 19, 2024
1 parent 47d0f4c commit c318bd1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 48 deletions.
56 changes: 24 additions & 32 deletions internal/sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ import (
// - Preserving user customizations during updates.
// - Ensuring safe atomic writes of config files.
type ConfigManager interface {
// Save persists the current configuration to disk.
Save() error

// Update modifies the configuration using the provided update function.
Update(updates func(*Config)) error

// Get returns the current configuration.
Get() *Config

// GetConfigDir returns the directory containing the config file.
GetConfigDir() string

// GetConfigPath returns the path of the file config.
GetConfigPath() string

// Save persists the current configuration to disk.
Save() error
}

// Config is the configuration for the contributoor sidecar.
Expand All @@ -59,7 +56,6 @@ type OutputServerConfig struct {
type configService struct {
logger *logrus.Logger
configPath string
configDir string
config *Config
}

Expand Down Expand Up @@ -116,19 +112,28 @@ func NewConfigService(logger *logrus.Logger, configPath string) (ConfigManager,
}

// Save migrated config
if err := WriteConfig(fullConfigPath, newConfig); err != nil {
if err := writeConfig(fullConfigPath, newConfig); err != nil {
return nil, fmt.Errorf("failed to save migrated config: %w", err)
}
}

return &configService{
logger: logger,
configPath: fullConfigPath,
configDir: filepath.Dir(fullConfigPath),
config: newConfig,
}, nil
}

func newDefaultConfig() *Config {
return &Config{
Logging: logrus.InfoLevel.String(),
Version: "latest",
RunMethod: RunMethodDocker,
NetworkName: "mainnet",
BeaconNodeAddress: "http://localhost:5052",
}
}

// Update updates the file config with the given updates.
func (s *configService) Update(updates func(*Config)) error {
// Apply updates to a copy
Expand All @@ -141,8 +146,8 @@ func (s *configService) Update(updates func(*Config)) error {
}

// Write to temporary file first
tmpPath := s.configPath + ".tmp"
if err := WriteConfig(tmpPath, &updatedConfig); err != nil {
tmpPath := fmt.Sprintf("%s.tmp", s.configPath)
if err := writeConfig(tmpPath, &updatedConfig); err != nil {
os.Remove(tmpPath)

return err
Expand All @@ -166,18 +171,18 @@ func (s *configService) Get() *Config {
return s.config
}

// GetConfigDir returns the directory of the file config.
func (s *configService) GetConfigDir() string {
return s.configDir
}

// GetConfigPath returns the path of the file config.
func (s *configService) GetConfigPath() string {
return s.configPath
}

// WriteConfig writes the file config to the given path.
func WriteConfig(path string, cfg *Config) error {
// Save persists the current configuration to disk.
func (s *configService) Save() error {
return writeConfig(s.configPath, s.config)
}

// writeConfig writes the file config to the given path.
func writeConfig(path string, cfg *Config) error {
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
Expand All @@ -194,16 +199,7 @@ func WriteConfig(path string, cfg *Config) error {
return nil
}

func newDefaultConfig() *Config {
return &Config{
Logging: logrus.InfoLevel.String(),
Version: "latest",
RunMethod: RunMethodDocker,
NetworkName: "mainnet",
BeaconNodeAddress: "http://localhost:5052",
}
}

// validate validates the config.
func (s *configService) validate(cfg *Config) error {
if cfg.Version == "" {
return fmt.Errorf("version is required")
Expand Down Expand Up @@ -261,7 +257,3 @@ func migrateConfig(target, source *Config) error {
*/
return nil
}

func (s *configService) Save() error {
return WriteConfig(s.configPath, s.config)
}
2 changes: 1 addition & 1 deletion internal/sidecar/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (s *dockerSidecar) getComposeEnv() []string {
cfg := s.configService.Get()

return append(os.Environ(),
fmt.Sprintf("CONTRIBUTOOR_CONFIG_PATH=%s", s.configService.GetConfigDir()),
fmt.Sprintf("CONTRIBUTOOR_CONFIG_PATH=%s", filepath.Dir(s.configPath)),
fmt.Sprintf("CONTRIBUTOOR_VERSION=%s", cfg.Version),
)
}
Expand Down
1 change: 0 additions & 1 deletion internal/sidecar/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func TestDockerService_Integration(t *testing.T) {
mockInstallerConfig := installer.NewConfig()
mockSidecarConfig := mock.NewMockConfigManager(ctrl)
mockSidecarConfig.EXPECT().Get().Return(cfg).AnyTimes()
mockSidecarConfig.EXPECT().GetConfigDir().Return(tmpDir).AnyTimes()
mockSidecarConfig.EXPECT().GetConfigPath().Return(filepath.Join(tmpDir, "config.yaml")).AnyTimes()

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
Expand Down
14 changes: 0 additions & 14 deletions internal/sidecar/mock/config.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c318bd1

Please sign in to comment.