Exemple #1
0
func bootstrapLocalConfig(spec ConfigSpec) error {
	task := "Bootstrap local configuration according to the spec"

	// Some handy variables.
	configKey := spec.ConfigKey()
	container := spec.LocalConfig()
	moduleSpec, isModuleSpec := spec.(ModuleConfigSpec)

	// Pre-read local config.
	// It is needed the pre-write hook as well.
	local, err := config.ReadLocalConfig()
	readConfig := func() (configFile, error) {
		return local, err
	}

	// Handle module config specs a bit differently.
	var preWriteHook func() (bool, error)
	if isModuleSpec {
		// Make sure the config container is not nil in case this is a module config.
		// In case the local config container is nil, the pre-write hook is not executed
		// and the active module ID is not set, and that would be a problem.
		// Returning a nil local config container is a valid choice, but we still
		// need the pre-write hook to be executed to set the active module ID.
		if container == nil {
			container = newEmptyModuleConfigContainer(configKey, moduleSpec.ModuleKind())
		}

		// In case this is a module config spec, set the the pre-write hook
		// to modify the local config file to activate the module being configured.
		preWriteHook = func() (bool, error) {
			return SetActiveModule(local, moduleSpec.ModuleKind(), configKey)
		}
	}

	// The post-write hook simply tells the user to commit the local config file.
	postWriteHook := func() error {
		fmt.Println()
		log.Warn("Local configuration file modified, please commit it.")
		return nil
	}

	// Run the common loading function with the right arguments.
	if err := load(&loadArgs{
		configKind:      "local",
		configKey:       configKey,
		configContainer: container,
		readConfig:      readConfig,
		emptyConfig:     emptyLocalConfig,
		preWriteHook:    preWriteHook,
		postWriteHook:   postWriteHook,
	}); err != nil {
		return errs.NewError(task, err)
	}
	return nil
}
Exemple #2
0
func LoadConfig() (*Config, error) {
	local, err := config.ReadLocalConfig()
	if err != nil {
		return nil, err
	}
	config := &Config{}
	if ts := local.EnabledTimestamp; ts != nil {
		config.SalsaFlowEnabledTimestamp = *ts
	} else {
		printSalsaFlowEnabledTimestampWarning()
		return nil, errors.New("SalsaFlow enabled timestamp not set")
	}
	return config, nil
}
Exemple #3
0
// RunModuleBootstrapDialog runs the module bootstrapping dialog for every
// section specification passed into the function.
func RunModuleBootstrapDialog(sections ...*ModuleDialogSection) error {
	// Try to read the local configuration file.
	localConfig, err := config.ReadLocalConfig()
	if err != nil {
		if !os.IsNotExist(errs.RootCause(err)) {
			return err
		}
	}

	for _, section := range sections {
		modules := section.AvailableModules

		// In case there are no modules available, skip the section.
		if len(modules) == 0 {
			continue
		}

		// Find the module for the given module kind.
		// Either use the one that is already configured
		// or prompt the user to select one.
		module := tryToFindActiveModule(localConfig, modules)
		if module == nil {
			var err error
			module, err = promptUserToSelectModule(section.AvailableModules, section.Optional)
			if err != nil {
				return err
			}
		}

		// Run the configuration dialog for the selected module.
		// The module can be unset in case it is optional.
		if module != nil {
			if err := BootstrapConfig(module.ConfigSpec()); err != nil {
				return err
			}
		}
	}

	return nil
}
Exemple #4
0
func loadActiveModule(kind loader.ModuleKind) (loader.Module, error) {
	// Load local configuration.
	localConfig, err := config.ReadLocalConfig()
	if err != nil {
		return nil, err
	}

	// Get the module matching the module kind.
	activeModuleId := loader.ActiveModule(localConfig, kind)
	if activeModuleId == "" {
		task := fmt.Sprintf("Get active module ID for module kind '%v'", kind)
		err := &ErrModuleNotSet{kind}
		hint := "\nMake sure the ID is specified in the local configuration file.\n\n"
		return nil, errs.NewErrorWithHint(task, err, hint)
	}

	// Find the module among the registered modules.
	for _, module := range registeredModules {
		if module.Id() == activeModuleId {
			return module, nil
		}
	}

	task := fmt.Sprintf("Load active module for module kind '%v'", kind)
	err = &ErrModuleNotFound{activeModuleId}
	hint := `
The module for the given module ID was not found.
This can happen for one of the following reasons:

  1. the module ID as stored in the local configuration file is mistyped, or
  2. the module for the given module ID was not linked into your SalsaFlow.

Check the scenarios as mentioned above to fix the issue.

`
	return nil, errs.NewErrorWithHint(task, err, hint)
}
Exemple #5
0
func readLocalConfig() (configFile, error) {
	return config.ReadLocalConfig()
}