-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbatools.ai.psm1
86 lines (74 loc) · 2.64 KB
/
dbatools.ai.psm1
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
$script:ModuleRoot = $PSScriptRoot
$script:dbSchema = @{}
$script:foreignKeyCache = @{}
$script:dbassist = {}
$script:threadcache = @{}
$script:ModuleRootLib = Join-Path -Path $script:ModuleRoot -Childpath lib
switch ($PSVersionTable.Platform) {
"Unix" {
$script:configdir = "$home/.config/dbatools.ai"
if (-not (Test-Path -Path $script:configdir)) {
$null = New-Item -Path $script:configdir -ItemType Directory -Force
}
}
default {
$script:configdir = "$env:APPDATA\dbatools.ai"
if (-not (Test-Path -Path $script:configdir)) {
$null = New-Item -Path $script:configdir -ItemType Directory -Force
}
}
}
function Import-ModuleFile {
[CmdletBinding()]
Param (
[string]
$Path
)
if ($doDotSource) { . $Path }
else { $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($Path))), $null, $null) }
}
# Import all internal functions
foreach ($function in (Get-ChildItem "$ModuleRoot\private\" -Filter "*.ps1" -Recurse -ErrorAction Ignore)) {
. Import-ModuleFile -Path $function.FullName
}
# Import all public functions
foreach ($function in (Get-ChildItem "$ModuleRoot\public" -Filter "*.ps1" -Recurse -ErrorAction Ignore)) {
. Import-ModuleFile -Path $function.FullName
}
# Create powershell alias called dbai for Invoke-DbaiQuery
Set-Alias -Name dbai -Value Invoke-DbaiQuery
Set-Alias -Name dtai -Value Invoke-DbatoolsAI
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
Set-Alias -Name Reset-DbaiProvider -Value Clear-DbaiProvider
$configFile = Join-Path -Path $script:configdir -ChildPath config.json
if (Test-Path -Path $configFile) {
$persisted = Get-Content -Path $configFile -Raw | ConvertFrom-Json
$splat = @{}
if ($persisted.ApiKey) {
$splat.ApiKey = $persisted.ApiKey
}
if ($persisted.ApiBase) {
$splat.ApiBase = $persisted.ApiBase
}
if ($persisted.Deployment) {
$splat.Deployment = $persisted.Deployment
}
if ($persisted.ApiType) {
$splat.ApiType = $persisted.ApiType
}
if ($persisted.ApiVersion) {
$splat.ApiVersion = $persisted.ApiVersion
}
if ($persisted.AuthType) {
$splat.AuthType = $persisted.AuthType
}
if ($persisted.Organization) {
$splat.Organization = $persisted.Organization
}
$null = Set-DbaiProvider @splat
}
$PSDefaultParameterValues['Import-Module:Verbose'] = $false
$PSDefaultParameterValues['Add-Type:Verbose'] = $false
if (-not (Get-DbaiProvider).ApiKey) {
Write-Warning "No API key found. Use Set-DbaiProvider or `$env:OPENAI_API_KEY to set the API key."
}