Sybren A. Stüvel 7d1ce8131a Manager: simplify value-to-variable replacement
Simplify the code for the two-way variables' value-to-variable replacement.

Instead of using a goroutine and two channels, use a separate struct and
call a function on that directly.

No functional changes.
2023-07-31 13:58:43 +02:00

86 lines
2.1 KiB
Go

package config
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"errors"
"fmt"
"io/fs"
"github.com/rs/zerolog/log"
)
// Service provides access to Flamenco Manager configuration.
type Service struct {
config Conf
forceFirstRun bool
}
func NewService() *Service {
return &Service{
config: DefaultConfig(),
}
}
// IsFirstRun returns true if this is likely to be the first run of Flamenco.
func (s *Service) IsFirstRun() (bool, error) {
if s.forceFirstRun {
return true, nil
}
config, err := getConf()
switch {
case errors.Is(err, fs.ErrNotExist):
// No configuration means first run.
return true, nil
case err != nil:
return false, fmt.Errorf("loading %s: %w", configFilename, err)
}
// No shared storage configured means first run.
return config.SharedStoragePath == "", nil
}
func (s *Service) ForceFirstRun() {
s.forceFirstRun = true
}
func (s *Service) Load() error {
config, err := getConf()
if err != nil {
return err
}
s.config = config
return nil
}
func (s *Service) Get() *Conf {
return &s.config
}
// Save writes the in-memory configuration to the config file.
func (s *Service) Save() error {
err := s.config.Write(configFilename)
if err != nil {
return err
}
// Do the logging here, as our caller doesn't know `configFilename``.
log.Info().Str("filename", configFilename).Msg("configuration file written")
return nil
}
// Expose some functions of Conf here, for easier mocking of functionality via interfaces.
func (s *Service) ExpandVariables(inputChannel <-chan string, outputChannel chan<- string, audience VariableAudience, platform VariablePlatform) {
s.config.ExpandVariables(inputChannel, outputChannel, audience, platform)
}
func (s *Service) NewVariableToValueConverter(audience VariableAudience, platform VariablePlatform) *ValueToVariableReplacer {
return s.config.NewVariableToValueConverter(audience, platform)
}
func (s *Service) ResolveVariables(audience VariableAudience, platform VariablePlatform) map[string]ResolvedVariable {
return s.config.ResolveVariables(audience, platform)
}
func (s *Service) EffectiveStoragePath() string {
return s.config.EffectiveStoragePath()
}