Skip to content

Commit

Permalink
Improve handling of variables (#77)
Browse files Browse the repository at this point in the history
* Properly abort the Cmdlet when the user aborts the interactive prompt
* Do not prompt for variables in the Update-Catlet Cmdlet as variables are fixed after catlet creation
* Bump config model packages
  • Loading branch information
ChristopherMann authored Jul 22, 2024
1 parent 31ac120 commit 14bbd73
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
26 changes: 16 additions & 10 deletions src/Eryph.ComputeClient.Commands/Catlets/CatletConfigCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ protected static CatletConfig DeserializeConfigString(string configString)

}

protected void PopulateVariables(CatletConfig catletConfig, Hashtable variables, bool skipVariablesPrompt)
/// <summary>
/// Populates the variables in the given <paramref name="catletConfig"/> by
/// applying the values from the <paramref name="variables"/> hashtable and
/// interactively asking the user for the values. Returns <see langword="false"/>
/// when the user aborted the interactive prompt and <see langword="true"/> otherwise.
/// </summary>
protected bool PopulateVariables(CatletConfig catletConfig, Hashtable variables, bool skipVariablesPrompt)
{
if (catletConfig.Variables is not { Length: > 0 })
return;
return true;

ApplyVariablesFromParameter(catletConfig.Variables, variables);
if (!skipVariablesPrompt)
{
ReadVariablesFromInput(catletConfig.Variables);
}

return skipVariablesPrompt || ReadVariablesFromInput(catletConfig.Variables);
}

private static void ApplyVariablesFromParameter(VariableConfig[] variableConfigs, Hashtable variables)
Expand All @@ -69,7 +73,7 @@ private static void ApplyVariablesFromParameter(VariableConfig[] variableConfigs
}
}

private void ReadVariablesFromInput(VariableConfig[] variableConfigs)
private bool ReadVariablesFromInput(VariableConfig[] variableConfigs)
{
var anyRequired = variableConfigs.Any(vc =>
vc.Required.GetValueOrDefault()
Expand Down Expand Up @@ -103,7 +107,7 @@ private void ReadVariablesFromInput(VariableConfig[] variableConfigs)

// The prompt returns -1 when the user cancels the prompt (e.g. Ctrl+C).
if (choice is -1 or 1)
return;
return choice != -1;

var requiredOnly = choice == 2;

Expand All @@ -114,17 +118,19 @@ private void ReadVariablesFromInput(VariableConfig[] variableConfigs)
{
var result = ReadVariableFromInput(variableConfig);
if (result is null)
return;
return false;

variableConfig.Value = result;
}
}
catch (PSInvalidOperationException)
catch (Exception ex) when (ex is PSInvalidOperationException or HostException)
{
// The prompt for choice will fail if the Powershell session is not
// interactive. Unfortunately, there is no easy way to reliably detect
// a non-interactive session. Hence, we just catch the exception and continue.
}

return true;
}

private string ReadVariableFromInput(VariableConfig config)
Expand Down
3 changes: 2 additions & 1 deletion src/Eryph.ComputeClient.Commands/Catlets/NewCatletCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ protected override void EndProcessing()
if (!string.IsNullOrWhiteSpace(Name))
config.Name = Name;

PopulateVariables(config, Variables, SkipVariablesPrompt);
if (!PopulateVariables(config, Variables, SkipVariablesPrompt))
return;

var serializedConfig = JsonSerializer.SerializeToElement(config, ConfigModelJsonSerializer.DefaultOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,12 @@ public SwitchParameter NoWait

private bool _nowait;

[Parameter]
public Hashtable Variables { get; set; }

[Parameter]
public SwitchParameter SkipVariablesPrompt { get; set; }

protected override void ProcessRecord()
{
foreach (var id in Id)
{
var config = DeserializeConfigString(Config);

PopulateVariables(config, Variables, SkipVariablesPrompt);

WaitForOperation(Factory.CreateCatletsClient().Update(id, new UpdateCatletRequestBody(Guid.NewGuid(),
JsonSerializer.SerializeToElement(config)))
, _nowait, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

<ItemGroup>
<PackageReference Include="Eryph.ClientRuntime.Powershell" Version="0.7.0" />
<PackageReference Include="Eryph.ConfigModel.Catlets.Yaml" Version="0.5.0" />
<PackageReference Include="Eryph.ConfigModel.Networks.Yaml" Version="0.5.0" />
<PackageReference Include="Eryph.ConfigModel.System.Json" Version="0.5.0" />
<PackageReference Include="Eryph.ConfigModel.Catlets.Yaml" Version="0.6.0" />
<PackageReference Include="Eryph.ConfigModel.Networks.Yaml" Version="0.6.0" />
<PackageReference Include="Eryph.ConfigModel.System.Json" Version="0.6.0" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 14bbd73

Please sign in to comment.