From 8739323f7277df4bcd197a6a4ffe9ad68eae3511 Mon Sep 17 00:00:00 2001 From: Atiq Rahman Date: Tue, 2 Nov 2021 12:39:31 -0700 Subject: [PATCH] move pwsh and win script related posts to powershell dir --- ...t-config-switch-using-powershell-script.md | 130 ++++++++++++++++++ .../{ => pwsh}/powershell-core-cmdlets.md | 0 input/posts/{ => pwsh}/powershell-core.md | 0 input/posts/{ => pwsh}/powershell-legacy.md | 0 input/posts/{ => pwsh}/windows-dos.md | 0 input/posts/{ => pwsh}/windows-tools-cmd.md | 0 6 files changed, 130 insertions(+) create mode 100644 input/posts/pwsh/git-config-switch-using-powershell-script.md rename input/posts/{ => pwsh}/powershell-core-cmdlets.md (100%) rename input/posts/{ => pwsh}/powershell-core.md (100%) rename input/posts/{ => pwsh}/powershell-legacy.md (100%) rename input/posts/{ => pwsh}/windows-dos.md (100%) rename input/posts/{ => pwsh}/windows-tools-cmd.md (100%) diff --git a/input/posts/pwsh/git-config-switch-using-powershell-script.md b/input/posts/pwsh/git-config-switch-using-powershell-script.md new file mode 100644 index 0000000..7a177bb --- /dev/null +++ b/input/posts/pwsh/git-config-switch-using-powershell-script.md @@ -0,0 +1,130 @@ +Title: Switch among multiple Git Config/Profiles using Powershell +Lead: use git cli to switch among multiple Git Config/Profiles +Published: 11/02/2020 +Tags: + - powershell core + - pwsh + - version control + - git +--- + +## Overview +Overview of the script, + +- verify user name stuff, arguments +- switch profile based on first command line argument + +## final script + +```powershell +# Copyright (c) iQubit Inc. +<# +.Synopsis + Switch git config +.DESCRIPTION + Switch to provided git config profile. Runs series of git config commands. + Suggests updating GH Token + ToDo: + Fow now, go with this.. + provide Force flag + probably read from a local json config file + too many params to pass otherwise.. + + facilitate proper checkout with GitUtil + + Demonstrates, + - switch syntax + - case insensitive string comparison: [System.StringComparison] + +.Parameter ProfileName + Select among available profiles +.Parameter CredUserNames + Available git user names +.EXAMPLE + Switch-GitConfig.ps1 USER_NAME_1 @('USER_NAME_1', 'USER_NAME_2') +#> + +[CmdletBinding()] +param( + [ValidateSet('USER_NAME_1', 'USER_NAME_2')] + [Parameter(Mandatory=$true)] [string] $ProfileName, + [Parameter(Mandatory=$true)] [string[]] $CredUserNames +) + + +<# +.SYNOPSIS + Switch to given git config profile +.DESCRIPTION + Sets for both git repo local and global, + - user name + - user email + - credential user name +#> +function Main() { + # Expecting switch between 2 users + if ($CredUserNames.Length -lt 2) { + throw [ArgumentException] ('Unexpected number of gUser Names!') + } + + switch ($ProfileName) { + "USER_NAME_1" { + $gitUserName = git config --get user.name + $shellUserName = $($Home.SubString($Home.LastIndexOf('\')+1)) + + # Expecting git user name to start with Windows Login UserName + # For example, + # windows username Mak will match with + # git user full name 'Atiq Rahman' + if (-not ($gitUserName).StartsWith($shellUserName, [System.StringComparison]::` + InvariantCultureIgnoreCase)) { + throw [ArgumentException] ('Unexpected user name ' + $gitUserName + '!') + } + + $gitUserEmail = git config --get user.email + # global + git config --global user.name "$gitUserName" + git config --global user.email $gitUserEmail + + git config credential.username $CredUserNames[0] + $Env:GITHUB_TOKEN = 'gxp_clylM69Sjj0OZz5JOKX98TzcIE2xBo1MjNTO' + Break + } + "USER_NAME_2" { + $gitUserName = git config --get user.name + # last name of user 2, just for validation + $userNameSuffix = 'USER2_SUFFIX' + + if (-not ($gitUserName).EndsWith($userNameSuffix)) { + throw [ArgumentException] ('Unexpected user name ' + $gitUserName + '!') + } + + $gitUserEmail = git config --get user.email + # global + git config --global user.name "$gitUserName" + git config --global user.email $gitUserEmail + + git config credential.username $CredUserNames[1] + $Env:GITHUB_TOKEN = 'gxp_84770a857cb1482de0e8af7c39a06de4ccf001eeb' + Break + } + Default { "Unexpected $ProfileName!" } + } + + VerifyGitProfile + + 'Remember to set your GITHUB_TOKEN before invoking ''deploy''' +} + +function VerifyGitProfile() { + 'Final Git Configuration:' + git config --global --get user.name + git config --global --get user.email + git config --get user.name + git config --get user.email + git config --get credential.username +} + + +Main +``` \ No newline at end of file diff --git a/input/posts/powershell-core-cmdlets.md b/input/posts/pwsh/powershell-core-cmdlets.md similarity index 100% rename from input/posts/powershell-core-cmdlets.md rename to input/posts/pwsh/powershell-core-cmdlets.md diff --git a/input/posts/powershell-core.md b/input/posts/pwsh/powershell-core.md similarity index 100% rename from input/posts/powershell-core.md rename to input/posts/pwsh/powershell-core.md diff --git a/input/posts/powershell-legacy.md b/input/posts/pwsh/powershell-legacy.md similarity index 100% rename from input/posts/powershell-legacy.md rename to input/posts/pwsh/powershell-legacy.md diff --git a/input/posts/windows-dos.md b/input/posts/pwsh/windows-dos.md similarity index 100% rename from input/posts/windows-dos.md rename to input/posts/pwsh/windows-dos.md diff --git a/input/posts/windows-tools-cmd.md b/input/posts/pwsh/windows-tools-cmd.md similarity index 100% rename from input/posts/windows-tools-cmd.md rename to input/posts/pwsh/windows-tools-cmd.md