Skip to content

Commit 2f5ff5f

Browse files
Dev/abhishekkuma/code refactoring (#16)
* fix connection-string issue * removing temp files * Use Action class from Runtime Config * removing temp files * minor fix * code refactoring * added summary for all the methods
1 parent 15f02bf commit 2f5ff5f

File tree

8 files changed

+485
-312
lines changed

8 files changed

+485
-312
lines changed

Hawaii-Cli/Classes/CommandLineHelp.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.

Hawaii-Cli/Classes/Utils.cs

Lines changed: 0 additions & 161 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using CommandLine;
4+
using CommandLine.Text;
5+
6+
namespace Hawaii.Cli.Models
7+
{
8+
/// <summary>
9+
/// Contains methods to parse the Command line Arguments.
10+
/// </summary>
11+
public class CommandLineHelp
12+
{
13+
14+
/// <summary>
15+
/// For displaying help with a command argument of --help
16+
/// </summary>
17+
public static void DisplayHelp<T>(ParserResult<T> result, IEnumerable<Error> errs)
18+
{
19+
20+
string help = HelpText.AutoBuild(result, helpText =>
21+
{
22+
helpText.AdditionalNewLineAfterOption = false;
23+
helpText.Heading = "See all the available options";
24+
25+
return HelpText.DefaultParsingErrorsHandler(result, helpText);
26+
27+
}, e => e);
28+
29+
Console.WriteLine(help);
30+
Environment.Exit(0);
31+
}
32+
33+
/// <summary>
34+
/// Parse command line for expected arguments
35+
/// </summary>
36+
/// <param name="args">incoming program arguments</param>
37+
public static void ParseArguments(string[] args)
38+
{
39+
Parser parser = new CommandLine.Parser();
40+
41+
ParserResult<CommandLineOptions> results = parser.ParseArguments<CommandLineOptions>(args);
42+
43+
string? command, entity;
44+
results.WithParsed(options => {
45+
command = options.command;
46+
switch (command)
47+
{
48+
case "init":
49+
Operations.Init(options);
50+
break;
51+
52+
case "add":
53+
entity = options.entity;
54+
if(entity==null) {
55+
Console.WriteLine("Please provide a valid Entity.");
56+
break;
57+
}
58+
59+
Operations.Add(entity, options);
60+
break;
61+
62+
case "update":
63+
entity = options.entity;
64+
if(entity==null) {
65+
Console.WriteLine("Please provide a valid Entity.");
66+
break;
67+
}
68+
69+
Operations.Update(entity, options);
70+
break;
71+
72+
default:
73+
Console.WriteLine($"ERROR: Could not execute because the specified command was not found.");
74+
Console.WriteLine("please do init to initialize the config file.");
75+
break;
76+
}
77+
78+
}).WithNotParsed(errors => CommandLineHelp.DisplayHelp(results, errors));
79+
}
80+
}
81+
}

Hawaii-Cli/Classes/CommandLineOptions.cs renamed to Hawaii-Cli/src/Models/CommandLineOptions.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
using System;
22
using CommandLine;
33

4-
namespace Hawaii.Cli.Classes
4+
namespace Hawaii.Cli.Models
55
{
6+
/// <summary>
7+
/// Contains different options that are supported by this Cli tool.
8+
/// </summary>
69
public sealed class CommandLineOptions
710
{
11+
[Value(0, Required = false, HelpText = "Specify the command - init/add/update")]
12+
public string? command { get; set;}
13+
14+
[Value(1, Required = false, HelpText = "Specify the name of entity for adding or updating an entity")]
15+
public string? entity { get; set;}
16+
817
[Option('n', "name", Required = false, HelpText = "Specify the file name, Default value = hawaii-config")]
918
public string? name { get; set; }
1019

@@ -14,6 +23,9 @@ public sealed class CommandLineOptions
1423
[Option("connection-string", Required = false, HelpText = "Connection details to connect to database")]
1524
public string? connectionString { get; set; }
1625

26+
[Option("resolver-config-file", Required = false, HelpText = "Path of the file to resolve the configuration for CosmosDB")]
27+
public string? resolverConfigFile { get; set; }
28+
1729
//TODO: Link options with Specidied commands
1830
// we need to make sure certain options are only required with certain commands.
1931
// for example: source is required only with add/update and not init
@@ -48,8 +60,5 @@ public sealed class CommandLineOptions
4860
[Option("mapping.fields", Required = false, HelpText = "Specify fields to be used for mapping the entities")]
4961
public string? mappingFields { get; set; }
5062

51-
[Option(Default = false, HelpText = "Prints all messages to standard output.")]
52-
public bool Verbose { get; set; }
53-
5463
}
5564
}

0 commit comments

Comments
 (0)