Functions/Open-Repo.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
<#
    .SYNOPSIS
        Changes your location to the directory of the repository you have specified.
 
    .DESCRIPTION
        Quickly changes the location of your session to the directory holding the
        repository you have specified, if it exists.
 
    .PARAMETER Repo
        The name of the repository which you would like to open.
 
    .PARAMETER Service
        The remote hosting provider from which you would like to open the repository.
 
    .PARAMETER Path
        The directory within which all of your repositories are stored. Defaults to the value of $GitTool.Directory
        if not specified.
 
    .EXAMPLE
        Open-Repo sierrasoftworks/git-tool
 
        Name Value
        ---- -----
        WebURL https://github.com/SierraSoftworks/git-tool
        Service github.com
        Repo SierraSoftworks/git-tool
        Path C:\dev\github.com\SierraSoftworks\git-tool
        Exists True
        GitURL git@github.com:SierraSoftworks/git-tool.git
 
        C:\dev\github.com\SierraSoftworks\git-tool PS>
#>

function Open-Repo {
    param(
        [string]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, HelpMessage = "The name of the repository that you wish to synchronize (e.g. Namespace/RepoName)")]
        $Repo,

        [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
    )

    $info = Get-RepoInfo -Repo $Repo -Service $Service -Path $Path

    if (-not $info.Exists) {
        Write-Error -Category ResourceExists -Message "The repository $Service/$Repo does not exist." -RecommendedAction "Use New-Repo to create it."
        return
    }

    Set-Location -Path $info.Path

    return $info
}