Adds ModuleBase Post (#15)

This commit is contained in:
OCram85 2022-03-22 08:34:32 +01:00 committed by GitHub
parent 725fc24cc6
commit 7d6412688f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 181 additions and 4 deletions

View File

@ -76,7 +76,7 @@ steps:
- name: "Trigger Service Update"
image: ocram85/portainer-serviceupdate
settings:
VERBOSE: true
#VERBOSE: true
URI: "https://portainer.ocram85.com"
TOKEN:
from_secret: NEXT_TOKEN
@ -137,7 +137,7 @@ steps:
- name: "Trigger Service Update"
image: ocram85/portainer-serviceupdate
settings:
VERBOSE: true
#VERBOSE: true
URI: "https://portainer.ocram85.com"
TOKEN:
from_secret: TOKEN

View File

@ -35,7 +35,7 @@ showScrollToTop = true
invertPagination = false
showReadingTime = true
showTableOfContents = true
showTaxonomies = false
showTaxonomies = true
showWordCount = true
sharingLinks = ["facebook", "twitter", "pinterest", "reddit", "linkedin", "email"]

View File

@ -0,0 +1,119 @@
---
title: 'PowerShell Module Base for Config Files'
date: 2022-03-21T09:14:41+01:00
draft: true
categories: ['PowerShell']
tags: ['ModuleBase', 'config']
# lastmod: 2022-03-21T09:14:41+01:00
# showDateUpdated: true
# custom overrides for pages
# showDate: false
# showAuthor: false
# showWordCount: false
# showReadingTime: false
# showTableOfContents: false
# showTaxonomies: false
# showEdit: false
# sharingLinks: [null]
---
![note](note.jpg 'Photo by [Sigmund](https://unsplash.com/@sigmund) on [Unsplash](https://unsplash.com)')
## 🖼️ Intro
Sometimes you want to use a basic config file for your module. This config file should be used to define basic
settings without any user specific content. This file could be placed into your PowerShell Module folder.
Therefore you can use the automatic variable `$MyInvocation`, especially with its properties
`$MyInvocation.MyCommand.Module.ModuleBase`. This returns the full path to your current module base folder, which
can be used to join a path for your config file.
## 📑 `.\config.psd1` config file
Let's assume you start a new module and you need multiple config keys to work with. So you usually create a
json or powershell data based config file:
```powershell
{
# Logging
OutputLevel = 'Detailed'
DefaultTarget = 'Console'
LogRetentionInWeeks = 4
# DataSource
CouchDBURI = 'http://localhost'
MongoDBURI = 'http://mongodb'
# ...
# ...
}
```
## 🔎 `Get-ConfigValue` helper function
Now you can use `$MyInvocation.MyCommand.Module.ModuleBase` with a helper function to parse the path to your config
file and return your needed values:
```powershell
function Get-ConfigValue {
<#
.SYNOPSIS
Returns the value of a given config file key.
.PARAMETER ByKey
Config file key.
.OUTPUTS
[string]
.EXAMPLE
Get-ConfigValue -ByKey 'OutputLevel'
.NOTES
Private module helper function. Used by other function within your module.
#>
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory = $true, HelpMessage = 'Existing key from config file.')]
[string]$ByKey
)
begin { }
process {
$ModuleBase = $MyInvocation.MyCommand.Module.ModuleBase
$ConfigFile = Join-Path -Path $ModuleBase -ChildPath 'config.psd1'
if (Test-Path -Path $ConfigFile) {
try {
$Config = Import-PowerShellDataFile -Path $ConfigFile
Write-Output $Config.$ByKey
}
catch {
Write-Error -Message $_.Exception.Message -ErrorAction Stop
}
}
else {
Write-Error -Message 'Config file not found!' -ErrorAction 'Stop'
}
}
end { }
}
```
## 💭 Final Thoughts
All you need to to is using the `Get-ConfigValue -ByKey '<example key>'` in your functions to get the any value
defined in you config file.
As far as I know, that's the simplest way to get your module root and using it with a config file.
{{< alert >}}
Keep in mind not to store any sensitive data in you config file. User specific data should also be stored in a user
context and not in a global module wide config.
{{< /alert >}}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

57
contributing.md Normal file
View File

@ -0,0 +1,57 @@
# Contributing to [OCram85.com](https://ocram85.com) blog
## Usage
To use the sources for local development you need to install the dependencies first:
```bash
npm install
```
### How to add new posts
To simplify the workflow for adding new posts you can run the npm script `new:post` with a valid path:
```bash
npm run new:post -- posts/my-new-post/index.md
```
This creates a new post file an populates the values from default [template file](./archetypes/default.md)
## npm scripts
### `build`
Use `npm run build` to build the html files for production.
### `check`
Use `npm run check` to get the current version for the embedded hugo version.
### `clean`
Use `npm run clean` to clean the _public_ content.
### `lint:markdown`
Use `npm run lint:markdown` to run the markdown linter for identifying markdown issues.
### `start`
Use `npm run start` as alias for `npm run server`.
### `server`
Use `npm run server` to start the development server which renders also draft posts. You can access the development server via `http://localhost:1313`
### `test`
Use `npm run test` all nested test tasks. For now this points to _lint:markdown_.
### `postinstall`
`npm run postinstall` is used by `npm install` to get the latest embedded hugo binary version.
### `new:post`
Use `npm run new:post` to create a new blog post based on the archetype template.

View File

@ -11,7 +11,8 @@
"start": "npm run server",
"server": "exec-bin node_modules/.bin/hugo/hugo server --bind=0.0.0.0 -D",
"test": "npm run lint:markdown",
"postinstall": "hugo-installer --version otherDependencies.hugo --extended --destination node_modules/.bin/hugo"
"postinstall": "hugo-installer --version otherDependencies.hugo --extended --destination node_modules/.bin/hugo",
"new:post": "exec-bin node_modules/.bin/hugo/hugo new"
},
"repository": {
"type": "git",