Public/Set-ATConfigurationItem.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function Set-ATConfigurationItem
{
<#
    .SYNOPSIS
        Set a module configuration item

    .DESCRIPTION
        Sets a user-configurable configuration item
        Currently you can only configure which application to use for file diffs.

        DYNAMIC PARAMETERS

        The cmdlet provides additional parameters in the context of the item being configured

    .PARAMETER ConfigurationItem
        The item to configure

        DYNAMIC PARAMETERS

        With -ConfigurationItem DiffTool, the following parameters becode active
        * -Git Use git for diffs
        * -WinMerge Use winmerge for diffs (Windows only)
        * -VSCode Use Visual Studio Code for diffs
        * -Path Available with the above 3 switches: Specify path to executable

    .EXAMPLE
        Set-ATConfigurationItem -ConfigurationItem DiffTool -Git

        Sets git diff as the file difference tool. Git executable is searched for in the system path

    .EXAMPLE
        Set-ATConfigurationItem -ConfigurationItem DiffTool -Git

        Sets git diff as the file difference tool. Git executable is searched for in the system path

    .EXAMPLE
        Set-ATConfigurationItem -ConfigurationItem DiffTool -Git -Path /opt/git/bin/git

        Sets git diff as the file difference tool, with executable located at specificed path.

    .EXAMPLE
        Set-ATConfigurationItem -ConfigurationItem DiffTool -WinMerge

        Sets winmerge as the file difference tool. Executable is searched for in known installation locations.
        This option is unavailable on non-windows operating systems

    .NOTES
        Supported diff tools are
        - git (all platforms) -Git
        - Winmerge (Windows only) -Winmerge
        - Visual Studio Code (all platforms) -VSCode

        Winmerge is generally preferable as it can set specific titles for the files being compared as opposed to just the file paths.
        This is especially useful when running Compare-ATDeployedStackWithSourceTemplate as the stack version is titled with the
        stack name rather than the temporary file path where the stack version has been downloaded to.

        If you have a diff tool you would like added, raise an issue in github.

#>

    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [ValidateSet('DiffTool')]
        [string]$ConfigurationItem
    )

    DynamicParam
    {

        #Create the RuntimeDefinedParameterDictionary
        $dpDict = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        switch ($ConfigurationItem)
        {
            'DiffTool'
            {
                New-DynamicParam -Name 'Path' -Type String -HelpMessage "Path to diff tool" -DPDictionary $dpDict

                # Generate remaining parameters from diff-tools.json
                New-DiffToolDynamicParameters -DPDictionary $dpDict
            }
        }

        $dpDict
    }

    begin
    {
        # This standard block of code loops through bound parameters...
        # If no corresponding variable exists, one is created
        # Get common parameters, pick out bound parameters not in that set
        Function _temp { [cmdletbinding()] param() }
        $BoundKeys = $PSBoundParameters.keys | Where-Object { (get-command _temp | Select-Object -ExpandProperty parameters).Keys -notcontains $_ }
        foreach ($param in $BoundKeys)
        {
            if (-not ( Get-Variable -name $param -scope 0 -ErrorAction SilentlyContinue ) )
            {
                New-Variable -Name $Param -Value $PSBoundParameters.$param
            }
        }
    }

    end
    {
        switch ($ConfigurationItem)
        {
            'DiffTool'
            {
                if ($null -eq $script:PluginConfig)
                {
                    Write-Warning "Cannot configure diff tool. plugin-config.json missing or corrupt."
                    return
                }

                $diffConfig = $null

                $userPath = $(

                    if (Get-Variable -name 'Path' -scope 0 -ErrorAction SilentlyContinue)
                    {
                        $Path
                    }
                    else
                    {
                        $null
                    }
                )

                $diffToolPlugin = $script:PluginConfig.DiffTools |
                Where-Object {
                    $_.Name -ieq $PSCmdlet.ParameterSetName
                }

                if ($diffToolPlugin)
                {
                    $diffConfig = New-DiffTool -DiffToolConfig $diffToolPlugin -UserPath $userPath

                    if ($diffConfig)
                    {
                        if ($script:moduleConfig.HasItem('DiffTool'))
                        {
                            $script:moduleConfig.DiffTool = $diffConfig
                        }
                        else
                        {
                            Add-Member -InputObject $script:moduleConfig -MemberType NoteProperty -Name DiffTool -Value $diffConfig
                        }

                        Update-AwsToolboxConfiguration
                    }
                }
            }
        }
    }
}