Functions/Get-RepoNamespaces.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
<#
    .SYNOPSIS
        Gets a list of all the known repository namespaces available for a certain hosting service.
 
    .DESCRIPTION
        Lists all of the namespaces for a given service for which you have local repositories. These
        namespaces form the first component of a repository name (i.e. namespace/name) and this information
        will primarily be used to provide autocompletion hints for the New-Repo command.
 
    .PARAMETER Service
        The remote hosting provider for the repository namespaces in question.
     
    .PARAMETER Path
        The directory within which all of your repositories are stored. Defaults to the value of $GitTool.Directory
        if not specified.
     
    .EXAMPLE
        Get-RepoNamespaces
        SierraSoftworks
        spartan563
 
    .LINK
        New-Repo
#>

function Get-RepoNamespaces {
    param (
        [string]
        [Parameter(HelpMessage = "The service hosting your repository (e.g. github.com)")]
        $Service = $GitTool.Service,

        [string]
        [Parameter(HelpMessage = "The directory within which your repositories will be checked out (e.g. /src/)")]
        $Path = $GitTool.Directory
    )

    $servicePath = [System.IO.Path]::Combine($Path, $Service)
    
    $matcher = "*"
    for ($i = 1; $i -lt $GitTool.Services[$Service].NamespaceDepth; $i++) {
        $matcher = "$matcher/*"
    }

    Get-ChildItem -Path $servicePath -Directory -Depth ($GitTool.Services[$Service].NamespaceDepth - 1) | ForEach-Object {
        $_.FullName.Substring($servicePath.Length).Replace([System.IO.Path]::DirectorySeparatorChar, "/").Trim("/")
    } | Sort-Object -Unique | Where-Object { $_ -like $matcher }
}