Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sources to be specified as Environment Variables #4271

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Paket.Core/Versioning/PackageSources.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,21 @@ type PackageSource =
static member Parse(line : string) =
let sourceRegex = Regex("source[ ]*[\"]([^\"]*)[\"]", RegexOptions.IgnoreCase)
let parts = line.Split ' '

let source =
if sourceRegex.IsMatch line then
sourceRegex.Match(line).Groups.[1].Value.TrimEnd([| '/' |])
else
parts.[1].Replace("\"","").TrimEnd([| '/' |])

let feed = normalizeFeedUrl source
PackageSource.Parse(feed, parseAuth(line, feed))
match EnvironmentVariable.Create(source) with
| None ->
let feed = normalizeFeedUrl source
PackageSource.Parse(feed, parseAuth(line, feed))
| Some var ->
PackageSource.Parse ("source " + var.Value)

static member Parse(source,auth) =
static member Parse(source, auth) =
match tryParseWindowsStyleNetworkPath source with
| Some path -> PackageSource.Parse(path)
| _ ->
Expand Down
41 changes: 39 additions & 2 deletions tests/Paket.Tests/DependenciesFile/ParserSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ nuget Rx-Main
let ``should read config with password in env variable``() =
Environment.SetEnvironmentVariable("FEED_USERNAME", "user XYZ", EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable("FEED_PASSWORD", "pw Love", EnvironmentVariableTarget.Process)
let cfg = DependenciesFile.FromSource( configWithPasswordInEnvVariable)
let cfg = DependenciesFile.FromSource(configWithPasswordInEnvVariable)

cfg.Groups.[Constants.MainDependencyGroup].Sources
|> shouldEqual [
Expand All @@ -769,7 +769,7 @@ nuget Rx-Main
let ``should read config with password in env variable and auth type specified``() =
Environment.SetEnvironmentVariable("FEED_USERNAME", "user XYZ", EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable("FEED_PASSWORD", "pw Love", EnvironmentVariableTarget.Process)
let cfg = DependenciesFile.FromSource( configWithPasswordInEnvVariableAndAuthType)
let cfg = DependenciesFile.FromSource(configWithPasswordInEnvVariableAndAuthType)

cfg.Groups.[Constants.MainDependencyGroup].Sources
|> shouldEqual [
Expand All @@ -779,6 +779,43 @@ let ``should read config with password in env variable and auth type specified``
cfg.Groups.[Constants.MainDependencyGroup].Sources.Head.Auth.Retrieve true
|> shouldEqual (Some (Credentials{ Username = "user XYZ"; Password = "pw Love"; Type = NetUtils.AuthType.NTLM}))

let configWithSourceInEnvVariable = """
source %FEED_URL%
nuget Rx-Main
"""

[<Test>]
let ``should read config with source in env variable``() =
Environment.SetEnvironmentVariable("FEED_URL", "http://www.nuget.org/api/v2", EnvironmentVariableTarget.Process)
let cfg = DependenciesFile.FromSource(configWithSourceInEnvVariable)

cfg.Groups.[Constants.MainDependencyGroup].Sources
|> shouldEqual [
PackageSource.NuGetV2 {
Url = "http://www.nuget.org/api/v2"
Authentication = AuthProvider.empty} ]


let configWithSourceAndCredentialsInEnvVariable = """
source %FEED_URL%
nuget Rx-Main
"""

[<Test>]
let ``should read config with source and credentials in env variable``() =
Environment.SetEnvironmentVariable("FEED_USERNAME", "user XYZ", EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable("FEED_PASSWORD", "pw Love", EnvironmentVariableTarget.Process)
Environment.SetEnvironmentVariable("FEED_URL", "http://www.nuget.org/api/v2 username: \"%FEED_USERNAME%\" password: \"%FEED_PASSWORD%\" authtype: \"nTlM\"", EnvironmentVariableTarget.Process)
let cfg = DependenciesFile.FromSource(configWithSourceAndCredentialsInEnvVariable)

cfg.Groups.[Constants.MainDependencyGroup].Sources
|> shouldEqual [
PackageSource.NuGetV2 {
Url = "http://www.nuget.org/api/v2"
Authentication = AuthProvider.empty} ]
cfg.Groups.[Constants.MainDependencyGroup].Sources.Head.Auth.Retrieve true
|> shouldEqual (Some (Credentials{ Username = "user XYZ"; Password = "pw Love"; Type = NetUtils.AuthType.NTLM}))

let configWithExplicitVersions = """
source "http://www.nuget.org/api/v2"

Expand Down