Replies: 11 comments 28 replies
-
In fact, the example json for However, the command does handle comments fine, and even handles multi-line strings which are NOT valid json either. |
Beta Was this translation helpful? Give feedback.
-
I feel like you're misunderstanding Crescendo. Crescendo uses the json to power / configure a PowerShell code generator which generates modules ... which don't have any dependencies. I think that's the right way to do native tool wrappers. We don't want a "native tool wrapper" module that has to actually dynamically generate code on the fly or anything like that -- it would be too slow. However, the json configuration document doesn't seem great. I love the idea of using actual PowerShell syntax to write the template instead of json, but it should still just be a template to drive the generator. Maybe we could write the equivalent configuration for apt like this: function Get-InstalledPackage {
[Alias("apt")]
[CmdletBinding()]
param()
Set-Handler @{
Default = @{
StreamOutput = $False
Handler = {
$args[0] | Select-Object -skip 1 | ForEach-Object {
$n, $v, $p, $s = "$_" -split ' '
[PSCustomObject]@{
Name = $n -replace '/now'
Version = $v
Architecture = $p
State = $s.Trim('[]') -split ','
}
}
}
}
}
apt -q list --installed
} |
Beta Was this translation helpful? Give feedback.
-
It's definitely the case that the whole output handler thing needs to be worked over. I wasn't satisfied when I wrote it (the whole multi-line thing, as well as other things), but I'm still trying to figure out a way to improve it. It works best when you can easily convert the output with something like Crescendo does happen to handle some trickier things as well (take a look at the dd wrapper) which may or may not be a problem for you. I chose json for a couple of reasons. One, I rather like the declarative rather than the code approach and more importantly, being able to use a schema when authoring. Currently, I'm not aware of a way to produce annotated schemas for psd1 files (I've thought about how to do this too). my earliest demos actually did create the functions dynamically in the session, ala: PS> invoke-expression ((Import-CommandConfiguration foo.crescendo.json).ToString()) but this has obvious problems. It's also possible that Crescendo can also be used as an accelerator, it would be fairly simple to scaffold a bunch of commands and tweak them slightly post Please note that this is a 0.4.0 version, we're still very early days. |
Beta Was this translation helpful? Give feedback.
-
YAML and TOML have the same json schema support as JSON :) |
Beta Was this translation helpful? Give feedback.
-
A point being brought up in favor of JSON seems to be schema support. While that certainly is a benefit, as a Crescendo file author I feel I would get much more out of not having to fight with quoting, lack of syntax highlighting, lack of IntelliSense, etc. than having a schema. |
Beta Was this translation helpful? Give feedback.
-
Speaking as someone responsible for maintaining CLI tools (and likely one of the people on the hook for "PowerShellizing" tools at Puppet I don't directly own), an implementation leveraging PowerShell classes, functions, or data files with a strongly opinionated snippet and a validation function would be more useful, debuggable, and maintainable for me than writing PowerShell in a JSON string. It's fine if the first (several!) iterations of the tooling are limited in capability and expanded on over time; the maintainability of the definitions is the primary concern for this imo. The fewer levels of interpolation required, the better. |
Beta Was this translation helpful? Give feedback.
-
In my comment on issue 27 I outlined the problems I see with using JSON and also suggested a PowerShell-native approach which likely would be more powerful and readable. See the issue for details but here is what the ipconfig example could look like: $module = New-CrescendoModule -Verb Get -Noun IpConfig `
-OriginalName "c:/windows/system32/ipconfig.exe" `
-Description "This will display the current IP configuration information on Windows"
Add-CrescendoParameter $module -Name All -OriginalName "/all" -ParameterType Switch `
-Description "This switch provides all ip configuration details"
Add-CrescendoParameter $module -Name AllCompartments -OriginalName "/allcompartments" -ParameterType Switch `
-Description "This switch provides compartment configuration details"
Add-CrescendoOutputHandler $module -ParameterSetName Default -Handler {
param ( $lines )
$post = $false;
foreach($line in $lines | ?{$_.trim()}) {
$LineToCheck = $line | select-string '^[a-z]';
if ( $LineToCheck ) {
if ( $post ) { [pscustomobject]$ht |add-member -pass -typename $oName }
$oName = ($LineToCheck -match 'Configuration') ? 'IpConfiguration' : 'EthernetAdapter';
$ht = @{};
$post = $true
}
else {
if ( $line -match '^ [a-z]' ) {
$prop,$value = $line.split(' :',2);
$pName = $prop -replace '[ .-]';
$ht[$pName] = $value.Trim()
}
else {
$ht[$pName] = .{$ht[$pName];$line.trim()}
}
}
}
[pscustomobject]$ht | add-member -pass -typename $oName
}
Export-CrescendoModule $module -ModuleName Ipconfig.psm1 |
Beta Was this translation helpful? Give feedback.
-
This is the first thing that crossed my mind - why json when we can have HashTable like champions? If this is for semi-n00b users, then schema will not help much, and if it is really going to be that complex to need schema, doesn't it defeats the end goal ? Why not supporting HashTable configuration besides the json ? For me at least, writing a PowerShell handler in json is instant turndown. This could ofc. be done externally as converter but it would be more stable to support it OTB. |
Beta Was this translation helpful? Give feedback.
-
While not all commands would have this, it would make sense to at least first use a bash completer, use https://github.com/PowerShell/Modules/tree/master/Modules/Microsoft.PowerShell.UnixCompleters to convert it into a usable C# type model, and then use that model to autogenerate a crescendo json. |
Beta Was this translation helpful? Give feedback.
-
Anything but not JSON, Please. |
Beta Was this translation helpful? Give feedback.
-
I'm hoping to figure out what you see as the advantages of this tool over just having a pre-canned set of functions that someone can dot source. I have some idea as to what I think this really should be which I'll go into below but I first wanted to ask that question to see if I'm missing anything.
Using the ls example here is what I would see as a very basic copy as a script
Anyone who has worked with PowerShell should be able to understand what's happening there and they can easily tweak certain things based on what their desired behaviour is. If we compare that to the json representation we effectively are writing the function as as an object and replicating things like parameter declarations, defaults and so on. This in my mind is just duplication for little gain and you've now added these disadvantages:
"
and\
making it less of a 1 for 1 representationLooking at the example for apt in the blog post we can see the output handler is the following
De json-ifying that it now becomes
To me that's quite ugly and honestly going to be hard to maintain going forward. Compare that to a psd1 file
While I still think this is too nested and the benefit would be more suited towards writing scripts at least the handler code is IMO more readable than what was in the json. I see the 2nd example has a multiline json string and it does seem like PowerShell can parse it but it doesn't seem to conform to the json standard that most tools work with. This makes it harder for external tools to generate these files in a human readable fashion.
What I personally think would be a better way is to make
Crescendo
a collection of scripts that wrap native tools with some boilerplate functions that handle all the native calls and handling of errors. An example of what I had in mind using ls as an example would beObviously this is very bare bones and not entirely thought out but I think it's definitely possible to to leave a common function structure that Crescendo can parse and automatically invoke for you while still giving users the flexibility to go against the grain if need be as well as use the existing syntax and tooling available to PowerShell today.
Beta Was this translation helpful? Give feedback.
All reactions