From ebf234089aa6cb4aa22c4832591a4b34e4bf9340 Mon Sep 17 00:00:00 2001 From: xieguigang Date: Wed, 23 Jan 2019 11:42:18 +0800 Subject: [PATCH] test success --- Docker/Captures/Models.vb | 10 ++++++ Docker/Captures/ParserHelpers.vb | 26 +++++++++++++++ Docker/Commands.vb | 55 +++++++++++++++++++------------- Docker/Docker.vbproj | 1 + Docker/PowerShell.vb | 8 +++++ Docker/test/Module1.vb | 2 ++ 6 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 Docker/Captures/ParserHelpers.vb diff --git a/Docker/Captures/Models.vb b/Docker/Captures/Models.vb index 5081efe..f17629c 100644 --- a/Docker/Captures/Models.vb +++ b/Docker/Captures/Models.vb @@ -7,4 +7,14 @@ Dim OFFICIAL As String Dim AUTOMATED As String End Structure + + Public Structure Container + Dim CONTAINER_ID As String + Dim IMAGE As Image + Dim COMMAND As String + Dim CREATED As String + Dim STATUS As String + Dim PORTS As String + Dim NAMES As String + End Structure End Namespace \ No newline at end of file diff --git a/Docker/Captures/ParserHelpers.vb b/Docker/Captures/ParserHelpers.vb new file mode 100644 index 0000000..2c52329 --- /dev/null +++ b/Docker/Captures/ParserHelpers.vb @@ -0,0 +1,26 @@ +Imports System.Runtime.CompilerServices +Imports Microsoft.VisualBasic.Text +Imports r = System.Text.RegularExpressions.Regex + +Namespace Captures + + Module ParserHelpers + + + Public Iterator Function ParseTable(Of T)(text$, creator As Func(Of String(), T)) As IEnumerable(Of T) + Dim summary$() = text.Trim.LineTokens + Dim header = r.Matches(summary(Scan0), "(\S+\s+)|(\S+)").ToArray + Dim fieldLength%() = header.Select(AddressOf Len).ToArray + + For Each line As String In summary.Skip(1) + Dim tokens$() = FormattedParser _ + .FieldParser(line, fieldLength) _ + .ToArray + + Yield creator(tokens) + Next + End Function + End Module +End Namespace + + diff --git a/Docker/Commands.vb b/Docker/Commands.vb index 0d7530c..2ed6481 100644 --- a/Docker/Commands.vb +++ b/Docker/Commands.vb @@ -1,6 +1,6 @@ -Imports Microsoft.VisualBasic.Text -Imports r = System.Text.RegularExpressions.Regex + +Imports Docker.Captures ''' ''' Docker commands ''' @@ -84,33 +84,42 @@ Public Module Commands ' Run 'docker COMMAND --help' for more information on a command. - ReadOnly ps As New PowerShell + ReadOnly powershell As New PowerShell ''' ''' Search the Docker Hub for images ''' ''' ''' - Public Iterator Function Search(term As String) As IEnumerable(Of Captures.Search) - Dim summary$() = ps _ - .RunScript($"docker search {term}") _ - .Trim _ - .LineTokens - Dim header = r.Matches(summary(Scan0), "(\S+\s+)|(\S+)").ToArray - Dim fieldLength%() = header.Select(AddressOf Len).ToArray - - For Each line As String In summary.Skip(1) - Dim tokens$() = FormattedParser _ - .FieldParser(line, fieldLength) _ - .ToArray + Public Function Search(term As String) As IEnumerable(Of Search) + Return powershell($"docker search {term}") _ + .ParseTable(Function(tokens) + Return New Search With { + .NAME = Image.ParseEntry(tokens(0)), + .DESCRIPTION = tokens(1).Trim, + .STARS = tokens(2).Trim, + .OFFICIAL = tokens(3).Trim, + .AUTOMATED = tokens(4).Trim + } + End Function) + End Function - Yield New Captures.Search With { - .NAME = Image.ParseEntry(tokens(0)), - .DESCRIPTION = tokens(1).Trim, - .STARS = tokens(2).Trim, - .OFFICIAL = tokens(3).Trim, - .AUTOMATED = tokens(4).Trim - } - Next + ''' + ''' List containers + ''' + ''' + Public Function PS() As IEnumerable(Of Container) + Return powershell("docker ps") _ + .ParseTable(Function(tokens) + Return New Container With { + .CONTAINER_ID = tokens(0).Trim, + .IMAGE = Image.ParseEntry(tokens(1)), + .COMMAND = tokens(2).Trim, + .CREATED = tokens(3).Trim, + .STATUS = tokens(4).Trim, + .PORTS = tokens(5), + .NAMES = tokens(6).Trim + } + End Function) End Function End Module diff --git a/Docker/Docker.vbproj b/Docker/Docker.vbproj index 6052c34..674aa36 100644 --- a/Docker/Docker.vbproj +++ b/Docker/Docker.vbproj @@ -94,6 +94,7 @@ + diff --git a/Docker/PowerShell.vb b/Docker/PowerShell.vb index e719002..33fd4bc 100644 --- a/Docker/PowerShell.vb +++ b/Docker/PowerShell.vb @@ -1,6 +1,7 @@ Imports System.Collections.ObjectModel Imports System.Management.Automation Imports System.Management.Automation.Runspaces +Imports System.Runtime.CompilerServices Imports System.Text ''' @@ -8,6 +9,13 @@ Imports System.Text ''' Public Class PowerShell + Default Public ReadOnly Property EVal(command As String) As String + + Get + Return RunScript(scriptText:=command) + End Get + End Property + ''' ''' Takes script text as input and runs it, then converts the results to a string to return to the user ''' diff --git a/Docker/test/Module1.vb b/Docker/test/Module1.vb index 74a8791..47bbcd6 100644 --- a/Docker/test/Module1.vb +++ b/Docker/test/Module1.vb @@ -7,6 +7,8 @@ Module Module1 Call Console.WriteLine(ps.RunScript("docker ps")) + Call Console.WriteLine(Docker.PS.ToArray.GetJson) + Call Console.WriteLine(Docker.Search("centos").ToArray.GetJson) Pause()