This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup in AzCopyCore Sample (#2123)
* cleaned up logging * improved Key.ComputeKeyBytes * cleanup AzCopyCore sample
- Loading branch information
1 parent
3243d04
commit 77599d4
Showing
11 changed files
with
241 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
samples/AzCopyCore/AzCopyCore/Helpers/ConsoleTraceListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
samples/AzCopyCore/AzCopyCore/Helpers/PipelinesExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.