Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to System.CommandLine #7653

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft

Conversation

rubo
Copy link
Contributor

@rubo rubo commented Oct 24, 2024

Replaces #4310

Changes

  • Replaced the McMaster.Extensions.CommandLineUtils with System.CommandLine as the CLI options parser
  • Changed configuration file extensions from .cfg to .json. The change is backward-compatible, i.e., the .cfg files are still handled if no .json equivalent is found. See Remarks below.
  • Program.cs has been heavily refactored and changed to a top-level file
  • Added new CLI option aliases to be closer to the conventional POSIX style and have less C# notation:
    nethermind --configsDirectory path/to/dir --JsonRpc.JwtSecretFile path/to/jwt.hex
    # or
    nethermind --configs-dir path/to/dir --jsonrpc-jwtsecretfile path/to/jwt.hex
  • Revised logs wording
  • Unified logging styles on startup to have coloring from the beginning and avoid sudden formatting switch on configuration load

Types of changes

What types of changes does your code introduce?

  • Bugfix (a non-breaking change that fixes an issue)
  • New feature (a non-breaking change that adds functionality)
  • Breaking change (a change that causes existing functionality not to work as expected)
  • Optimization
  • Refactoring
  • Documentation update
  • Build-related changes
  • Other: Description

Testing

Requires testing

  • Yes
  • No

If yes, did you write tests?

  • Yes
  • No

Notes on testing

Needs manual testing

Documentation

Requires documentation update

  • Yes
  • No

PR pending.

Requires explanation in Release Notes

  • Yes
  • No

Optionally. Pending.

Remarks

The large number of changed files is due to .cfg to .json rename. I commented on the changes that need attention except for Program.cs, which has too many changes. The rest are file scope namespace changes or extension changes.

@rubo rubo force-pushed the feature/system-commandline-2 branch from 2f13e8e to 9c98998 Compare October 24, 2024 23:12
@LukaszRozmej
Copy link
Member

LukaszRozmej commented Oct 25, 2024

Can you explain why? I thought McMaster.Extensions.CommandLineUtils is quite good and solved all our issues, why System.CommandLine would be better if not only by being 1st party?

Also why change cfg to json? It was a nice distinction which is chainspec and which config.

@rubo
Copy link
Contributor Author

rubo commented Oct 25, 2024

@LukaszRozmej We had no particular issues with Microsoft.Extensions.CommandLineUtils but NuGet warnings of deprecation. The replacement McMaster.Extensions.CommandLineUtils was also abandoned by the time we picked it; its package is just not yet marked as deprecated. So, the goal here is to have something in active development and future-oriented.

.json is a well-known standard extension and is more editor-friendly and self-explanatory. Generally, I'd like to use something standard and familiar to everyone. Given we have chain specs and configs in different directories, there's no confusion, and the distinction is still clear. Moreover, chain specs are embedded in binary and are not visible to end-users.

[ConfigItem(Description = "Enables Hive plugin used for executing Hive Ethereum Tests.", EnvironmentVariable = "NETHERMIND_HIVE_ENABLED", DefaultValue = "false")]
public bool HiveEnabled { get; set; }
//[ConfigItem(Description = "Enables Hive plugin used for executing Hive Ethereum Tests.", EnvironmentVariable = "NETHERMIND_HIVE_ENABLED", DefaultValue = "false")]
//public bool HiveEnabled { get; set; }
Copy link
Contributor Author

@rubo rubo Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukaszRozmej The NETHERMIND_HIVE_ENABLED seems redundant as we already have NETHERMIND_HIVECONFIG_ENABLED with IHiveConfig.Enabled. Related tests are failing, but I don't wanna touch them until I get feedback.


[ConfigItem(Description = "Sets the job name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_JOB")]
public string MonitoringJob { get; set; }

[ConfigItem(Description = "Sets the default group name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_GROUP")]
public string MonitoringGroup { get; set; }
//[ConfigItem(Description = "Sets the default group name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_GROUP")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukaszRozmej NETHERMIND_MONITORING_GROUP seems no longer relevant.

[ConfigItem(Description = "Plugins directory")]
public string PluginsDirectory { get; set; }
//[ConfigItem(Description = "Plugins directory")]
//public string PluginsDirectory { get; set; }

[ConfigItem(Description = "Sets the job name for metrics monitoring.", EnvironmentVariable = "NETHERMIND_MONITORING_JOB")]
public string MonitoringJob { get; set; }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukaszRozmej NETHERMIND_MONITORING_JOB seems no longer relevant.


void Load(ILogManager logManager);
void Load();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ILogManager parameter has been removed from the Load() method. Implementations can require it in their constructors if needed. This was helpful in the context of Program.cs refactoring.

namespace Nethermind.Api.Extensions
namespace Nethermind.Api.Extensions;

public class PluginLoader(string pluginPath, IFileSystem fileSystem, ILogger logger, params Type[] embedded) : IPluginLoader
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added ILogger to the constructor instead of taking ILogManager in the Load() method. That's it for this file.


public void Load(ILogManager logManager) { }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed ILogManager from the Load() method. This implementation doesn't need it at all.

public CancellationToken Token => _cancellationTokenSource!.Token;
public ProcessExitSource(CancellationToken cancellationToken)
{
_cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
Copy link
Contributor Author

@rubo rubo Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.CommandLine provides a cancellation token for ctrl+c, so we don't need to handle it ourselves. Thus, ProcessExitSource now uses a linked cancellation token source to combine its own with the one of the CLI parser.


foreach (var source in _configSource)
// Skip the validation for ArgsConfigSource items as they are already validated by the CLI parser
foreach (IConfigSource source in _configSource.Where(s => s is not ArgsConfigSource))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabled validation for ArgsConfigSource (CLI arguments) since the CLI parser does the job now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants