Functions/Get-SciProfileConfig.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
# ===========================================================================
# Get-SciProfileConfig.ps1 -----------------------------------------------
# ===========================================================================

# function ----------------------------------------------------------------
# ---------------------------------------------------------------------------
function Get-SciProfileConfig {

    <#
    .SYNOPSIS
        Get the content of module's configuration files.
 
    .DESCRIPTION
        Displays the content of module's configuration files in powershell. All available configuration files can be accessed by autocompletion.
     
    .PARAMETER Name
 
    .PARAMETER Unformatted
 
    .EXAMPLE
        PS C:\> Get-SciProfileConfig -Name config.ini
 
        Name Value
        ---- -----
        work-dir A:\.config\SciProfile
        project-file A:\.config\SciProfile\config\project.json
        project-alias papis workspace psmodule
        import-file A:\.config\SciProfile\config\import.json
        default-editor code
        editor-arguments --new-window --disable-gpu
        config-lib pocs-config
        virtual-env papis-dev
        module-dir A:\Documents\PowerShell\Modules
 
        -----------
        Description
        Displays the content of module's configuration files in powershell. All available configuration files can be accessed by autocompletion.
 
    .INPUTS
        None.
 
    .OUTPUTS
        System.Object. Content of module's configuration files.
    #>


    [CmdletBinding(PositionalBinding)]

    [OutputType([System.Object])]

    Param(
        [ValidateSet([ValidateSciProfileConfigFiles])]
        [Parameter(Position=1, HelpMessage="File name of a configuration file.")]
        [System.String] $Name = "config.ini",

        [Parameter(HelpMessage="Return information not as readable table with additional details.")]
        [Switch] $Unformatted
    )

    Process {

        foreach($config_file in $SciProfile.ConfigFileList){
            if ($config_file -match $Name){
                $file = $config_file
            }
        }

        switch ([System.IO.Path]::GetExtension($file)){
            ".ini" {
                $config_content = Get-IniContent -FilePath $file -IgnoreComments
                $config_content = Format-IniContent -Content $config_content -Substitution $SciProfile 
                
                $result = @()
                $config_content.Keys | ForEach-Object {
                    $result += $config_content[$_]
                }
                break
            }
            ".json" {
                $result = Get-Content -Path $file | ConvertFrom-Json
                break
            }
            default { 
                $result = Get-Content -Path $file
                $Unformatted = $True
                break
            }
        }

        if ($Unformatted) {
            return $result
        }
        return $result | Format-Table
    }
}