Skip to content

Commit 10a1524

Browse files
authored
Merge pull request #57 from box/accept-tokens
Added new --token feature, fixed collaboration command bugs, fixed pr…
2 parents 27dda21 + a006a0a commit 10a1524

File tree

118 files changed

+398
-284
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+398
-284
lines changed

BoxCLI/BoxCLI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<PackageId>BoxCLI</PackageId>
88
</PropertyGroup>
99
<ItemGroup>
10-
<PackageReference Include="Box.V2.Core" Version="3.1.0" />
10+
<PackageReference Include="Box.V2.Core" Version="3.3.0" />
1111
<PackageReference Include="CsvHelper" Version="2.16.3.0" />
1212
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.0" />
1313
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />

BoxCLI/BoxCLIInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ public class BoxCLIInfo
44
{
55
public const string ProductTitle = "Box CLI";
66

7-
public const string Version = "1.0.2";
7+
public const string Version = "1.1.0";
88
}
99
}

BoxCLI/BoxHome/BoxHomeFiles/BoxEnvironments.cs

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ public BoxEnvironments(string fileName, IBoxHome home)
2525
_boxHome = home;
2626
_boxHomeEnvironmentsFileName = fileName;
2727
}
28-
public bool VerifyBoxConfigFile(string filePath)
28+
public bool VerifyBoxConfigFile(string filePath, bool ignoreFilePathTranslation = false)
2929
{
30-
filePath = GeneralUtilities.TranslatePath(filePath);
30+
if (!ignoreFilePathTranslation)
31+
{
32+
filePath = GeneralUtilities.TranslatePath(filePath);
33+
}
3134
Reporter.WriteInformation($"Looking for file at this path {filePath}");
3235
if (File.Exists(filePath))
3336
{
@@ -71,6 +74,7 @@ public bool VerifyBoxConfigFile(string filePath)
7174
}
7275
else
7376
{
77+
Reporter.WriteError($"Couldn't open config file at this path: {filePath}");
7478
return false;
7579
}
7680
}
@@ -95,28 +99,35 @@ public BoxHomeConfigModel RevalidateExistingConfigFile(string filePath, string p
9599
{
96100
return TranslateConfigFileToEnvironment(filePath, privateKeyPath);
97101
}
98-
public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, string privateKeyPath = "")
102+
public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, string privateKeyPath = "", bool ignoreFilePathTranslation = false)
99103
{
100-
filePath = GeneralUtilities.TranslatePath(filePath);
104+
if (!ignoreFilePathTranslation)
105+
{
106+
filePath = GeneralUtilities.TranslatePath(filePath);
107+
}
101108
var translatedConfig = new BoxHomeConfigModel();
102109
if (File.Exists(filePath))
103110
{
104111
var config = DeserializeBoxConfigFile(filePath);
105112
if (!string.IsNullOrEmpty(privateKeyPath))
106113
{
107-
var potentialPathFromOptions = GeneralUtilities.TranslatePath(privateKeyPath);
108-
if (File.Exists(potentialPathFromOptions))
114+
if (!ignoreFilePathTranslation)
109115
{
110-
translatedConfig.PrivateKeyPath = potentialPathFromOptions;
116+
privateKeyPath = GeneralUtilities.TranslatePath(privateKeyPath);
117+
}
118+
if (File.Exists(privateKeyPath))
119+
{
120+
translatedConfig.PrivateKeyPath = privateKeyPath;
111121
}
112122
else
113123
{
114-
throw new Exception("Couldn't access the private key file from the path provided.");
124+
throw new Exception($"Couldn't access the private key file from the path provided: {privateKeyPath}.");
115125
}
116126
}
117127
else if (!string.IsNullOrEmpty(config.AppSettings.AppAuth.PrivateKey))
118128
{
119129
Reporter.WriteInformation("Detected private key value in config...");
130+
Reporter.WriteInformation("Calculating between in-line private key or separate private key file...");
120131
var pattern = @"^-----BEGIN ENCRYPTED PRIVATE KEY-----\n";
121132
var regex = new Regex(pattern);
122133
if (regex.IsMatch(config.AppSettings.AppAuth.PrivateKey))
@@ -127,11 +138,19 @@ public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, stri
127138
else
128139
{
129140
Reporter.WriteInformation("Attempting to resolve file path for private key.");
130-
var potentialPath = GeneralUtilities.TranslateDependentPath(config.AppSettings.AppAuth.PrivateKey, filePath);
131-
Reporter.WriteInformation($"Found {potentialPath}.");
132-
if (File.Exists(potentialPath))
141+
var privateKeyPathInline = config.AppSettings.AppAuth.PrivateKey;
142+
if (!ignoreFilePathTranslation)
143+
{
144+
privateKeyPathInline = GeneralUtilities.TranslateDependentPath(privateKeyPathInline, filePath);
145+
}
146+
Reporter.WriteInformation($"Path to private key file identified: {privateKeyPathInline}.");
147+
if (File.Exists(privateKeyPathInline))
133148
{
134-
translatedConfig.PrivateKeyPath = potentialPath;
149+
translatedConfig.PrivateKeyPath = privateKeyPathInline;
150+
}
151+
else
152+
{
153+
throw new Exception($"Unable to open private key file at {privateKeyPathInline}");
135154
}
136155
}
137156
}
@@ -145,7 +164,7 @@ public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, stri
145164
}
146165
else
147166
{
148-
Reporter.WriteError("Couldn't open file...");
167+
throw new Exception($"Couldn't open config file at {filePath}");
149168
}
150169
return translatedConfig;
151170
}
@@ -303,7 +322,8 @@ public bool UpdatePrivateKeyPath(string existingName, string newPath)
303322
this.SerializeBoxEnvironmentFile(environments);
304323
return true;
305324
}
306-
public bool UpdateConfigFilePath(string existingName, string newPath, string newPemPath = "")
325+
public bool UpdateConfigFilePath(string existingName, string newPath, string newPemPath = "",
326+
bool ignoreFilePathTranslation = false)
307327
{
308328
var environments = DeserializeBoxEnvironmentFile();
309329
var foundEnv = new BoxHomeConfigModel();
@@ -312,18 +332,24 @@ public bool UpdateConfigFilePath(string existingName, string newPath, string new
312332
{
313333
throw new Exception("Couldn't find that environment");
314334
}
315-
var translatePath = GeneralUtilities.TranslatePath(newPath);
316-
if (this.VerifyBoxConfigFile(translatePath))
335+
if (!ignoreFilePathTranslation)
336+
{
337+
newPath = GeneralUtilities.TranslatePath(newPath);
338+
}
339+
if (this.VerifyBoxConfigFile(newPath))
317340
{
318341
BoxHomeConfigModel env;
319342
if (!string.IsNullOrEmpty(newPemPath))
320343
{
321-
var translatePemPath = GeneralUtilities.TranslatePath(newPemPath);
322-
env = this.TranslateConfigFileToEnvironment(translatePath, translatePemPath);
344+
if (!ignoreFilePathTranslation)
345+
{
346+
newPemPath = GeneralUtilities.TranslatePath(newPemPath);
347+
}
348+
env = this.TranslateConfigFileToEnvironment(newPath, newPemPath, ignoreFilePathTranslation: ignoreFilePathTranslation);
323349
}
324350
else
325351
{
326-
env = this.TranslateConfigFileToEnvironment(translatePath);
352+
env = this.TranslateConfigFileToEnvironment(newPath, ignoreFilePathTranslation: ignoreFilePathTranslation);
327353
}
328354
foundEnv.BoxConfigFilePath = env.BoxConfigFilePath;
329355
foundEnv.ClientId = env.ClientId;
@@ -500,7 +526,7 @@ public bool DeleteEnvironment(string name)
500526
throw new Exception("Couldn't find this environment.");
501527
}
502528
}
503-
public bool UpdateEnvironmentFilePath(string path, string envName)
529+
public bool UpdateEnvironmentFilePath(string path, string envName, bool ignoreFilePathTranslation = false)
504530
{
505531
var environments = DeserializeBoxEnvironmentFile();
506532
var foundEnv = new BoxHomeConfigModel();
@@ -511,7 +537,7 @@ public bool UpdateEnvironmentFilePath(string path, string envName)
511537
}
512538
if (this.VerifyBoxConfigFile(path))
513539
{
514-
var newEnv = this.TranslateConfigFileToEnvironment(path);
540+
var newEnv = this.TranslateConfigFileToEnvironment(path, ignoreFilePathTranslation: ignoreFilePathTranslation);
515541
newEnv.AdminAsUserId = (string.IsNullOrEmpty(foundEnv.AdminAsUserId)) ? "" : foundEnv.AdminAsUserId;
516542
newEnv.DefaultAsUserId = (string.IsNullOrEmpty(foundEnv.DefaultAsUserId)) ? "" : foundEnv.DefaultAsUserId;
517543
newEnv.UseDefaultAsUser = foundEnv.UseDefaultAsUser;

BoxCLI/BoxHome/Models/BoxHomeDefaultSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ public class BoxHomeDefaultSettings
1818
[JsonProperty(PropertyName = "autoSave")]
1919
public bool AutoSave { get; set; } = false;
2020
[JsonProperty(PropertyName = "outputJson")]
21-
public bool OutputJson { get; set; } = false;
21+
public bool OutputJson { get; set; } = true;
2222
}
2323
}

BoxCLI/BoxPlatform/Service/BoxPlatformService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public BoxClient AsUserClient(string asUserId)
3434
}
3535
public BoxClient ClientFromToken(string token)
3636
{
37-
var auth = new OAuthSession(token, "", 3600, "bearer");
38-
return new BoxClient(this.BoxPlatformConfig, auth);
37+
var auth = new OAuthSession(token, "1", 3600, "bearer");
38+
return new BoxClient(new BoxConfig("", "", new Uri("http://localhost")), auth);
3939
}
4040

4141
public async Task<bool> BustCache()

BoxCLI/BoxPlatform/Service/BoxPlatformServiceBuilder.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,15 @@ public void SetIterators()
9595
BoxService.BoxCollectionsIterators = BoxIterators;
9696
}
9797

98-
public IBoxPlatformService Build()
98+
public IBoxPlatformService Build(bool isTokenCall = false)
9999
{
100-
SetConfig();
101-
SetCache();
102-
SetAuthorizedClient();
103-
SetIterators();
100+
if (!isTokenCall)
101+
{
102+
SetConfig();
103+
SetCache();
104+
SetAuthorizedClient();
105+
SetIterators();
106+
}
104107
return BoxService;
105108
}
106109
}

BoxCLI/BoxPlatform/Service/IBoxPlatformServiceBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace BoxCLI.BoxPlatform.Service
44
{
55
public interface IBoxPlatformServiceBuilder
66
{
7-
IBoxPlatformService Build();
7+
IBoxPlatformService Build(bool isTokenCall = false);
88
}
99
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using Microsoft.Extensions.CommandLineUtils;
3+
4+
namespace BoxCLI.CommandUtilities.CommandOptions
5+
{
6+
public static class ProvideTokenOption
7+
{
8+
public static CommandOption ConfigureOption(CommandLineApplication command)
9+
=> command.Option("--token <TOKEN>", "Provide a token to perform this call", CommandOptionType.SingleValue);
10+
}
11+
}

BoxCLI/CommandUtilities/GeneralUtilities.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public static string TranslateDependentPath(string path, string containerPath)
129129
path = path.Substring(match.Length - 1);
130130
}
131131

132+
if (path.StartsWith("/"))
133+
{
134+
return path;
135+
}
136+
132137
if (!path.StartsWith(Path.DirectorySeparatorChar.ToString()) && !path.StartsWith("~") && !path.StartsWith("..") &&
133138
!path.StartsWith($".{Path.DirectorySeparatorChar}") && !winDirectoryRegex.IsMatch(path))
134139
{
@@ -188,6 +193,11 @@ public static string TranslatePath(string path)
188193
path = path.Substring(match.Length - 1);
189194
}
190195

196+
if (path.StartsWith("/"))
197+
{
198+
return path;
199+
}
200+
191201
if (!path.StartsWith(Path.DirectorySeparatorChar.ToString()) && !path.StartsWith("~") && !path.StartsWith("..") &&
192202
!path.StartsWith($".{Path.DirectorySeparatorChar}") && !winDirectoryRegex.IsMatch(path))
193203
{

BoxCLI/CommandUtilities/ProgressBar.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public static void UpdateProgress(string item, int progress, int total)
1313
lock (_sync)
1414
{
1515
Console.CursorLeft = 0;
16-
Console.Write(item + " [" + new string('=', percentage / 2) + "] " + percentage + "%");
16+
var spaces = new string(' ', 100 - percentage);
17+
var equals = new string('=', percentage);
18+
var output = $"{item} [{equals}{spaces}] {percentage}%";
19+
Console.Write(output);
1720
}
1821
}
1922
}

0 commit comments

Comments
 (0)