
Hello! One of my favorite languages is Go, as a result of which I thought about writing something like Package Manager ... Well, or at least a search engine for packages. There is an idea, it's time to get behind the development. Of course, first of all, I thought of Go as a tool for solving a problem. But, after some thought, I decided to give PowerShell a chance to seduce me, for studying which I had already sat 3 times, but something kept stopping me (most likely laziness and lack of projects that could be implemented on it). Well, it is said - done. This article is intended for people who are not familiar with PowerShell, but who have programming experience. If it became interesting to you, welcome under kat.
For a start, it would be nice to know what a PS is. Wikipedia says the following:
WikipediaWindows PowerShell is an open source extensible automation tool from Microsoft, consisting of a shell with a command line interface and a related scripting language.
Now you can get down to business. The first problem that novices will face is a ban on executing third-party scripts. The guys from Microsoft care about our security with you, but we understand what we are doing;). So let's fix this annoying care. Open a PowerShell window that looks like this:
')

And run the following command:
Set-ExecutionPolicy RemoteSigned
The system will ask you if you are aware of what you are doing, answer yes Y.
Generally, PowerShell is based on cmdlets. They have the following syntax:
<Verb> - <Noun>
This is quite convenient, as it introduces an understanding of what should happen. In addition to the cmdlets, there are also Alias for most standard commands, not only CMD but also Bash. For example, you want to clear the screen. The standard cmdlet for this is Clear-Host, but you can also use aliases for it: cls and clear. The list of aliases can be found with the Get-Alias team.
And so, we made the basic PowerShell setup and it's time to write the scripts. The extension of PowerShell scripts is “* .ps1”. Create a file to develop our cmdlets with the command:
New-Item -Path 'C:\work\goPS.ps1' -Type File -Force
If everything is clear with the -Path parameter, you will have to deal with the rest. -Type indicates the type of file being created, since we need a file, we explicitly specify this. -Force creates paths if they do not exist.
The question is: where to get the list of modules for Go? Answer:
Go-Search resource, which provides a very convenient, and most importantly free API, will help us in this. Thank them for that.
You can go to your favorite code editor, but personally I can advise you to use PowerShell ISE, kindly preinstalled in the system. We need to create the framework of our module:
function Find-GoPackage { }
This is our future cmdlet. PS thing is not simple, in contrast to Bash and CMD operates with objects, not rows, which is very convenient when working with a pipeline. The frame is, now we will deal with the parameters. Variables in PS are set like PHP with $ my_var. However, they can be both untyped and typed.
# $no_type # , [string]$have_type
Declare a block of arguments:
[CmdletBinding()] Param ( [string]$Name = "" )
As you can see, it is easy to set the arguments, and there is also a default value.
The logic of the work is as follows. First of all, we need to make a request to the server. This can be done in several ways, but, as for me, the easiest is to use Invoke-WebRequest, which will return the object with the content of the response.
We introduce a variable:
$uri = "http://go-search.org/api?action=search&q=" + $Name
PowerShell supports work with the pipeline. The answer from the server comes in the form of json, but thanks to the kind people who have already thought about us. PS has standard serializers and object deserializers. ConvertFrom-Json takes the string Json and returns a “raw” object with the fields of our json. Enough words, more code, and the code will be small.
$hits = $($(Invoke-WebRequest -Uri $uri).Content | ConvertFrom-Json).hits
We pass our link to the invoker, take the content of the response from the .Content field and pass it along a pipeline (the pipeline is “|”) to ConvertFrom-JSON. We, in turn, are interested in the .hits field, which contains a list of found modules. Only one line did all the work for us!
It remains only to return our list, well, it's already quite simple:
return $hits
I give a full listing:
function Find-GoPackage { [CmdletBinding()] Param ( [string]$Name = "" ) $uri = "http://go-search.org/api?action=search&q=" + $Name $hits = $($(Invoke-WebRequest -Uri $uri).Content | ConvertFrom-Json).hits return $hits }
Now let's go back to PowerShell and import our module into the session:
PS C:\ >. “C:\work\goPS.ps1”
PowerShell analyzed our script and is ready to go.
For example, execute the “Find-GoPackage -Name json” command and get a list of found modules for working with json, but for beauty you can also add formatting:
Find-GoPackage json | Format-Table -Wrap -AutoSize
We have a neat plate at the exit.
True, importing a module every time is not convenient, so you can do one interesting thing: there is a profile system in PS. A profile is a file that is executed every time you open a terminal.
Type in PS the following:
Test-Path $PROFILE
$ PROFILE is an environment variable containing the path to your profile file. If the command above returned $ false, then your profile is not configured. In this case, run the following command:
New-Item -Path $PROFILE -Type File -Force
Open this file:
notepad $PROFILE
And copy our above code into a file, save, restart PowerShell and check that everything works.
That's all, thank you for your attention!