Skip to content

Commit 6ae47dd

Browse files
committed
feat: support custom import prefix
1 parent 1086f45 commit 6ae47dd

File tree

7 files changed

+58
-50
lines changed

7 files changed

+58
-50
lines changed

PSSourcegraph.psd1

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@
7777
# NestedModules = @()
7878

7979
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
80+
# Note the DefaultCommandPrefix will be added automatically to each of these on import.
8081
FunctionsToExport = @(
81-
'Get-SourcegraphRepository'
82-
'Get-SourcegraphHover'
83-
'Get-SourcegraphDefinition'
84-
'Get-SourcegraphReference'
85-
'Get-SourcegraphUser'
86-
'Invoke-SourcegraphApiRequest'
87-
'New-SourcegraphUser'
88-
'Search-Sourcegraph'
82+
'Get-Repository'
83+
'Get-Hover'
84+
'Get-Definition'
85+
'Get-Reference'
86+
'Get-User'
87+
'Invoke-ApiRequest'
88+
'New-User'
89+
'Search-'
8990
)
9091

9192
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
@@ -95,16 +96,7 @@
9596
VariablesToExport = @()
9697

9798
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
98-
AliasesToExport = @(
99-
'Get-SrcRepository',
100-
'Get-SrcHover',
101-
'Get-SrcDefinition',
102-
'Get-SrcReference',
103-
'Get-SrcUser',
104-
'New-SrcUser',
105-
'Invoke-SrcApiRequest',
106-
'Search-Src'
107-
)
99+
AliasesToExport = @()
108100

109101
# DSC resources to export from this module
110102
# DscResourcesToExport = @()
@@ -143,7 +135,6 @@
143135
# HelpInfoURI = ''
144136

145137
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
146-
# DefaultCommandPrefix = ''
147-
138+
DefaultCommandPrefix = 'Sourcegraph'
148139
}
149140

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ Use `Get-Help` to see documentation for any command.
3939

4040
Missing something? Please file an issue!
4141

42+
## Alias
43+
44+
By default, all commands are prefixed with `Sourcegraph` like shown above.
45+
If you prefer a shorter prefix, e.g. `Src` or `Sg`, you can import the module under a different prefix in your `$PROFILE`:
46+
47+
```powershell
48+
Import-Module PSSourcegraph -Prefix Src
49+
```
50+
51+
The commands are then available under that prefix, e.g. `Search-Src` or `Get-SrcHover`.
52+
4253
## Configuration
4354

4455
You can use a private instance by passing an instance URL with the `-Endpoint` parameter and an API token with the `-Token` parameter to any command.

src/api.psm1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
function Invoke-SourcegraphApiRequest {
1+
function Invoke-ApiRequest {
32
<#
43
.SYNOPSIS
54
Invoke an API Request to the Sourcegraph GraphQL API
@@ -15,11 +14,11 @@ function Invoke-SourcegraphApiRequest {
1514
.PARAMETER Token
1615
The authentication token (if needed). Go to the settings page on Sourcegraph to generate one.
1716
.EXAMPLE
18-
C:\PS> Invoke-SourcegraphApiRequest 'query { currentUser { username } }'
17+
Invoke-SourcegraphApiRequest 'query { currentUser { username } }'
1918
2019
Echo back the username of the authenticated user
2120
.EXAMPLE
22-
C:\PS> Invoke-SourcegraphApiRequest 'query($query: String) { search(query: $query) { results { resultCount } } }' @{query = 'repogroup:sample test'}
21+
Invoke-SourcegraphApiRequest 'query($query: String) { search(query: $query) { results { resultCount } } }' @{query = 'repogroup:sample test'}
2322
2423
Search the repogroup "sample" for the term "test"
2524
#>
@@ -73,4 +72,3 @@ function Invoke-SourcegraphApiRequest {
7372
}
7473
$parsed.data
7574
}
76-
Set-Alias Invoke-SrcApiRequest Invoke-SourcegraphApiRequest

src/codeintel.psm1

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
Import-Module -Scope Local "$PSScriptRoot/api.psm1"
2+
13
$HoverQuery = Get-Content -Raw "$PSScriptRoot/queries/Hover.graphql"
24
$DefinitionQuery = Get-Content -Raw "$PSScriptRoot/queries/Definition.graphql"
35
$ReferenceQuery = Get-Content -Raw "$PSScriptRoot/queries/Reference.graphql"
46

5-
function Get-SourcegraphHover {
7+
function Get-Hover {
68
<#
79
.SYNOPSIS
810
Gets the hover content for a token at a given position in a file.
@@ -63,7 +65,7 @@ function Get-SourcegraphHover {
6365
line = $LineNumber
6466
character = $_
6567
}
66-
$data = Invoke-SourcegraphApiRequest -Query $HoverQuery -Variables $variables -Endpoint $Endpoint -Token $Token
68+
$data = Invoke-ApiRequest -Query $HoverQuery -Variables $variables -Endpoint $Endpoint -Token $Token
6769
if (!$data.repository) {
6870
Write-Error "Repository $RepositoryName not found"
6971
return
@@ -89,9 +91,9 @@ function Get-SourcegraphHover {
8991
}
9092
}
9193
}
92-
Set-Alias Get-SrcHover Get-SourcegraphHover
94+
Export-ModuleMember -Function Get-Hover
9395

94-
function Get-SourcegraphDefinition {
96+
function Get-Definition {
9597
<#
9698
.SYNOPSIS
9799
Gets the definition location for a token at a given position in a file.
@@ -154,7 +156,7 @@ function Get-SourcegraphDefinition {
154156
line = $LineNumber
155157
character = $_
156158
}
157-
$data = Invoke-SourcegraphApiRequest -Query $DefinitionQuery -Variables $variables -Endpoint $Endpoint -Token $Token
159+
$data = Invoke-ApiRequest -Query $DefinitionQuery -Variables $variables -Endpoint $Endpoint -Token $Token
158160
if (!$data.repository) {
159161
Write-Error "Repository $RepositoryName not found"
160162
return
@@ -181,9 +183,9 @@ function Get-SourcegraphDefinition {
181183
}
182184
}
183185
}
184-
Set-Alias Get-SrcDefinition Get-SourcegraphDefinition
186+
Export-ModuleMember -Function Get-Definition
185187

186-
function Get-SourcegraphReference {
188+
function Get-Reference {
187189
<#
188190
.SYNOPSIS
189191
Gets the reference locations for a given position in a file.
@@ -252,7 +254,7 @@ function Get-SourcegraphReference {
252254
after = $null
253255
}
254256
while ($true) {
255-
$data = Invoke-SourcegraphApiRequest -Query $ReferenceQuery -Variables $variables -Endpoint $Endpoint -Token $Token
257+
$data = Invoke-ApiRequest -Query $ReferenceQuery -Variables $variables -Endpoint $Endpoint -Token $Token
256258
if (!$data.repository) {
257259
Write-Error "Repository $RepositoryName not found"
258260
return
@@ -285,4 +287,4 @@ function Get-SourcegraphReference {
285287
}
286288
}
287289
}
288-
Set-Alias Get-SrcReference Get-SourcegraphReference
290+
Export-ModuleMember -Function Get-Reference

src/repos.psm1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
Import-Module -Scope Local "$PSScriptRoot/api.psm1"
12

23
$repositoryFields = Get-Content -Raw "$PSScriptRoot/queries/RepositoryFields.graphql"
34
$repositoryQuery = (Get-Content -Raw "$PSScriptRoot/queries/Repository.graphql") + $repositoryFields
45
$repositoriesQuery = (Get-Content -Raw "$PSScriptRoot/queries/Repositories.graphql") + $repositoryFields
5-
function Get-SourcegraphRepository {
6+
function Get-Repository {
67
<#
78
.SYNOPSIS
89
List all repositories known to a Sourcegraph instance
@@ -47,7 +48,7 @@ function Get-SourcegraphRepository {
4748
id = $Id
4849
cloneUrl = $CloneUrl
4950
}
50-
$data = Invoke-SourcegraphApiRequest -Query $repositoryQuery -Variables $vars -Endpoint $Endpoint -Token $Token
51+
$data = Invoke-ApiRequest -Query $repositoryQuery -Variables $vars -Endpoint $Endpoint -Token $Token
5152
$data.repository
5253
if ($PSCmdlet.PagingParameters.IncludeTotalCount) {
5354
$count = if ($data.repository) { 1 } else { 0 }
@@ -66,12 +67,12 @@ function Get-SourcegraphRepository {
6667
orderBy = $SortBy
6768
descending = [bool]$Descending
6869
}
69-
$data = Invoke-SourcegraphApiRequest -Query $repositoriesQuery -Variables $vars -Endpoint $Endpoint -Token $Token
70+
$data = Invoke-ApiRequest -Query $repositoriesQuery -Variables $vars -Endpoint $Endpoint -Token $Token
7071
if ($PSCmdlet.PagingParameters.IncludeTotalCount) {
7172
$PSCmdlet.PagingParameters.NewTotalCount($data.repositories.totalCount, 1)
7273
}
7374
$data.repositories.nodes | Select-Object -Skip $PSCmdlet.PagingParameters.Skip
7475
}
7576
}
7677
}
77-
Set-Alias Get-SrcRepository Get-SourcegraphRepository
78+
Export-ModuleMember -Function Get-Repository

src/search.psm1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
Import-Module -Scope Local "$PSScriptRoot/api.psm1"
12

23
$RepositoryFields = Get-Content -Raw "$PSScriptRoot/queries/RepositoryFields.graphql"
34
$SearchQuery = (Get-Content -Raw "$PSScriptRoot/queries/Search.graphql") + $RepositoryFields
45
$SuggestionsQuery = (Get-Content -Raw "$PSScriptRoot/queries/Suggestions.graphql")
56

6-
function Search-Sourcegraph {
7+
# Note: The default name of this function is Search-Sourcegraph,
8+
# the prefix/suffix is added automatically as configured in PSSourcegraph.psd1
9+
# or overridden when calling Import-Module.
10+
function Search- {
711
<#
812
.SYNOPSIS
913
Get users on a Sourcegraph instance
@@ -48,7 +52,7 @@ function Search-Sourcegraph {
4852
query = $Query
4953
patternType = $PSCmdlet.ParameterSetName
5054
}
51-
$data = Invoke-SourcegraphApiRequest -Query $SearchQuery -Variables $variables -Endpoint $Endpoint -Token $Token
55+
$data = Invoke-ApiRequest -Query $SearchQuery -Variables $variables -Endpoint $Endpoint -Token $Token
5256
if ($data.search.results.cloning.Count -gt 0) {
5357
Write-Warning "Cloning:"
5458
$data.search.results.cloning.name | Write-Warning
@@ -99,9 +103,9 @@ function Search-Sourcegraph {
99103
}
100104
}
101105
}
102-
Set-Alias Search-Src Search-Sourcegraph
106+
Export-ModuleMember -Function Search-
103107

104-
function Get-SourcegraphSearchSuggestions {
108+
function Get-SearchSuggestions {
105109
[CmdletBinding(DefaultParameterSetName = 'literal')]
106110
param (
107111
[Parameter(Mandatory)]
@@ -131,7 +135,7 @@ function Get-SourcegraphSearchSuggestions {
131135
first = 10
132136
patternType = $PSCmdlet.ParameterSetName
133137
}
134-
Invoke-SourcegraphApiRequest -Query $SuggestionsQuery -Variables $vars -Endpoint $Endpoint -Token $Token |
138+
Invoke-ApiRequest -Query $SuggestionsQuery -Variables $vars -Endpoint $Endpoint -Token $Token |
135139
ForEach-Object { $_.search.suggestions }
136140
}
137141
}

src/users.psm1

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
Import-Module -Scope Local "$PSScriptRoot/api.psm1"
12

23
$CreateUserQuery = Get-Content -Raw "$PSScriptRoot/queries/CreateUser.graphql"
3-
function New-SourcegraphUser {
4+
function New-User {
45
<#
56
.SYNOPSIS
67
Creates a new user account
@@ -30,16 +31,16 @@ function New-SourcegraphUser {
3031
)
3132

3233
if ($PSCmdlet.ShouldProcess("Creating user $Username <$Email>", "Create user $Username <$Email>?", "Confirm")) {
33-
$data = Invoke-SourcegraphApiRequest -Query $CreateUserQuery -Variables @{username = $Username; email = $Email} -Endpoint $Endpoint -Token $Token
34+
$data = Invoke-ApiRequest -Query $CreateUserQuery -Variables @{username = $Username; email = $Email} -Endpoint $Endpoint -Token $Token
3435
$data.createUser
3536
}
3637
}
37-
Set-Alias New-SrcUser New-SourcegraphUser
38+
Export-ModuleMember -Function New-User
3839

3940
$UserFields = Get-Content -Raw "$PSScriptRoot/queries/UserFields.graphql"
4041
$UserQuery = (Get-Content -Raw "$PSScriptRoot/queries/User.graphql") + $UserFields
4142
$UsersQuery = (Get-Content -Raw "$PSScriptRoot/queries/Users.graphql") + $UserFields
42-
function Get-SourcegraphUser {
43+
function Get-User {
4344
<#
4445
.SYNOPSIS
4546
Get users on a Sourcegraph instance
@@ -66,17 +67,17 @@ function Get-SourcegraphUser {
6667
if ($Username) {
6768
$query = $UserQuery
6869
$variables = @{ username = $Username }
69-
(Invoke-SourcegraphApiRequest -Query $UserQuery -Variables $variables -Endpoint $Endpoint -Token $Token).user
70+
(Invoke-ApiRequest -Query $UserQuery -Variables $variables -Endpoint $Endpoint -Token $Token).user
7071
} else {
7172
$variables = @{ query = $Query; tag = $Tag; activePeriod = $ActivePeriod }
7273
if ($PSBoundParameters.ContainsKey('First')) {
7374
$variables['first'] = $PSCmdlet.PagingParameters.First
7475
}
75-
$data = Invoke-SourcegraphApiRequest -Query $UsersQuery -Variables $variables -Endpoint $Endpoint -Token $Token
76+
$data = Invoke-ApiRequest -Query $UsersQuery -Variables $variables -Endpoint $Endpoint -Token $Token
7677
if ($PSCmdlet.PagingParameters.IncludeTotalCount) {
7778
$PSCmdlet.PagingParameters.NewTotalCount($data.users.totalCount, 1)
7879
}
7980
$data.users.nodes
8081
}
8182
}
82-
Set-Alias Get-SrcUser Get-SourcegraphUser
83+
Export-ModuleMember -Function Get-User

0 commit comments

Comments
 (0)