Search packages Go on PowerShell

Hello! One of my favorite languages is a Go, with the result that I was thinking about writing something like the Package Manager... or at least the search packages. There is an idea, it's time to get behind the development. Of course the first thing I thought of Go as a tool for solving problems. But, after a little thought, I decided to give a chance inviting me PowerShell, for which I got already 3 times, but something always stopped me (probably laziness and lack of projects that could be on it to implement). Well, no sooner said than done. This article is intended for people who are not familiar with PowerShell, but have experience in programming. If you become interested, you are welcome under the cat.
For a start it would be nice to know what PS. Wikipedia says the following:
Wikipedia
Windows PowerShell is an extensible automation tool from Microsoft and open source, consisting of a shell with command line interface and accompanying scripting language.
You can now get down to business. The first problem faced by beginners is the prohibition of the execution of third party scripts. The guys from Microsoft care about our security, but we understand what we are doing ;). So let's fix this annoying concern. Open a PowerShell window that looks something like:

And run the following command:
the
Set-ExecutionPolicy RemoteSigned
The system will ask you if You realize what you're doing, the answer is affirmative Y.
All are based PowerShell cmdlets. They have the following syntax:
<Verb>-<Noun>
This is quite convenient as it introduces the understanding of what needs to happen. In addition to cmdlets, there are also Alias's for most standard commands, Pritchett not only CMD but also Bash. For example, You want to clear the screen. The standard for this cmdlet Clear-Host, but you can also use aliases for him: cls and clear. The list of aliases can be found with the command Get-Alias.
And so, we made a basic setup PowerShell and it's time for scripting. The extension of PowerShell scripts is “*.ps1”. Create a file to develop our cmdlets with the command:
the
New-Item -Path 'C:\work\goPS.ps1' -Type File -Force
If-Path is all clear, then the rest will have to deal. -Type specifies the type of file created, as we need the file we explicitly specify it. -Force creates the path if they do not exist.
Question: where to get the list of modules to Go? Answer: we will help resource Go Search, which provides a very easy and free API. I thank them for that.
You can go to your favorite code editor, but personally I can advise You to use PowerShell ISE, kindly pre-installed in the system. You need to create the skeleton of our module:
the
function Find-GoPackage {
}
This is our next cmdlet. PS is not that easy, in contrast to the Bash and CMD operates on objects and not strings, which is very convenient when working with the conveyor. The frame is there, now for the parameters. The variables in PS are set similarly to the PHP using $my_var. They can be both untyped and typed.
the
# is an untyped
$no_type
# Typed, line
[string]$have_type
Declare a block argument:
the
[CmdletBinding()]
Param (
[string]$Name = ""
)
As you can see, ask a simple argument, and the default value is.
The logic works as follows. In particular, we need to make a request to the server. This can be done in several ways, but, for me, the easiest is to use Invoke-WebRequest, which will return the object with the contents of the response.
Let's introduce a variable:
the
$uri = "http://go-search.org/api?action=search&q=" + $Name
PowerShell supports the conveyor. The response from the server comes in the form of json, but thanks to the kind people who have thought about us. In PS there is a standard serializers and deserializers features. ConvertFrom-Json takes a Json string and returns the “raw” object with fields in our json. Enough words, more code, and the code will not be enough.
the
$hits = $($(Invoke-WebRequest -Uri $uri).Content | ConvertFrom-Json).hits
We pass our link to Invoker, take out the contents of the response from the field .Content and pass through the pipeline (the pipeline is “|”) to ConvertFrom-JSON. We, in turn, interested in the field .hits, which contains a list of found modules. Only one line did all the work for us!
You only have to return our list, well that's just:
the
return $hits
Here is the full listing:
the
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 again go to PowerShell and import the module into your session:
the
PS C:\ >. “C:\work\goPS.ps1”
PowerShell reviewed our script and ready to go.
For example, execute the command “Find-GoPackage -Name json” and get a list of found modules to work with json, but for beauty you can also add formatting:
the
Find-GoPackage json | Format-Table -Wrap-AutoSize
Have to exit neat the plate.
True to import each time the module is not convenient, so you can do one interesting thing: in PS there is a system of profiles. A profile is a file that is executed every time you open the terminal.
Enter the following in the PS:
the
Test-Path $PROFILE
$PROFILE is an environment variable containing the path to your profile file. If the above command returned $false, it means that your profile is not configured. In this case, run the following command:
the
New-Item-Path $PROFILE -Type File -Force
Open this file:
the
notepad $PROFILE
And our copy the above code into the file, save, restart PowerShell and check that everything works.
That's all, thank you for your attention!
Комментарии
Отправить комментарий