Skip to content

Instantly share code, notes, and snippets.

💭
code like hell

Giacomo Stelluti Scala gsscoder

💭
code like hell
View GitHub Profile
@gsscoder
gsscoder / Install.ps1
Last active May 11, 2020
PowerShell Install helper function (DevOps friendly)
View Install.ps1
function Out-Error([Parameter(ValueFromPipeline)] [string] $message, [switch] $stdOut) {
if ($DevOps) { "##[error]$message" | Write-Host } else {
if ($stdOut) { $message | Write-Host -ForegroundColor Red } else { $message | Write-Error } }
function Install([Parameter(ValueFromPipeline)] [string] $module) {
"Testing module '$module' presence." | Write-Verbose
$moduleInfo = Get-InstalledModule $module -ErrorAction SilentlyContinue -Verbose:$false
if (-not $moduleInfo) {
"Module '$module' is not present. Attempting installation." | Write-Verbose
try {
@gsscoder
gsscoder / Test-Property.ps1
Last active Apr 6, 2020
Properly way to test object properties existence in PowerShell with strict mode activated
View Test-Property.ps1
Set-StrictMode -Version Latest
function Test-Property([Parameter(ValueFromPipeline)] $object, [string] $name) {
try { $name -cin $object.PSObject.Properties.Name }
catch { $false }
}
$path = '/Users/someone/temp/file.json'
$json = $path | Get-Content -Raw | ConvertFrom-Json
[bool] $exists = $json.someKey | Test-Property -name 'someOtherKey'
@gsscoder
gsscoder / New-AzResource_APIM.ps1
Created Apr 3, 2020
Attempt to create APIM with New-AzResource
View New-AzResource_APIM.ps1
# This code actually doesn't work.
$secret = 'B=EBh3glTf523Xj@uLGO]o@kSteLoC@O'
$group = 'INTRANETAI-Europe-DEV'
$service = 'intranetai-europe-test-dev'
$context = New-AzApiManagementContext -ResourceGroupName $group -ServiceName $service
$versionSet = New-AzApiManagementApiVersionSet -Context $context -Name 'news' `
-Scheme Header -HeaderName 'x-api-version' `
@gsscoder
gsscoder / Get-InstalledModule_out.txt
Created Apr 1, 2020
Get-InstalledModule output on my organization development machine
View Get-InstalledModule_out.txt
Version Name Repository Description
------- ---- ---------- -----------
3.6.1 Az PSGallery Microsoft Azure PowerShell - Cmdlets ...
1.7.3 Az.Accounts PSGallery Microsoft Azure PowerShell - Accounts...
1.1.1 Az.Advisor PSGallery Microsoft Azure PowerShell - Azure Ad...
1.0.3 Az.Aks PSGallery Microsoft Azure PowerShell - Azure ma...
1.1.2 Az.AnalysisServices PSGallery Microsoft Azure PowerShell - Analysis...
1.4.0 Az.ApiManagement PSGallery Microsoft Azure PowerShell - Api Mana...
1.0.3 Az.ApplicationInsights PSGallery
@gsscoder
gsscoder / ConvertTo-PascalCase.ps1
Last active May 26, 2020
PowerShell function to convert kebab-case to PascalCase
View ConvertTo-PascalCase.ps1
# 'hello-wonderful-world' | ConvertTo-PascalCase
# outcome: HelloWonderfulWorld
function ConvertTo-PascalCase([Parameter(ValueFromPipeline)] [string] $text) {
($text -split '-' | ForEach-Object {
"$($_.ToCharArray()[0].ToString().ToUpper())$($_.Substring(1))" }) -join ''
}
@gsscoder
gsscoder / which.fsx
Created Mar 14, 2020
F# script that finds a command path like Bash 'which' or PowerShell 'Get-Command'
View which.fsx
open System
open System.IO
let envPaths = Environment.GetEnvironmentVariable("PATH").Split(':')
let rec find (paths : seq<string>) command =
let result = paths |> Seq.tryHead |> Option.bind (fun path -> Some (Path.Combine(path, command)))
match result with
| Some _ -> result
| _ -> find (paths |> Seq.skip 1) command
@gsscoder
gsscoder / self-parse_pipeline.yml
Last active Mar 5, 2020
Azure Pipeline step to check it's pool or fail
View self-parse_pipeline.yml
trigger:
- none
pool:
vmImage: 'windows-latest'
steps:
- powershell: |
$value = Select-String -Path 'azure-pipelines.yml' -Pattern "(?<=vmImage:\s').*(?='$)"
if ($value -like '*windows*') {
@gsscoder
gsscoder / AzurePipelines_Conditions.cs
Last active Feb 22, 2020
C# draft of Azure Pipelines conditions with ExpressionEngine
View AzurePipelines_Conditions.cs
using System;
using System.Collections.Immutable;
using System.Linq;
using ExpressionEngine;
class Program
{
static void Main(string[] args)
{
var context = new Context();
@gsscoder
gsscoder / iterators.ts
Created Feb 13, 2020
Simple TypeScript iterator example
View iterators.ts
function yieldNumbers() : IterableIterator<number> {
return generator()
function *generator(): IterableIterator<number> {
yield 0
yield 1
yield 2
yield 3
}
}
@gsscoder
gsscoder / factorial.ts
Created Feb 8, 2020
Typescript script to calculate factorial of a number
View factorial.ts
// $ tsc factorial.ts && node factorial.js
// 3628800
function factorial(num: number) : number {
if (num == 1) return 1
else return num * factorial(num - 1)
}
console.log(factorial(10))
You can’t perform that action at this time.