Skip to content

Commit

Permalink
Added args for switch Url and authentication scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
mminns committed Dec 17, 2018
1 parent 8d63243 commit 9e1c83c
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,4 @@ $RECYCLE.BIN/

# Visual Studio 2014 CTP
**/*.sln.ide
.vs/
14 changes: 8 additions & 6 deletions src/Itofinity.Bitbucket.Cli/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@ public class ClientFactory : IClientFactory<ApiClient>

public Type ClientType => typeof(ApiClient);

public const string CanonicalApiUrl = "https://api.bitbucket.org/";
public const string CanonicalApiUrl = "https://api.bitbucket.org/2.0";

public ApiClient GetClient()
{
return GetClient(null);
return GetClient((string)null, (string)null);
}

public ApiClient GetClient(string securityToken)
public ApiClient GetClient(string auth, string securityToken)
{
return GetClient(CanonicalApiUrl, securityToken);
}

public ApiClient GetClient(string apiUrl, string securityToken)
public ApiClient GetClient(string apiUrl, string scheme, string securityToken)
{
var urlToUse = apiUrl ?? Environment.GetEnvironmentVariable("BITBUCKET_API_URL") ?? CanonicalApiUrl;
var schemeToUse = scheme ?? Environment.GetEnvironmentVariable("BITBUCKET_API_AUTH") ?? "Bearer";
var tokenToUse = securityToken ?? Environment.GetEnvironmentVariable("BITBUCKET_API_TOKEN");

if (string.IsNullOrWhiteSpace(tokenToUse))
Expand All @@ -38,9 +40,9 @@ public ApiClient GetClient(string apiUrl, string securityToken)
// TODO for not continue and fail, possibly fail early here?
}

return GetClient(apiUrl, () =>
return GetClient(urlToUse, () =>
{
return Task.FromResult(new Tuple<string, string>("Basic", tokenToUse));
return Task.FromResult(new Tuple<string, string>(schemeToUse, tokenToUse));
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/Itofinity.Bitbucket.Cli/Itofinity.Bitbucket.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Itofinity.Bitbucket.Cli.Ext\Itofinity.Bitbucket.Cli.Ext.csproj" />
<ProjectReference Include="..\Itofinity.Bitbucket.Refit\Itofinity.Bitbucket.Refit.csproj" />
<ProjectReference Include="..\Itofinity.Bitbucket.Refit\Itofinity.Bitbucket.Refit.csproj" />
<ProjectReference Include="..\Itofinity.Cli.Utils\Itofinity.Refit.Cli.Utils.csproj" />
</ItemGroup>
</Project>
14 changes: 12 additions & 2 deletions src/Itofinity.Bitbucket.Cli/PorcelainCommandFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Itofinity.Bitbucket.Refit.Client;
using Itofinity.Refit.Cli.Utils;
using Itofinity.Refit.Cli.Utils.Commands;
using Itofinity.Refit.Cli.Utils.Options.Command;
using Itofinity.Refit.Cli.Utils.Options.Global;
using Microsoft.Extensions.CommandLineUtils;
using System.Collections.Generic;
Expand Down Expand Up @@ -43,6 +42,17 @@ public void AddPorcelain<ApiClient>(CommandLineApplication app, IClientFactory<A
apiToken.Description,
apiToken.OptionType);

var scheme = new Scheme();
var schemeOption = command.Option(scheme.Template,
scheme.Description,
scheme.OptionType);

var url = new Url();
var urlOption = command.Option(url.Template,
url.Description,
url.OptionType);


var indent = new Indent();
command.Option(indent.Template,
indent.Description,
Expand Down Expand Up @@ -73,7 +83,7 @@ public void AddPorcelain<ApiClient>(CommandLineApplication app, IClientFactory<A
app,
() =>
{
var client = clientFactory.GetClient(apiTokenOption.Value());
var client = clientFactory.GetClient(urlOption.Value(), schemeOption.Value(), apiTokenOption.Value());

var api = GetApi(client, prop);

Expand Down
2 changes: 1 addition & 1 deletion src/Itofinity.Bitbucket.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Itofinity.Bitbucket.Cli": {
"commandName": "Project",
"commandLineArgs": "--help"
"commandLineArgs": "repositories-get -t bW1pbm5zOlllODJ5OVl0V1JNTVBHelBHekRZ -u https://api.bitbucket.org/2.0 -s Basic "
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
Contact: support@bitbucket.org
-->
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>{15F9DCF4-97C0-4B4A-92B7-651FA4C9D5EA}</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="Itofinity.Bitbucket.Refit.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
<PropertyGroup Label="Globals">
<ProjectGuid>{E563B1D4-8996-4D29-B045-46D309ABD843}</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="Itofinity.Bitbucket.Refit.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
18 changes: 17 additions & 1 deletion src/Itofinity.Cli.Utils/CommandLineApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,28 @@ public static int Run(CommandLineApplication command,
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
LogException(ex, 0);
}

return 0;
}

public static void LogException(Exception ex, int level)
{
Console.WriteLine($"{new String('.', level)} {ex.Message}");
if (ex is AggregateException)
{
var aex = ex as AggregateException;
foreach (var aexInnerException in aex.InnerExceptions)
{
LogException(aexInnerException, level+1);
}
} else if (ex.InnerException != null)
{
LogException(ex.InnerException, level+1);
}
}

private static void ConfigureLogging(CommandOption logLocationOption, CommandOption logVerbosityOption)
{
var logLevel = NLog.LogLevel.Off;
Expand Down
4 changes: 2 additions & 2 deletions src/Itofinity.Cli.Utils/IClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public interface IClientFactory<T>

T GetClient();

T GetClient(string securityToken);
T GetClient(string scheme, string securityToken);

T GetClient(string apiUrl, string securityToken);
T GetClient(string apiUrl, string scheme, string securityToken);

T GetClient(string apiUrl, Func<Task<Tuple<string, string>>> getSchemeAndToken);

Expand Down
13 changes: 13 additions & 0 deletions src/Itofinity.Cli.Utils/Options/Global/Scheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Extensions.CommandLineUtils;

namespace Itofinity.Refit.Cli.Utils.Options.Global
{
public class Scheme : AbstractOptionDefinition
{
public override string Template => "-s|--scheme";

public override string Description => "An authentication scheme. Defaults to Bearer";

public override CommandOptionType OptionType => CommandOptionType.SingleValue;
}
}
7 changes: 2 additions & 5 deletions src/Itofinity.Cli.Utils/Options/Global/Token.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.CommandLineUtils;

namespace Itofinity.Refit.Cli.Utils.Options.Command
namespace Itofinity.Refit.Cli.Utils.Options.Global
{
public class Token : AbstractOptionDefinition
{
Expand Down
13 changes: 13 additions & 0 deletions src/Itofinity.Cli.Utils/Options/Global/Url.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Extensions.CommandLineUtils;

namespace Itofinity.Refit.Cli.Utils.Options.Global
{
public class Url : AbstractOptionDefinition
{
public override string Template => "-u|--url";

public override string Description => "Root API Url";

public override CommandOptionType OptionType => CommandOptionType.SingleValue;
}
}

0 comments on commit 9e1c83c

Please sign in to comment.