Skip to content

Commit

Permalink
Changed ConfigServer to be in module's scope
Browse files Browse the repository at this point in the history
  • Loading branch information
lipkau authored and AtlassianPS Automated User committed Aug 15, 2019
1 parent e23a63b commit 6f6cd04
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 196 deletions.
53 changes: 2 additions & 51 deletions JiraPS/Public/Get-JiraConfigServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ function Get-JiraConfigServer {
# .ExternalHelp ..\JiraPS-help.xml
[CmdletBinding()]
[OutputType([System.String])]
param(
[String]
$ConfigFile
)
param()

begin {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
Expand All @@ -15,53 +12,7 @@ function Get-JiraConfigServer {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)"
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

# Using a default value for this parameter wouldn't handle all cases. We want to make sure
# that the user can pass a $null value to the ConfigFile parameter...but if it's null, we
# want to default to the script variable just as we would if the parameter was not
# provided at all.

if (-not ($ConfigFile)) {
# This file should be in $moduleRoot/Functions/Internal, so PSScriptRoot will be $moduleRoot/Functions
$moduleFolder = Split-Path -Path $PSScriptRoot -Parent
$ConfigFile = Join-Path -Path $moduleFolder -ChildPath 'config.xml'
}

if (-not (Test-Path -Path $ConfigFile)) {
$exception = ([System.IO.FileNotFoundException]"Could not find $ConfigFile")
$errorId = 'ConfigFile.NotFound'
$errorCategory = 'ObjectNotFound'
$errorTarget = $ConfigFile
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
$errorItem.ErrorDetails = "Config file [$ConfigFile] does not exist. Use Set-JiraConfigServer first to define the configuration file."
$PSCmdlet.ThrowTerminatingError($errorItem)
}

$xml = New-Object -TypeName XML
$xml.Load($ConfigFile)

$xmlConfig = $xml.DocumentElement
if ($xmlConfig.LocalName -ne 'Config') {
$exception = ([System.IO.FileFormatException]"XML had not the expected format")
$errorId = 'ConfigFile.UnexpectedElement'
$errorCategory = ParserError
$errorTarget = $ConfigFile
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
$errorItem.ErrorDetails = "Unexpected document element [$($xmlConfig.LocalName)] in configuration file [$ConfigFile]. You may need to delete the config file and recreate it using Set-JiraConfigServer."
$PSCmdlet.ThrowTerminatingError($errorItem)
}

if ($xmlConfig.Server) {
Write-Output $xmlConfig.Server
}
else {
$exception = ([System.UriFormatException]"Could not find URI")
$errorId = 'ConfigFile.EmptyElement'
$errorCategory = OpenError
$errorTarget = $ConfigFile
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
$errorItem.ErrorDetails = "No Server element is defined in the config file. Use Set-JiraConfigServer to define one."
$PSCmdlet.ThrowTerminatingError($errorItem)
}
return $script:JiraServerUrl
}

end {
Expand Down
59 changes: 2 additions & 57 deletions JiraPS/Public/Set-JiraConfigServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,15 @@ function Set-JiraConfigServer {
[ValidateNotNullOrEmpty()]
[Alias('Uri')]
[Uri]
$Server,

[String]
$ConfigFile
$Server
)

begin {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
}

process {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)"
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

# Using a default value for this parameter wouldn't handle all cases. We want to make sure
# that the user can pass a $null value to the ConfigFile parameter...but if it's null, we
# want to default to the script variable just as we would if the parameter was not
# provided at all.

if (-not ($ConfigFile)) {
# This file should be in $moduleRoot/Functions/Internal, so PSScriptRoot will be $moduleRoot/Functions
$moduleFolder = Split-Path -Path $PSScriptRoot -Parent
$ConfigFile = Join-Path -Path $moduleFolder -ChildPath 'config.xml'
}

Write-Debug "[$($MyInvocation.MyCommand.Name)] Config file path: $ConfigFile"
if (-not (Test-Path -Path $ConfigFile)) {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] Creating new Config file"
$xml = [XML] '<Config></Config>'
}
else {
Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] Using existing Config file"
$xml = New-Object -TypeName XML
$xml.Load($ConfigFile)
}

$xmlConfig = $xml.DocumentElement
if ($xmlConfig.LocalName -ne 'Config') {
$exception = ([System.ArgumentException]"Invalid Document")
$errorId = 'InvalidObject.InvalidDocument'
$errorCategory = 'InvalidData'
$errorTarget = $_
$errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
$errorItem.ErrorDetails = "Unexpected document element [$($xmlConfig.LocalName)] in configuration file. You may need to delete the config file and recreate it using this function."
$PSCmdlet.ThrowTerminatingError($errorItem)
}

$fixedServer = $Server.AbsoluteUri.Trim('/')

if ($xmlConfig.Server) {
$xmlConfig.Server = $fixedServer
}
else {
$xmlServer = $xml.CreateElement('Server')
$xmlServer.InnerText = $fixedServer
[void] $xmlConfig.AppendChild($xmlServer)
}

try {
$xml.Save($ConfigFile)
}
catch {
throw $_
}
$script:JiraServerUrl = $Server
}

end {
Expand Down
13 changes: 5 additions & 8 deletions Tests/Functions/Get-JiraConfigServer.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ Describe "Get-JiraConfigServer" -Tag 'Unit' {

$jiraServer = 'http://jiraserver.example.com'

$configFile = Join-Path -Path $TestDrive -ChildPath 'config.xml'

It "Throws an exception if the config file does not exist" {
{ Get-JiraConfigServer -ConfigFile $configFile } | Should Throw
It "returns empty if no server has been set" {
Get-JiraConfigServer | Should -BeNullOrEmpty
}

It "Returns the defined Server in the config.xml file" {
Set-JiraConfigServer -Server $jiraServer -ConfigFile $configFile
$s = Get-JiraConfigServer -ConfigFile $configFile
$s | Should Be $jiraServer
It "returns the server stored in the module's session" {
$script:JiraServerUrl = $jiraServer
Get-JiraConfigServer | Should -Be $jiraServer
}
}
}
33 changes: 3 additions & 30 deletions Tests/Functions/Set-JiraConfigServer.Unit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,10 @@ Describe "Set-JiraConfigServer" -Tag 'Unit' {

$jiraServer = 'http://jiraserver.example.com'

$configFile = Join-Path -Path $TestDrive -ChildPath 'config.xml'
Set-JiraConfigServer -Server $jiraServer -ConfigFile $configFile
It "stores the server address in the module session" {
Set-JiraConfigServer -Server $jiraServer

It "Ensures that a config.xml file exists" {
$configFile | Should Exist
}

$xml = New-Object -TypeName Xml
$xml.Load($configFile)
$xmlServer = $xml.Config.Server

It "Ensures that the XML file has a Config.Server element" {
$xmlServer | Should Not BeNullOrEmpty
}

It "Sets the config file's Server value " {
$xmlServer | Should Be $jiraServer
}

It "Trims whitespace from the provided Server parameter" {
Set-JiraConfigServer -Server "$jiraServer " -ConfigFile $configFile
$xml = New-Object -TypeName Xml
$xml.Load($configFile)
$xml.Config.Server | Should Be $jiraServer
}

It "Trims trailing slasher from the provided Server parameter" {
Set-JiraConfigServer -Server "$jiraServer/" -ConfigFile $configFile
$xml = New-Object -TypeName Xml
$xml.Load($configFile)
$xml.Config.Server | Should Be $jiraServer
$script:JiraServerUrl | Should -Be "$jiraServer/"
}
}
}
32 changes: 4 additions & 28 deletions docs/en-US/commands/Get-JiraConfigServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ schema: 2.0.0
layout: documentation
permalink: /docs/JiraPS/commands/Get-JiraConfigServer/
---

# Get-JiraConfigServer

## SYNOPSIS
Expand All @@ -23,8 +24,6 @@ Get-JiraConfigServer [[-ConfigFile] <String>] [<CommonParameters>]

This function returns the configured URL for the JIRA server that JiraPS should manipulate.

By default, this is stored in a config.xml file at the module's root path.

## EXAMPLES

### EXAMPLE 1
Expand All @@ -33,34 +32,10 @@ By default, this is stored in a config.xml file at the module's root path.
Get-JiraConfigServer
```

Returns the server URL of the JIRA server configured in the JiraPS config file.

### EXAMPLE 2

```powershell
Get-JiraConfigServer -ConfigFile C:\jiraconfig.xml
```

Returns the server URL of the JIRA server configured at C:\jiraconfig.xml.
Returns the server URL of the JIRA server configured for the JiraPS module.

## PARAMETERS

### -ConfigFile

Path to the configuration file, if not the default.

```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
Expand All @@ -76,7 +51,8 @@ For more information, see about_CommonParameters (http://go.microsoft.com/fwlink

Support for multiple configuration files is limited at this point in time, but enhancements are planned for a future update.

<TODO: link to issue for tracking>
<https://github.com/AtlassianPS/JiraPS/issues/45>
<https://github.com/AtlassianPS/JiraPS/issues/194>

## RELATED LINKS

Expand Down
25 changes: 3 additions & 22 deletions docs/en-US/commands/Set-JiraConfigServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ schema: 2.0.0
layout: documentation
permalink: /docs/JiraPS/commands/Set-JiraConfigServer/
---

# Set-JiraConfigServer

## SYNOPSIS
Expand All @@ -16,15 +17,13 @@ Defines the configured URL for the JIRA server
## SYNTAX

```powershell
Set-JiraConfigServer [-Server] <Uri> [[-ConfigFile] <String>] [<CommonParameters>]
Set-JiraConfigServer [-Server] <Uri> [<CommonParameters>]
```

## DESCRIPTION

This function defines the configured URL for the JIRA server that JiraPS should manipulate.

By default, this is stored in a config.xml file at the module's root path.

## EXAMPLES

### EXAMPLE 1
Expand All @@ -33,7 +32,7 @@ By default, this is stored in a config.xml file at the module's root path.
Set-JiraConfigServer 'https://jira.example.com:8080'
```

This example defines the server URL of the JIRA server configured in the JiraPS config file.
This example defines the server URL of the JIRA server configured for the JiraPS module.

## PARAMETERS

Expand All @@ -53,24 +52,6 @@ Accept pipeline input: False
Accept wildcard characters: False
```
### -ConfigFile
Path where the file with the configuration will be stored.
> This parameter is not yet implemented
```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
Expand Down

0 comments on commit 6f6cd04

Please sign in to comment.