All Projects → PoshCode → Configuration

PoshCode / Configuration

Licence: mit
A module to help other modules have settings

Programming Languages

powershell
5483 projects

Projects that are alternatives of or similar to Configuration

Simple-YAML
A Java API that provides an easy-to-use way to store data using the YAML format.
Stars: ✭ 68 (-49.63%)
Mutual labels:  serialization, configuration
Rails Settings
Manage settings with Ruby on Rails
Stars: ✭ 807 (+497.78%)
Mutual labels:  settings, configuration
rails-settings-ui
User interface for manage settings in rails application (using rails-settings gem) / Интерфейс для управления настройками в Rails приложении
Stars: ✭ 93 (-31.11%)
Mutual labels:  settings, configuration
climatecontrol
Python library for loading settings and config data from files and environment variables
Stars: ✭ 20 (-85.19%)
Mutual labels:  settings, configuration
Ins sandstorm
[INS] Config setting for our sandstorm server
Stars: ✭ 61 (-54.81%)
Mutual labels:  settings, configuration
config
Config component, strictly typed
Stars: ✭ 14 (-89.63%)
Mutual labels:  settings, configuration
Senparc.co2net
支持 .NET Framework & .NET Core 的公共基础扩展库
Stars: ✭ 289 (+114.07%)
Mutual labels:  settings, configuration
Django Dynamic Preferences
Dynamic global and instance settings for your django project
Stars: ✭ 238 (+76.3%)
Mutual labels:  settings, configuration
Laravel Settings
Simple Settings package for a laravel application
Stars: ✭ 45 (-66.67%)
Mutual labels:  settings, configuration
Yamlsettings
Yaml Settings Configuration Module
Stars: ✭ 12 (-91.11%)
Mutual labels:  settings, configuration
JsonSettings
This library simplifies creating configuration for your C# app/service by utilizing the serialization capabilities of Json.NET to serialize nested (custom) objects, dictionaries and lists as simply as by creating a POCO and inheriting JsonSettings class.
Stars: ✭ 59 (-56.3%)
Mutual labels:  settings, configuration
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+1442.22%)
Mutual labels:  settings, configuration
cfg-rs
A Configuration Library for Rust Applications
Stars: ✭ 18 (-86.67%)
Mutual labels:  settings, configuration
gconfigs
gConfigs - Config and Secret parser
Stars: ✭ 42 (-68.89%)
Mutual labels:  settings, configuration
setset
Powerful Incremental Type-driven Settings Engine.
Stars: ✭ 20 (-85.19%)
Mutual labels:  settings, configuration
Dry Configurable
A simple mixin to make Ruby classes configurable
Stars: ✭ 280 (+107.41%)
Mutual labels:  settings, configuration
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (+22.22%)
Mutual labels:  settings, configuration
Strictyaml
Type-safe YAML parser and validator.
Stars: ✭ 836 (+519.26%)
Mutual labels:  serialization, configuration
Rime pure
【rime小狼毫\trime同文】手机/PC一站式配置【简约皮肤\拼音搜狗词库\原创trime同文四叶草九宫格拼音方案\四叶草拼音、小鹤双拼、极品五笔、徐码、郑码】 rime配置
Stars: ✭ 73 (-45.93%)
Mutual labels:  settings, configuration
Settings.net
⚙️ Settings.Net - An easy to use .NET library for accessing and storing settings and configurations.
Stars: ✭ 114 (-15.56%)
Mutual labels:  settings, configuration

Build Status

The Configuration Module

Metadata commands for working with .psd1:

  • Manipulating metadata files
  • Extensible serialization of types
  • Built in support for DateTime, Version, Guid, SecureString, ScriptBlocks and more
  • Lets you store almost anything in readable metadata (.psd1) files
  • Serializing (Export) to metadata (.psd1) files
  • Deserializing (Import) from metadata (.psd1) files

Configuration commands for loading settings:

  • Ship default configuration files with your module
  • Automatically determines where to save your settings
    • Supports Windows roaming profiles
    • Supports XDG settings for Linux (and MacOS)
  • Allow users to configure settings using our commands, or your wrappers
  • Supports layered configurations:
    • Machine-level config
    • User override files
  • Supports automatic configuration of parameters values for a command
    • Reads Noun configuration files in your working directory
    • Filters only values which apply to the current command

It supports WindowsPowerShell, as well as PowerShell Core on Windows, Linux and OS X.

Installation

Install-Module Configuration

Usage

The Configuration module is designed to be used by other modules (or from scripts) to allow the storage of configuration data (generally, hashtables, but any PSObject).

In its simplest form, you add a Configuration.psd1 file to a module you're authoring, and put your default settings in it -- perhaps something as simple as this:

@{
    DriveName = "data"
}

Then, in your module, you import those settings in a function when you need them, or expose them to your users like this:

function Get-FaqConfig {
    Import-Configuration
}

Perhaps, in a simple case like this one, you might write a wrapper function so your users can get and set that one configuration option directly:

function Get-DataDriveName {
    $script:Config = Import-Configuration
    $config.DriveName
}

function Set-DataDriveName {
    param([Parameter(Mandatory)][String]$Name)

    @{ DriveName = $Name} | Export-Config
}

Of course, you could have imported the configuration, edited that one setting, and then exported the whole config, but you can also just export a few settings, because Import-Configuration supports a layered configuration. More on that in a moment, but first, let's talk about how this all works.

Versioning

Versioning your configuration is supported, but is only done explicitly (in Import-Configuration, Export-Configuration, and Get-ConfigurationPath). Whenever you need to change your module's configuration in an incompatible way, you can write a migration function that runs at import-time in your new version, something like this:

# Specify a script-level version number
$ConfigVersion = @{ Version = 1.1 }

function MigrateData {
    # Specify the version you want to migrate
    $OldVersion = @{ <# I didn't specify a version at first #> }

    # If there are no configuration files, migrate them
    if(!(Get-ConfigurationPath @ConfigVersion | Get-ChildItem -File)) {
        # Import the old config
        $oldConfig = Import-Configuration @OldVersion
        # Transform your configuration however you like
        $newConfig = @{ PSDriveName = $existing.DriveName }
        # Export the new config
        $newConfig | Export-Configuration @ConfigVersion
    }
}

# Call your migration function during module import
MigrateData

Then you just need to be sure you specify the @ConfigVersion whenever you call Import-Configuration elsewhere in your module.

Note that configuration files are not currently deleted by Uninstall-Module, so they are never automatically cleaned up.

How it works

The Configuration module works by serializing PowerShell hashtables or custom objects into PowerShell data language in a Configuration.psd1 file!

Configuration path

When you Export-Configuration you can set the -Scope, which determines where the Configuration.psd1 are stored:

  • User exports to $Env:LocalAppData or ~/.config/
  • Enterprise exports to $Env:AppData (the roaming path) or ~/.local/share/
  • Machine exports to $Env:ProgramData or /etc/xdg/

Note that the linux paths are controlled by XDG environment variables, and the default paths can be overriden by manually editing the Configuration module manifest.

Within that folder, the Configuration module root is "PowerShell," followed by either a company or author and the module name -- within which your configuration file(s) are stored.

From a module that uses Configuration, you can call the Get-ConfigurationPath command to get the path to that folder, and since the folder is created for you, you can use it store other files, like cached images, etc.

Layered Configuration

In addition to automatically determining the storage path, the configuration module supports layered configuration, so that you can have defaults you ship with your module, or configure default at the enterprise or machine level, and still allow users to override the settings. When you call Import-Configuration from within a module, it automatically imports all the available files and updates the configuration object which is returned at the end:

  1. First, it imports the default Configuration.psd1 from the module's folder.
  2. Then it imports machine-wide settings (e.g. the ProgramData folder)
  3. Then it imports the users' enterprise roaming settings (e.g. from AppData\Roaming)
  4. Finally it imports the users' local settings (from AppData\Local)

Any missing files are just skipped, and each layer of settings updates the settings from the previous layers, so if you don't set a setting in one layer, the setting from the previous layers persists.

However, it's up to individual users and module authors to take advantage of this..

Serialization

The actual serialization commands (with the Metadata noun) are: ConvertFrom, ConvertTo, Import and Export. By default, the Configuration serializer can handle a variety of custom PSObjects, hashtables, and arrays recursively, and has specific handling for booleans, strings and numbers, as well as Versions, GUIDs, and DateTime, DateTimeOffset, and even ScriptBlocks and PSCredential objects.

Important note: PSCredentials are stored using ConvertTo-SecureString, and currently only work on Windows. They should be stored in the user scope, since they're serialized per-user, per-machine, using the Windows Data Protection API.

In other words, it handles everything you're likely to need in a configuration file. However, it also has support for adding additional type serializers via the Add-MetadataConverter command. If you want to store anything that doesn't work, please raise an issue 😉.

One little catch

The configuration module uses the caller's scope to determine the name of the module (and Company or Author name) that is asking for configuration. For this reason you normally just call Import-Configuration from within a function in your module (to make sure the callstack shows the module scope).

The very important side effect is that you must not change the module name nor the author of your module if you're using this Configuration module, or you will need to manually call Import-Configuration with the old information and then Export those settings to the new location (see the )

Using the cmdlets from outside a module

It is possible to use the commands to Import and Export the configuration for a module from outside the module (or from the main module body, instead of a function), simply pipe the ModuleInfo to Import-Configuration. To continue our example from earlier:

$Config = Get-Module DataModule | Import-Configuration
$Config.DriveName = "DataDrive"
Get-Module DataModule | Export-Configuration $Config

Note that if you look at the parameter sets for Import-Configuration you will find that you can also just pass the the -Author (or -CompanyName) and module -Name by hand, but you must be sure to get them exactly right, or you'll import nothing...

$Config = Import-Configuration -Name DataModule -Author HuddledMasses.org

Because of how easily this can go wrong, I strongly recommend you don't use this syntax -- but if you do, be aware that you must also specify the -DefaultPath if you want to load the default configuration file from the module folder.

A little history:

The Configuration module is something I first wrote as part of the PoshCode packaging module and have been meaning to pull out for awhile.

I finally started working on this while I work on writing the Gherkin support for Pester. That support was merged into Pester with the 4.0 release, and I'm using it for the tests in this module.

In any case, this module is mostly code ported from my PoshCode module as I develop the specs (the .feature files) and the Gherkin support to run them! Anything you see here has better than 95% code coverage in the feature and step files, which are executable via Invoke-Gherkin.

For the tests to work, you need to make sure that the module isn't already loaded, because the tests import it with the file paths mocked for testing:

Remove-Module Configuration -ErrorAction SilentlyContinue
Invoke-Gherkin -CodeCoverage *.psm1
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].