Public/Credentials.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
function Set-MangaDexCredentials {
    [CmdletBinding(DefaultParameterSetName = 'Login')]
    Param(
        [Parameter(ParameterSetName = 'Login', HelpMessage = 'Enter your MangaDex password.')]
        [String]$Username,

        [Parameter(ParameterSetName = 'Login', HelpMessage = 'Enter your MangaDex password.')]
        [SecureString]$Password
    )

    if($Username -or $Password) {
        $config = Get-Content -Path $MDX_Config

        if($Username) {
            $config.IndexOf( ($config | Where-Object {$_ -match 'username=.+'}) ) = 'username={0}' -f $Username
        }

        if($Password) {
            $config.IndexOf( ($config | Where-Object { $_ -match 'password=.+' }) ) = 'password={0}' -f ($Password | ConvertFrom-SecureString)
            Remove-Variable Password
        }

        Set-Content -Path $MDX_Config -Value $config
    }
}


function Get-MangaDexCredentials {
    [CmdletBinding(DefaultParameterSetName = 'None')]
    Param(
        [Parameter(ParameterSetName = 'Login')]
        [Switch]$Username,
        [Parameter(ParameterSetName = 'Login')]
        [Switch]$Password
    )

    if($PSCmdlet.ParameterSetName -eq 'None') {
        return @{
            username = (Get-IniContent -FilePath $MDX_Config)['Credentials']['username']
            password = (Get-IniContent -FilePath $MDX_Config)['Credentials']['password']
        }
    } else {
        if($Username) {
            return (Get-IniContent -FilePath $MDX_Config)['Credentials']['username']
        } elseif($Password) {
            return (Get-IniContent -FilePath $MDX_Config)['Credentials']['password']
        }
    }
}

function Remove-MangaDexCredentials {
    Param(
        [Switch]$Username,
        [Switch]$Password
    )

    $config = Get-Content -Path $MDX_Config

    if ($Username) {
        $config.IndexOf( ($config | Where-Object { $_ -match 'username=.*' }) ) = 'username='
    }

    if ($Password) {
        $config.IndexOf( ($config | Where-Object { $_ -match 'password=.*' }) ) = 'password='
    }

    Set-Content -Path $MDX_Config -Value $config
}