Skip to content

Commit

Permalink
Allow cfg open command to complete when app-descriptor is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillkrylov committed Nov 9, 2023
1 parent 37339ba commit 2fab781
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 66 deletions.
102 changes: 40 additions & 62 deletions clio/Command/RegAppCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,46 @@

namespace Clio.Command
{
[Verb("reg-web-app", Aliases = new string[] { "reg", "cfg" }, HelpText = "Configure a web application settings")]
[Verb("reg-web-app", Aliases = new string[] {"reg", "cfg"}, HelpText = "Configure a web application settings")]
public class RegAppOptions : EnvironmentNameOptions
{

[Option('a', "ActiveEnvironment", Required = false, HelpText = "Set as default web application")]
public string ActiveEnvironment {
get; set;
}
public string ActiveEnvironment { get; set; }

[Option("add-from-iis", Required = false, HelpText = "Register all Creatios from IIS")]
public bool FromIis {
get; set;
}
public bool FromIis { get; set; }

[Option("checkLogin", Required = false, HelpText = "Try login after registration")]
public bool CheckLogin {
get; set;
}
public bool CheckLogin { get; set; }

[Option("host", Required = false, HelpText = "Computer name where IIS is hosted")]
public string Host {
get; set;
}
public string Host { get; set; }

}

public class RegAppCommand : Command<RegAppOptions>
{

private readonly ISettingsRepository _settingsRepository;
private readonly IApplicationClientFactory _applicationClientFactory;
private readonly IPowerShellFactory _powerShellFactory;

public RegAppCommand(ISettingsRepository settingsRepository, IApplicationClientFactory applicationClientFactory, IPowerShellFactory powerShellFactory)
{
public RegAppCommand(ISettingsRepository settingsRepository, IApplicationClientFactory applicationClientFactory,
IPowerShellFactory powerShellFactory){
_settingsRepository = settingsRepository;
_applicationClientFactory = applicationClientFactory;
_powerShellFactory = powerShellFactory;
}

public override int Execute(RegAppOptions options)
{
try
{
if (options.FromIis)
{
public override int Execute(RegAppOptions options){
try {
if (options.FromIis) {
_powerShellFactory.Initialize(options.Login, options.Password, options.Host);
var sites = IISScannerHandler.getSites(_powerShellFactory);

sites.ToList().ForEach(site =>
{
_settingsRepository.ConfigureEnvironment(site.Key, new EnvironmentSettings()
{
sites.ToList().ForEach(site => {
_settingsRepository.ConfigureEnvironment(site.Key, new EnvironmentSettings() {
Login = "Supervisor",
Password = "Supervisor",
Uri = site.Value.ToString(),
Expand All @@ -74,15 +64,11 @@ public override int Execute(RegAppOptions options)
return 0;
}

if (options.EnvironmentName.ToLower(CultureInfo.InvariantCulture) == "open")
{
if (options.EnvironmentName.ToLower(CultureInfo.InvariantCulture) == "open") {
_settingsRepository.OpenFile();
return 0;
}
else
{
var environment = new EnvironmentSettings
{
} else {
var environment = new EnvironmentSettings {
Login = options.Login,
Password = options.Password,
Uri = options.Uri,
Expand All @@ -95,61 +81,53 @@ public override int Execute(RegAppOptions options)
AuthAppUri = options.AuthAppUri,
WorkspacePathes = options.WorkspacePathes
};
if (!string.IsNullOrWhiteSpace(options.ActiveEnvironment))
{
if (_settingsRepository.IsEnvironmentExists(options.ActiveEnvironment))
{
if (!string.IsNullOrWhiteSpace(options.ActiveEnvironment)) {
if (_settingsRepository.IsEnvironmentExists(options.ActiveEnvironment)) {
_settingsRepository.SetActiveEnvironment(options.ActiveEnvironment);
Console.WriteLine($"Active environment set to {options.ActiveEnvironment}");
return 0;
}
else
{
} else {
throw new Exception($"Not found environment {options.ActiveEnvironment} in settings");
}
}
_settingsRepository.ConfigureEnvironment(options.EnvironmentName, environment);
Console.WriteLine($"Environment {options.EnvironmentName} was configured...");
environment = _settingsRepository.GetEnvironment(options);

if (options.CheckLogin)
{
Console.WriteLine($"Try login to {environment.Uri} with {environment.Login ?? environment.ClientId} credentials ...");
if (options.CheckLogin) {
Console.WriteLine(
$"Try login to {environment.Uri} with {environment.Login ?? environment.ClientId} credentials ...");
var creatioClient = _applicationClientFactory.CreateClient(environment);
creatioClient.Login();
Console.WriteLine($"Login successful");
}
return 0;
}
}
catch (ValidationException vex)
{
vex.Errors.Select(e => new { e.ErrorMessage, e.ErrorCode, e.Severity })
.ToList().ForEach(e =>
{
Console.WriteLine($"{e.Severity.ToString().ToUpper()} ({e.ErrorCode}) - {e.ErrorMessage}");
});
} catch (ValidationException vex) {
vex.Errors.Select(e => new {e.ErrorMessage, e.ErrorCode, e.Severity})
.ToList().ForEach(e => {
Console.WriteLine($"{e.Severity.ToString().ToUpper()} ({e.ErrorCode}) - {e.ErrorMessage}");
});
return 1;
}
catch (Exception e)
{
} catch (Exception e) {
Console.WriteLine($"{e.Message}");
return 1;
}
}
}


[Verb("open-settings", Aliases = new string[] { "conf", "configuration", "settings", "os" }, HelpText = "Open configuration file")]
public class OpenCfgOptions {

}

[Verb("open-settings", Aliases = new string[] {"conf", "configuration", "settings", "os"},
HelpText = "Open configuration file")]
public class OpenCfgOptions
{ }

public class OpenCfgCommand : Command<OpenCfgOptions>
{
public OpenCfgCommand() { }

public override int Execute(OpenCfgOptions options) {
public OpenCfgCommand(){ }

public override int Execute(OpenCfgOptions options){
try {
SettingsRepository.OpenSettingsFile();
return 0;
Expand All @@ -158,6 +136,6 @@ public override int Execute(OpenCfgOptions options) {
return 1;
}
}
}

}
}
}
9 changes: 7 additions & 2 deletions clio/Environment/ConfigurationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,14 @@ private void InitSettings() {
_settings = JsonConvert.DeserializeObject<Settings>(fileContent);
}
}
} catch(FormatException ex) {
} catch(Exception ex) {
Console.WriteLine($"{ex.Message} Correct or delete settings file before use clio. File path: {AppSettingsFilePath}");
throw;
if(Program.EnableUpdate) {
_settings = default;
}
else {
throw;
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion clio/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ private static void TryCheckForUpdate() {
}
}


private static int Main(string[] args) {
try {
return ExecuteCommands(args);
Expand All @@ -314,7 +315,7 @@ private static int Main(string[] args) {
}

private static int ExecuteCommands(string[] args) {
TryCheckForUpdate();
TryCheckUpdate(args);
var creatioEnv = new CreatioEnvironment();
string helpFolderName = $"help";
string helpDirectoryPath = helpFolderName;
Expand All @@ -329,6 +330,19 @@ private static int ExecuteCommands(string[] args) {
}
return HandleParseError(((NotParsed<object>)parserResult).Errors);
}

public static bool EnableUpdate = false;
private static void TryCheckUpdate(string[] args){
EnableUpdate = args.Length switch
{
2 when args[0] == "cfg" && args[1] == "open" => true,
_ => EnableUpdate
};

if(!EnableUpdate) {
TryCheckForUpdate();
}
}

private static PushPkgOptions CreateClioGatePkgOptions(InstallGateOptions opts) {
var pushPackageOptions = CreatePushPkgOptions(opts);
Expand Down
3 changes: 2 additions & 1 deletion clio/clio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<CodeAnalysisRuleSet>clio.ruleset</CodeAnalysisRuleSet>
<PackageReadmeFile>README.md</PackageReadmeFile>
<LangVersion>11</LangVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down Expand Up @@ -744,7 +745,7 @@
<Content Include="tpl\k8\infrastructure\pgadmin\pgadmin-secrets.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 2fab781

Please sign in to comment.