使用VB.net-从GitLab提取文件 [英] Using VB.net - pull a file from GitLab

查看:128
本文介绍了使用VB.net-从GitLab提取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我正在寻找一种使用VB.net(2017)从私有GitLab存储库中提取特定文件的方法.

As the title implies, I'm looking for a way to pull specific file(s) from a private GitLab repo using VB.net (2017).

我有一个正在编写的应用程序,它将调用某些PowerShell脚本.我还有其他一些用户正在编写脚本,因此我们将GitLab用作这些脚本的存储库.

I have an application that I'm writing which will call certain PowerShell scripts. I have a few other users working with me writing the scripts, so we are using GitLab as the repository for these.

我们希望应用程序在打开时从GitLab中提取脚本的最新版本,然后从应用程序内部调用脚本.

We want the application to pull the latest version of the scripts from GitLab when the application opens, then from within the app, call the scripts.

除了从GitLab下载脚本之外,我已完成所有工作.

I have everything done, with the exception of downloading the scripts from GitLab.

推荐答案

所以我在这里发布答案,以防万一其他人有相同的问题.实际上,我很容易就能做到这一点.

So I'm posting an answer here just in case anyone else has the same question. I was actually able to get this done pretty easily.

首先,您必须生成一个专用令牌.为此,有很多演练,所以我在这里不再赘述.

First, you have to generate a private token. Plenty of walk-throughs for that, so I won't go into that here.

接下来,您必须获取要下载的原始文件的地址.您可以通过在GitLab中打开文件来获取此信息,然后在窗口右上方的"Open Raw"按钮打开原始页面.
在此处查看图片

Next, you have to get the address of raw file that you want to download. You can get this by opening the file in GitLab, then there's a button on the top right of the window to "Open Raw", which opens the raw page.
See Image Here

从地址栏中获取该网址.一旦有了它,就拥有了使用VB.net缩小文件长度所需的所有步骤.

Grab the url from the address bar. Once you have that, you have all the pieces you need to curl the file down using VB.net.

您必须获取原始文件的地址,假设它是" https://gitlab.com/CompanyName/raw/master/folder/filename.ps1 ",然后使用您的私人令牌附加?,如下所示:"

You have to take the address of the raw file, let's say that was "https://gitlab.com/CompanyName/raw/master/folder/filename.ps1", you then append ?, with your private token so it looks like this: "https://gitlab.com/CompanyName/raw/master/folder/filename.ps1?private_token=MyPrivateToken" and use a curl (through powershell) to get it.

我的代码中已经有一个功能可以运行Powershell脚本(使用代码,我相信我已经离开了该网站...忘记了确切的位置),就像这样:

I already had a function in my code to run powershell scripts (with code I believe I got off this site...forgot the exact location), which was like this:

    Private Function RunScript(ByVal scriptText As String) As String
    ' Takes script text as input and runs it, then converts
    ' the results to a string to return to the user
    ' create Powershell runspace
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()

    ' open it
    MyRunSpace.Open()

    ' create a pipeline and feed it the script text
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()

    MyPipeline.Commands.AddScript(scriptText)

    ' add an extra command to transform the script output objects into nicely formatted strings
    ' remove this line to get the actual objects that the script returns. For example, the script
    ' "Get-Process" returns a collection of System.Diagnostics.Process instances.
    MyPipeline.Commands.Add("Out-String")

    ' execute the script
    Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

    ' close the runspace
    MyRunSpace.Close()

    ' convert the script result into a single string
    Dim MyStringBuilder As New StringBuilder()

    For Each obj As PSObject In results
        MyStringBuilder.AppendLine(obj.ToString())
    Next

    ' return the results of the script that has
    ' now been converted to text
    Return MyStringBuilder.ToString()

End Function

现在,我可以使用如下函数调用curl命令:

Now, I can call a curl command with that function like this:

RunScript("curl https://gitlab.com/CompanyName/raw/master/folder/filename.ps1?private_token=MyPrivateToken -outfile C:\DownloadFolder\FileName.ps1")

就是这样!每当需要获取文件时,您都可以简单地获取原始文件的位置并修改函数调用以反映新地址并获取它.

That's it! Anytime you need to get a file, you can simply get the location of the raw file and modify the function call to reflect the new address and grab it.

这篇关于使用VB.net-从GitLab提取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆