Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
cleanup AzCopyCore sample
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina committed Feb 14, 2018
1 parent d0a1e7f commit bfd1b14
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 188 deletions.
17 changes: 1 addition & 16 deletions corefxlab.sln
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Azure.Experimental",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.IO.Pipelines.Extensions.Tests", "tests\System.IO.Pipelines.Extensions.Tests\System.IO.Pipelines.Extensions.Tests.csproj", "{64C08774-982C-4141-8F8D-2884B6FA0E4B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AzCopyCore", "samples\AzCopyCore\AzCopyCore\AzCopyCore.csproj", "{A96B401D-2A16-4633-8105-3E8999079414}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -922,18 +920,6 @@ Global
{64C08774-982C-4141-8F8D-2884B6FA0E4B}.Release|x64.Build.0 = Release|Any CPU
{64C08774-982C-4141-8F8D-2884B6FA0E4B}.Release|x86.ActiveCfg = Release|Any CPU
{64C08774-982C-4141-8F8D-2884B6FA0E4B}.Release|x86.Build.0 = Release|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Debug|x64.ActiveCfg = Debug|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Debug|x64.Build.0 = Debug|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Debug|x86.ActiveCfg = Debug|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Debug|x86.Build.0 = Debug|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Release|Any CPU.Build.0 = Release|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Release|x64.ActiveCfg = Release|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Release|x64.Build.0 = Release|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Release|x86.ActiveCfg = Release|Any CPU
{A96B401D-2A16-4633-8105-3E8999079414}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1003,9 +989,8 @@ Global
{0BA3C0A9-64B9-445D-B085-8FFAC09D6E37} = {3079E458-D0E6-4F99-8CAB-80011D35C7DA}
{916370AB-B0D3-4136-850B-AA12FAB23ECD} = {4B000021-5278-4F2A-B734-DE49F55D4024}
{64C08774-982C-4141-8F8D-2884B6FA0E4B} = {3079E458-D0E6-4F99-8CAB-80011D35C7DA}
{A96B401D-2A16-4633-8105-3E8999079414} = {E1DE693E-C1FB-4A1F-B6D8-A071D86D7354}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9DD4022C-A010-4A9B-BCC5-171566D4CB17}
EndGlobalSection
EndGlobal
EndGlobal
45 changes: 45 additions & 0 deletions samples/AzCopyCore/AzCopyCore/Helpers/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.CommandLine
{
// TODO (pri 3): Should I use the command line library?
class CommandLine
{
readonly string[] _options;

public CommandLine(string[] options)
{
_options = options;
}

public bool Contains(string optionName)
{
for (int i = 0; i < _options.Length; i++)
{
var candidate = _options[i];
if (candidate.StartsWith(optionName)) return true;
}
return false;
}

public ReadOnlySpan<char> this[string optionName] => Get(optionName).Span;

public ReadOnlyMemory<char> Get(string optionName)
{
if (optionName.Length < 1) throw new ArgumentOutOfRangeException(nameof(optionName));

for (int i = 0; i < _options.Length; i++)
{
var candidate = _options[i];
if (candidate.StartsWith(optionName))
{
var option = candidate.AsReadOnlyMemory();
return option.Slice(optionName.Length);
}
}
return ReadOnlyMemory<char>.Empty;
}
}
}
67 changes: 67 additions & 0 deletions samples/AzCopyCore/AzCopyCore/Helpers/ConsoleTraceListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Diagnostics
{
class ConsoleTraceListener : TraceListener
{
public override void Write(string message)
=> Console.Write(message);

public override void WriteLine(string message)
=> Console.WriteLine(message);

public override void Fail(string message)
{
base.Fail(message);
}
public override void Fail(string message, string detailMessage)
{
base.Fail(message, detailMessage);
}
public override bool IsThreadSafe => false;

public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
{
ConsoleColor color = default;
if (eventType == TraceEventType.Error || eventType == TraceEventType.Critical)
{
color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
}

Console.WriteLine(eventType.ToString());

if (eventType == TraceEventType.Error || eventType == TraceEventType.Critical)
{
Console.ForegroundColor = color;
}

foreach (var item in data)
{
Console.Write("\t");
Console.WriteLine(data);
}
}

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
ConsoleColor color = default;
if (eventType == TraceEventType.Error || eventType == TraceEventType.Critical)
{
color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
}

Console.WriteLine(format, args);

if (eventType == TraceEventType.Error || eventType == TraceEventType.Critical)
{
Console.ForegroundColor = color;
}
}
}
}


63 changes: 63 additions & 0 deletions samples/AzCopyCore/AzCopyCore/Helpers/PipelinesExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Threading.Tasks;

namespace System.IO.Pipelines
{
// TODO (pri 3): Would be nice to add to the platform (but NetStandard does not support the stream APIs)
static class PipelinesExtensions
{
/// <summary>
/// Copies bytes from ReadOnlySequence to a Stream
/// </summary>
public static async Task WriteAsync(this Stream stream, ReadOnlySequence<byte> buffer)
{
for (var position = buffer.Start; buffer.TryGet(ref position, out var memory);)
{
await stream.WriteAsync(memory).ConfigureAwait(false);
}
}

/// <summary>
/// Copies bytes from PipeReader to a Stream
/// </summary>
public static async Task WriteAsync(this Stream stream, PipeReader reader, ulong bytes)
{
while (bytes > 0)
{
var result = await reader.ReadAsync();
ReadOnlySequence<byte> bodyBuffer = result.Buffer;
if (bytes < (ulong)bodyBuffer.Length)
{
throw new NotImplementedException();
}
bytes -= (ulong)bodyBuffer.Length;
await stream.WriteAsync(bodyBuffer).ConfigureAwait(false);
await stream.FlushAsync().ConfigureAwait(false);
reader.AdvanceTo(bodyBuffer.End);
}
}

/// <summary>
/// Copies bytes from Stream to PipeWriter
/// </summary>
public static async Task WriteAsync(this PipeWriter writer, Stream stream)
{
if (!stream.CanRead) throw new ArgumentException("Stream.CanRead returned false", nameof(stream));
while (true)
{
var buffer = writer.GetMemory();
if (buffer.Length == 0) throw new NotSupportedException("PipeWriter.GetMemory returned an empty buffer.");
int read = await stream.ReadAsync(buffer).ConfigureAwait(false);
if (read == 0) return;
writer.Advance(read);
await writer.FlushAsync();
}
}
}
}


26 changes: 14 additions & 12 deletions samples/AzCopyCore/AzCopyCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
using System;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Azure.Authentication;
using System.Azure.Storage;
using System.Azure.Storage.Requests;
using System.Buffers;
using System.CommandLine;
using System.Diagnostics;
using System.IO;
using System.IO.Pipelines;
using System.Threading.Tasks;

static class Program
Expand All @@ -24,7 +30,7 @@ static void Main(string[] args)
Log.Listeners.Add(new ConsoleTraceListener());
Log.Switch.Level = SourceLevels.Error;

var options = new CommandOptions(args);
var options = new CommandLine(args);
ReadOnlyMemory<char> source = options.Get("/Source:");
ReadOnlyMemory<char> destination = options.Get("/Dest:");

Expand All @@ -47,7 +53,7 @@ static void Main(string[] args)
}
}

static void TransferDirectoryToStorage(ReadOnlyMemory<char> localDirectory, ReadOnlyMemory<char> storageDirectory, CommandOptions options)
static void TransferDirectoryToStorage(ReadOnlyMemory<char> localDirectory, ReadOnlyMemory<char> storageDirectory, CommandLine options)
{
var directoryPath = new string(localDirectory.Span);
if (!Directory.Exists(directoryPath))
Expand All @@ -61,15 +67,13 @@ static void TransferDirectoryToStorage(ReadOnlyMemory<char> localDirectory, Read
return;
}

ReadOnlyMemory<char> key = options.Get("/DestKey:");
byte[] keyBytes = key.Span.ComputeKeyBytes();

ReadOnlyMemory<char> storageFullPath = storageDirectory.Slice(7);
ReadOnlyMemory<char> storageFullPath = storageDirectory.Slice("http://".Length);
int pathStart = storageFullPath.Span.IndexOf('/');
ReadOnlyMemory<char> host = storageFullPath.Slice(0, pathStart);
ReadOnlyMemory<char> path = storageFullPath.Slice(pathStart + 1);
ReadOnlyMemory<char> account = storageFullPath.Slice(0, storageFullPath.Span.IndexOf('.'));

byte[] keyBytes = options["/DestKey:"].ComputeKeyBytes();
using (var client = new StorageClient(keyBytes, account, host))
{
client.Log = Log;
Expand All @@ -87,7 +91,7 @@ static void TransferDirectoryToStorage(ReadOnlyMemory<char> localDirectory, Read
}
}

static void TransferStorageFileToDirectory(ReadOnlyMemory<char> storageFile, ReadOnlyMemory<char> localDirectory, CommandOptions options)
static void TransferStorageFileToDirectory(ReadOnlyMemory<char> storageFile, ReadOnlyMemory<char> localDirectory, CommandLine options)
{
var directory = new string(localDirectory.Span);
if (!options.Contains("/SourceKey:"))
Expand All @@ -96,10 +100,7 @@ static void TransferStorageFileToDirectory(ReadOnlyMemory<char> storageFile, Rea
return;
}

ReadOnlyMemory<char> key = options.Get("/SourceKey:");
byte[] keyBytes = key.Span.ComputeKeyBytes();

ReadOnlyMemory<char> storageFullPath = storageFile.Slice(7);
ReadOnlyMemory<char> storageFullPath = storageFile.Slice("http://".Length);
int pathStart = storageFullPath.Span.IndexOf('/');
ReadOnlyMemory<char> host = storageFullPath.Slice(0, pathStart);
ReadOnlyMemory<char> storagePath = storageFullPath.Slice(pathStart + 1);
Expand All @@ -112,6 +113,7 @@ static void TransferStorageFileToDirectory(ReadOnlyMemory<char> storageFile, Rea
Directory.CreateDirectory(directory);
}

byte[] keyBytes = options["/SourceKey:"].ComputeKeyBytes();
string destinationPath = directory + "\\" + new string(file.Span);
using (var client = new StorageClient(keyBytes, account, host))
{
Expand Down
6 changes: 5 additions & 1 deletion samples/AzCopyCore/AzCopyCore/SocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Buffers;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.IO.Pipelines;
Expand Down
6 changes: 5 additions & 1 deletion samples/AzCopyCore/AzCopyCore/StorageClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Azure.Authentication;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Azure.Authentication;
using System.Azure.Storage.Requests;
using System.Buffers.Cryptography;
using System.Buffers.Text;
Expand Down
7 changes: 5 additions & 2 deletions samples/AzCopyCore/AzCopyCore/StorageRequests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Azure.Authentication;
using System.Buffers;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Azure.Authentication;
using System.Buffers.Text;
using System.Buffers.Transformations;
using System.IO;
Expand Down
Loading

0 comments on commit bfd1b14

Please sign in to comment.