Skip to content

Commit

Permalink
Product, config, environment model updates, paging in selection prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Oct 16, 2021
1 parent eb680a1 commit 4787fb6
Show file tree
Hide file tree
Showing 20 changed files with 537 additions and 309 deletions.
2 changes: 2 additions & 0 deletions src/ConfigCat.Cli.Models/Api/ConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public class ConfigModel
public string ConfigId { get; set; }

public string Name { get; set; }

public string Description { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/ConfigCat.Cli.Models/Api/EnvironmentModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ public class EnvironmentModel
public string EnvironmentId { get; set; }

public string Name { get; set; }

public string Color { get; set; }

public string Description { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/ConfigCat.Cli.Models/Api/FlagModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public UpdateFlagModel ToUpdateModel() =>
{
Name = this.Name,
Hint = this.Hint,
TagIds = this.Tags.Select(t => t.TagId)
TagIds = this.Tags?.Select(t => t.TagId)
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/ConfigCat.Cli.Models/Api/ProductModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public class ProductModel
public string ProductId { get; set; }

public string Name { get; set; }

public string Description { get; set; }
}
}
12 changes: 6 additions & 6 deletions src/ConfigCat.Cli.Services/Api/ConfigClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public interface IConfigClient
{
Task<IEnumerable<ConfigModel>> GetConfigsAsync(string productId, CancellationToken token);

Task<ConfigModel> CreateConfigAsync(string productId, string name, CancellationToken token);
Task<ConfigModel> CreateConfigAsync(string productId, string name, string description, CancellationToken token);

Task<ConfigModel> GetConfigAsync(string configId, CancellationToken token);

Task UpdateConfigAsync(string configId, string name, CancellationToken token);
Task UpdateConfigAsync(string configId, string name, string description, CancellationToken token);

Task DeleteConfigAsync(string configId, CancellationToken token);
}
Expand All @@ -34,8 +34,8 @@ public ConfigClient(IOutput output,
public Task<IEnumerable<ConfigModel>> GetConfigsAsync(string productId, CancellationToken token) =>
this.GetAsync<IEnumerable<ConfigModel>>(HttpMethod.Get, $"v1/products/{productId}/configs", token);

public Task<ConfigModel> CreateConfigAsync(string productId, string name, CancellationToken token) =>
this.SendAsync<ConfigModel>(HttpMethod.Post, $"v1/products/{productId}/configs", new { Name = name }, token);
public Task<ConfigModel> CreateConfigAsync(string productId, string name, string description, CancellationToken token) =>
this.SendAsync<ConfigModel>(HttpMethod.Post, $"v1/products/{productId}/configs", new { Name = name, Description = description }, token);

public Task<ConfigModel> GetConfigAsync(string configId, CancellationToken token) =>
this.GetAsync<ConfigModel>(HttpMethod.Get, $"v1/configs/{configId}", token);
Expand All @@ -48,10 +48,10 @@ public async Task DeleteConfigAsync(string configId, CancellationToken token)
this.Output.WriteLine();
}

public async Task UpdateConfigAsync(string configId, string name, CancellationToken token)
public async Task UpdateConfigAsync(string configId, string name, string description, CancellationToken token)
{
this.Output.Write($"Updating Config... ");
await this.SendAsync(HttpMethod.Put, $"v1/configs/{configId}", new { Name = name }, token);
await this.SendAsync(HttpMethod.Put, $"v1/configs/{configId}", new { Name = name, Description = description }, token);
this.Output.WriteSuccess();
this.Output.WriteLine();
}
Expand Down
12 changes: 6 additions & 6 deletions src/ConfigCat.Cli.Services/Api/EnvironmentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public interface IEnvironmentClient

Task<EnvironmentModel> GetEnvironmentAsync(string environmentId, CancellationToken token);

Task<EnvironmentModel> CreateEnvironmentAsync(string productId, string name, CancellationToken token);
Task<EnvironmentModel> CreateEnvironmentAsync(string productId, string name, string description, string color, CancellationToken token);

Task UpdateEnvironmentAsync(string environmentId, string name, CancellationToken token);
Task UpdateEnvironmentAsync(string environmentId, string name, string description, string color, CancellationToken token);

Task DeleteEnvironmentAsync(string environmentId, CancellationToken token);
}
Expand All @@ -37,8 +37,8 @@ public Task<IEnumerable<EnvironmentModel>> GetEnvironmentsAsync(string productId
public Task<EnvironmentModel> GetEnvironmentAsync(string environmentId, CancellationToken token) =>
this.GetAsync<EnvironmentModel>(HttpMethod.Get, $"v1/environments/{environmentId}", token);

public Task<EnvironmentModel> CreateEnvironmentAsync(string productId, string name, CancellationToken token) =>
this.SendAsync<EnvironmentModel>(HttpMethod.Post, $"v1/products/{productId}/environments", new { Name = name }, token);
public Task<EnvironmentModel> CreateEnvironmentAsync(string productId, string name, string description, string color, CancellationToken token) =>
this.SendAsync<EnvironmentModel>(HttpMethod.Post, $"v1/products/{productId}/environments", new { Name = name, Description = description, Color = color }, token);

public async Task DeleteEnvironmentAsync(string environmentId, CancellationToken token)
{
Expand All @@ -48,10 +48,10 @@ public async Task DeleteEnvironmentAsync(string environmentId, CancellationToken
this.Output.WriteLine();
}

public async Task UpdateEnvironmentAsync(string environmentId, string name, CancellationToken token)
public async Task UpdateEnvironmentAsync(string environmentId, string name, string description, string color, CancellationToken token)
{
this.Output.Write($"Updating Environment... ");
await this.SendAsync(HttpMethod.Put, $"v1/environments/{environmentId}", new { Name = name }, token);
await this.SendAsync(HttpMethod.Put, $"v1/environments/{environmentId}", new { Name = name, Description = description, Color = color }, token);
this.Output.WriteSuccess();
this.Output.WriteLine();
}
Expand Down
12 changes: 6 additions & 6 deletions src/ConfigCat.Cli.Services/Api/ProductClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public interface IProductClient

Task<ProductModel> GetProductAsync(string productId, CancellationToken token);

Task<ProductModel> CreateProductAsync(string organizationId, string name, CancellationToken token);
Task<ProductModel> CreateProductAsync(string organizationId, string name, string description, CancellationToken token);

Task UpdateProductAsync(string productId, string name, CancellationToken token);
Task UpdateProductAsync(string productId, string name, string description, CancellationToken token);

Task DeleteProductAsync(string productId, CancellationToken token);
}
Expand All @@ -37,8 +37,8 @@ public Task<IEnumerable<ProductModel>> GetProductsAsync(CancellationToken token)
public Task<ProductModel> GetProductAsync(string productId, CancellationToken token) =>
this.GetAsync<ProductModel>(HttpMethod.Get, $"v1/products/{productId}", token);

public Task<ProductModel> CreateProductAsync(string organizationId, string name, CancellationToken token) =>
this.SendAsync<ProductModel>(HttpMethod.Post, $"v1/organizations/{organizationId}/products", new { Name = name }, token);
public Task<ProductModel> CreateProductAsync(string organizationId, string name, string description, CancellationToken token) =>
this.SendAsync<ProductModel>(HttpMethod.Post, $"v1/organizations/{organizationId}/products", new { Name = name, Description = description }, token);

public async Task DeleteProductAsync(string productId, CancellationToken token)
{
Expand All @@ -48,10 +48,10 @@ public async Task DeleteProductAsync(string productId, CancellationToken token)
this.Output.WriteLine();
}

public async Task UpdateProductAsync(string productId, string name, CancellationToken token)
public async Task UpdateProductAsync(string productId, string name, string description, CancellationToken token)
{
this.Output.Write($"Updating Product... ");
await this.SendAsync(HttpMethod.Put, $"v1/products/{productId}", new { Name = name }, token);
await this.SendAsync(HttpMethod.Put, $"v1/products/{productId}", new { Name = name, Description = description }, token);
this.Output.WriteSuccess();
this.Output.WriteLine();
}
Expand Down
63 changes: 63 additions & 0 deletions src/ConfigCat.Cli.Services/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Linq;

namespace System.Collections.Generic
{
public static class ListExtensions
{
public static List<List<T>> Split<T>(this List<T> source, int chunkSize)
{
if (source is null || source.Count == 0)
return new List<List<T>>();

if (chunkSize > source.Count || chunkSize == 0)
return new List<List<T>> { source };

var whole = source.Count / chunkSize;
var remaining = source.Count % chunkSize;

var from = 0;
var splittedSize = remaining == 0 ? whole : whole + 1;
var splitted = new List<List<T>>(splittedSize);
for (int i = splittedSize; i-- > 0;)
{
if (remaining != 0 && i == 0)
chunkSize = remaining;

splitted.Add(new List<T>(source.GetRange(from, chunkSize)));
from += chunkSize;
}

return splitted;
}

public static List<List<T>> SplitFilled<T>(this List<T> source, int chunkSize)
{
if (source is null || source.Count == 0)
return new List<List<T>>();

if (chunkSize > source.Count || chunkSize == 0)
return new List<List<T>> { source };

var splitted = source.Split(chunkSize);
var last = splitted[splitted.Count - 1];
if(last.Count < chunkSize)
last.AddRange(Enumerable.Repeat<T>(default, chunkSize - last.Count));

return splitted;
}

public static int PageIndexOf<T>(this List<List<T>> source, T item)
{
if (source is null || source.Count == 0)
return -1;

for (int i = 0; i < source.Count; i++)
{
if (source[i].IndexOf(item) != -1)
return i;
}

return -1;
}
}
}
12 changes: 7 additions & 5 deletions src/ConfigCat.Cli.Services/Git/GitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ConfigCat.Cli.Services.Rendering;
using System;
using System.Collections.Generic;
using System.CommandLine.Rendering;
using System.Diagnostics;
using System.Text.RegularExpressions;

Expand All @@ -23,16 +24,17 @@ public GitClient(IOutput output)

public GitRepositoryInfo GatherGitInfo(string path)
{
this.output.WriteLine($"Collecting Git repository information from {path}");
this.output.Write("Collecting Git repository information from ")
.WriteColored(path, ForegroundColorSpan.LightCyan())
.WriteLine();

try
{
return this.CollectInfoFromCLI(path);
}
catch
{
this.output.WriteYellow("Could not execute the Git CLI, it's probably not installed. Skipping.");
this.output.WriteLine();
this.output.WriteYellow("Could not execute the Git CLI, it's probably not installed. Skipping.").WriteLine();
return null;
}
}
Expand All @@ -44,11 +46,11 @@ private GitRepositoryInfo CollectInfoFromCLI(string path)
var repoWorkingDir = this.ExecuteCommand(process, "rev-parse --show-toplevel");
if (repoWorkingDir.IsEmpty())
{
this.output.WriteLine("Git repository not found.");
this.output.WriteYellow($"{path} is not a Git repository. Skipping.").WriteLine();
return null;
}

this.output.WriteLine($"Git repository found at {repoWorkingDir}");
this.output.WriteGreen($"Git repository found at {repoWorkingDir}").WriteLine();

var commitHash = this.ExecuteCommand(process, "rev-parse HEAD");
var branchName = this.ExecuteCommand(process, "rev-parse --abbrev-ref HEAD");
Expand Down
Loading

0 comments on commit 4787fb6

Please sign in to comment.