This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Use AST to pull a test's $Desired value #191
Open
brianbunke
wants to merge
5
commits into
WahlNetwork:master
Choose a base branch
from
brianbunke:ast178
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84a0db2
Update helper docs
brianbunke d88e392
Update changelog
brianbunke 78fa93a
Add private Get-VestConfigValue
brianbunke 26498a9
Use Get-VestConfigValue in New-VesterConfig
brianbunke 22f9fe6
Remove $Matches from Set-VesterConfigValue
brianbunke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,20 @@ | ||
# Contributing to Vester | ||
|
||
Everyone is welcome to contribute to this project. | ||
The goal is to add fine-grained tests that look at specific values within a vSphere environment, compare them to defined configuration value, and optionally remediate discrepancies if the user so decides. | ||
However, there is nothing wrong with submitting a pull request (PR) with a non-remediating test. | ||
This is a great starting point for those newer to coding with PowerShell! | ||
|
||
## Contribution Requirements | ||
|
||
Every test that is added to Vester needs three things: | ||
|
||
1. An update to the example [`Config.ps1`][config] file with your required configuration value(s), comments, and accepted input type. | ||
2. An update to the [`Config.Tests.ps1`][config.tests] file to validate that the `Config.ps1` file contains valid entries. | ||
3. A test file using a properly formatted `Verb-Noun` format (use `Get-Verb` for more details) placed into the Tests folder. | ||
Hello! We're always happy to hear from friends old and new. Here's some basic info about contributing for newcomers. | ||
|
||
## Your First Contribution | ||
|
||
If you're looking for your first bit of code to add, try this list: | ||
If you're a first-timer, welcome! If you're not sure how to GitHub, allow me to humbly recommend a [GitHub 101] guide I wrote to try to help. | ||
|
||
Fixing typos or updating outdated documentation is always welcome. New sets of eyes are a very scarce resource :slightly_smiling_face: | ||
|
||
1. Identify a configuration value in your vSphere environment that isn't being inspected by Vester. | ||
2. Use the [Template][template] to create a test that inspects this value and try it out locally. | ||
3. At this point you can submit a pull request (PR) for a non-remediating test. | ||
If someone else wants the remediation code added, they will grab your code and write that portion. | ||
4. Optionally, write the remediation portion yourself to make a fully remediating test. | ||
If you're submitting anything more substantial, we'd love if you would open an issue with your bug or feature request first, and then describe your pull request as resolving a single issue. | ||
|
||
## Contribution Process | ||
Vester is designed so that Someone Like You™ could [Write Your Own Vester Test]. If you have a small value in your vSphere environment you want to test for, we'd love to add more tests to the suite. | ||
|
||
1. Create a fork of the project into your own repository. | ||
2. From your fork, create a new feature branch (other than master) that expresses your feature or enhancement. | ||
3. Make all your necessary changes in your feature branch. | ||
4. Create a pull request with a description on what was added or removed and details explaining the changes in lines of code. | ||
Questions? [Reach out!] | ||
|
||
If approved, project owners will merge it. | ||
|
||
[config]: https://github.com/WahlNetwork/Vester/blob/master/Configs/Config.ps1 | ||
[config.tests]: https://github.com/WahlNetwork/Vester/blob/master/Configs/Config.Tests.ps1 | ||
[template]: https://github.com/WahlNetwork/Vester/blob/master/Templates/Update-Template.ps1 | ||
[GitHub 101]: http://www.brianbunke.com/blog/2017/05/08/github-101/ | ||
[Write Your Own Vester Test]: http://www.brianbunke.com/blog/2017/03/09/write-your-own-vester-test/ | ||
[Reach out!]: /blob/master/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
function Get-VestConfigValue { | ||
<# | ||
.SYNOPSIS | ||
Extract the $cfg.blah.blah value each Vester test uses. | ||
|
||
.DESCRIPTION | ||
Get-VestConfigValue uses the abstract syntax tree (AST) to parse the test | ||
file, determine the $cfg value assigned to the $Desired variable within, | ||
and output that $cfg value as a string for New-VesterConfig to consume. | ||
|
||
.EXAMPLE | ||
C:\DNS-Address.Vester.ps1 | Get-VestConfigValue | ||
Returns the $cfg.blah.blah value assigned to the $Desired variable. | ||
|
||
.NOTES | ||
Consulted @ThmsRynr's AstHelper & @MathieuBuisson's PSCodeHealth modules. Thanks! | ||
#> | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(ValueFromPipeline = $true)] | ||
$Vest | ||
) | ||
|
||
Write-Verbose "[Get-VestConfigValue] started for $(Split-Path $Vest -Leaf)" | ||
|
||
$parse = [System.Management.Automation.Language.Parser]::ParseFile($Vest, [ref]$null, [ref]$null) | ||
|
||
$ast = $parse.FindAll({$args[0] -is 'System.Management.Automation.Language.AssignmentStatementAst'}, $true) | ||
|
||
$Output = $ast | Where-Object { | ||
$_.Left.Extent.Text -eq '$Desired' -and | ||
$_.Right.Extent.Text -like '$cfg.*' | ||
} | ||
|
||
Write-Debug "$($Output.Count) of $($ast.Count) objects will return. Inspect `$ast and `$Output for details" | ||
|
||
Write-Output $Output.Right.Extent.Text | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad to see this happened. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I'm super happy with how this PR turned out, and how much freaking work I'll be doing with the AST moving forward...I think you found the easter egg I was most excited about. 👌