NuGet / Home

Repo for NuGet Client issues
Other
1.5k stars 253 forks source link

Add source name to `dotnet nuget list source` short format #9251

Open gitfool opened 4 years ago

gitfool commented 4 years ago

@rrelyea I'm playing with the preview of 3.1.200 SDK and dotnet nuget list source:

$ cat NuGet.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <disabledPackageSources>
    <clear />
  </disabledPackageSources>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

$ dotnet nuget list source --configfile NuGet.config
Registered Sources:
  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json

$ dotnet nuget list source --format detailed --configfile NuGet.config
Registered Sources:
  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json

$ dotnet nuget list source --format short --configfile NuGet.config
E https://api.nuget.org/v3/index.json

My immediate impression is that the short format should also include the source name, in this case nuget.org; i.e. I would expect the short format to be succinct but complete:

$ dotnet nuget list source --format short --configfile NuGet.config
E nuget.org https://api.nuget.org/v3/index.json

This is backed up by the following experience:

$ dotnet nuget add source https://ci.appveyor.com/nuget/gitversion-8nigugxjftrw --configfile NuGet.config
Package source with Name: Package source {0}1 added successfully.

$ dotnet nuget list source --configfile NuGet.config
Registered Sources:
  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json
  2.  Package source {0}1 [Enabled]
      https://ci.appveyor.com/nuget/gitversion-8nigugxjftrw

$ cat NuGet.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <disabledPackageSources>
    <clear />
  </disabledPackageSources>
  <packageSources>
    <clear />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="Package source {0}1" value="https://ci.appveyor.com/nuget/gitversion-8nigugxjftrw" />
  </packageSources>
</configuration>

$ dotnet nuget remove source https://ci.appveyor.com/nuget/gitversion-8nigugxjftrw --configfile NuGet.config
error: Unable to find any package source(s) matching name: https://ci.appveyor.com/nuget/gitversion-8nigugxjftrw.
...

$ dotnet nuget remove source "Package source {0}1" --configfile NuGet.config
Package source with Name: Package source {0}1 removed successfully.

$ dotnet nuget add source https://ci.appveyor.com/nuget/gitversion-8nigugxjftrw --name gitversion --configfile NuGet.config
Package source with Name: gitversion added successfully.

$ dotnet nuget disable source gitversion --configfile NuGet.config
Package source with Name: gitversion disabled successfully.

$ dotnet nuget enable source gitversion --configfile NuGet.config
Package source with Name: gitversion enabled successfully.

$ dotnet nuget update source gitversion --username user --password pass --store-password-in-clear-text --configfile NuGet.config
Package source "gitversion" was successfully updated.

$ dotnet nuget remove source gitversion --configfile NuGet.config
Package source with Name: gitversion removed successfully.

I think the source name is important enough to always be specified, first to avoid the weird default source name, but also because it's required for all other dotnet nuget ... source commands, which are source name centric, as shown above.

FWIW, I'm currently writing a Cake tool "wrapper" for these dotnet nuget ... source commands and a common pattern is to manipulate NuGet sources depending on whether or not a source already exists. A short (single-line) format is better suited to tooling, and given the source name is essential to any such manipulation, I'd argue that it should be included here.

moikot commented 4 years ago

I believe it's a critical issue in the new set of dotnet nuget commands. Many of them require a source name, and there is no way to interrogate current sources and manipulate them (unless you parse the full output of dotnet nugget list source).

I'm trying to perform dotnet restore while building a Docker image, and I want to go through the list of sources and add credentials (PATs) for accessing private feeds. Currently, I can't perform this simple procedure because the full output is not structured, and the short format is missing names.

chenghuang-mdsol commented 2 years ago

For now, you can use this regex to filter out name/url pair from the detailed output (use multiline mode)

^\s*\d*\.\s*(.*) \[(?:Enabled|Disabled)\]$[\r\n]+\s*(.*)
SamHard commented 1 month ago

Until we get official support, if you have access to PowerShell, you could use this function I wrote to help. It borrows the regular expression from the comment above by @chenghuang-mdsol.

Notes:

function Get-NuGetSourceNameByUrl {
    param(
        [string]$sourceUrl,
        [string]$configFile
    )
    $nugetSources = $(dotnet nuget list source --configfile $configFile | Out-String)
    $sourceName = [regex]::Replace($nugetSources, '(\[(?:Enabled|Disabled)\])\r?\n', '$1', "Singleline") `
        -split '\r?\n' `
        | Select-String $sourceUrl `
        | Select-String "[Enabled]" `
        | foreach {[regex]::Replace($_.Line, '^\s*\d+\.\s*(.*) \[(?:Enabled|Disabled)\].*$', '$1')}
    return $sourceName
}