Functions/Get-Repos.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
<#
    .SYNOPSIS
        Gets a list of all the known repositories available for a certain hosting service.
 
    .DESCRIPTION
        Lists all of the repositories for a given service for which you have local copies. These repositories
        may then be used to perform bulk operations, searches and more. In addition to this, the data reported
        by this command will be used for autocompletion of repository names in commands like Get-Repo, Get-RepoInfo,
        Open-Repo and more.
 
    .PARAMETER Service
        The remote hosting provider for the repositories 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-Repos
        SierraSoftworks/bender
        SierraSoftworks/git-tool
        SierraSoftworks/iridium
        SierraSoftworks/sentry-go
        SierraSoftworks/vue-template
 
    .LINK
        Get-Repo
 
    .LINK
        Get-RepoInfo
 
    .LINK
        Open-Repo
#>

function Get-Repos {
    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 = 0; $i -lt $GitTool.Services[$Service].NamespaceDepth; $i++) {
        $matcher = "$matcher/*"
    }

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