Skip to content

Commit

Permalink
Merge pull request #18 from Crequency/dev=main
Browse files Browse the repository at this point in the history
[Pull Request] New powerful cli tool project
  • Loading branch information
Dynesshely authored Mar 21, 2024
2 parents 315aa3a + 8b01c11 commit e8a52cc
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 32 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x
- name: Build
run: |
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Publish

on:
push:
branches: ["main", "dev=main"]
pull_request:
branches: ["main"]

workflow_dispatch:

jobs:
build-on-ubuntu:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
8.0.x
- name: Add to GitHub Repo
if: ${{ github.event_name != 'pull_request' }}
run: |
nuget sources add -name github -Source https://nuget.pkg.github.com/Crequency/index.json -Username Crequency -Password ${{ secrets.GitHubToken }}
- name: Install NuGet
uses: nuget/setup-nuget@v1
if: ${{ github.event_name != 'pull_request' }}
with:
nuget-version: "6.x"

- name: Build and Publish Cli Tool
working-directory: "Common.BasicHelper.Cli"
if: ${{ github.event_name != 'pull_request' }}
run: |
dotnet build -c Release
nuget push ./bin/Release/*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }} -NoSymbol
nuget push ./bin/Release/*.nupkg -Source github -SkipDuplicate
13 changes: 13 additions & 0 deletions Common.BasicHelper.Cli/Arguments/BasicOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CommandLine;

namespace Common.BasicHelper.Cli.Arguments;

public class BasicOptions
{

[Option("verbose", HelpText = "Display verbose output.")]
public bool Verbose { get; set; }

[Option("dry-run", HelpText = "Dry run the command.")]
public bool DryRun { get; set; }
}
81 changes: 81 additions & 0 deletions Common.BasicHelper.Cli/Arguments/Verbs/VerbPassword.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using CommandLine;
using Common.BasicHelper.Utils;

namespace Common.BasicHelper.Cli.Arguments.Verbs;

[Verb("password", aliases: ["passwd", "pwd"], HelpText = "Password related utils.")]
public class VerbPassword : BasicOptions
{
[Option('g', "generate", Default = true, HelpText = "Generate a password.")]
public bool Generate { get; set; }

[Option('l', "length", Default = 12, HelpText = "Length of the password.")]
public int Length { get; set; }

[Option('r', "length-range", HelpText = "Length range of the password, like `3,5`.")]
public string? LengthRange { get; set; }

[Option('u', "ignore-uppercase", HelpText = "Ignore uppercase letters.")]
public bool IgnoreUppercase { get; set; }

[Option('e', "ignore-lowercase", HelpText = "Ignore lowercase letters.")]
public bool IgnoreLowercase { get; set; }

[Option('n', "ignore-numbers", HelpText = "Ignore numbers.")]
public bool IgnoreNumbers { get; set; }

[Option('s', "ignore-symbols", HelpText = "Ignore symbols.")]
public bool IgnoreSymbols { get; set; }

[Option('U', "supported-uppercase", Default = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", HelpText = "Supported uppercase letters.")]
public string? SupportedUppercase { get; set; }

[Option('E', "supported-lowercase", Default = "abcdefghijklmnopqrstuvwxyz", HelpText = "Supported lowercase letters.")]
public string? SupportedLowercase { get; set; }

[Option('N', "supported-numbers", Default = "0123456789", HelpText = "Supported numbers.")]
public string? SupportedNumbers { get; set; }

[Option('S', "supported-symbols", Default = "!@#$%^&*()_+-=[]{};':,./<>?", HelpText = "Supported symbols.")]
public string? SupportedSymbols { get; set; }
}

public static class PasswordExtensions
{
public static VerbPassword Execute(this VerbPassword pwdc)
{
int lengthRangeStart = 0;
int lengthRangeEnd = 0;

var lengthRangeProvided = pwdc.LengthRange is not null;

if (pwdc.LengthRange is not null)
{
var splited = pwdc.LengthRange.Split(',');

if (splited.Length != 2)
throw new ArgumentException("Length range should be like `3,5`.");

lengthRangeProvided = lengthRangeProvided && int.TryParse(splited[0], out lengthRangeStart);
lengthRangeProvided = lengthRangeProvided && int.TryParse(splited[1], out lengthRangeEnd);
}

var pwd = Password.GeneratePassword(
pwdc.Length,
lengthRangeProvided ? lengthRangeStart : null,
lengthRangeProvided ? lengthRangeEnd : null,
pwdc.IgnoreUppercase == false,
pwdc.IgnoreLowercase == false,
pwdc.IgnoreNumbers == false,
pwdc.IgnoreSymbols == false,
pwdc.SupportedUppercase!,
pwdc.SupportedLowercase!,
pwdc.SupportedNumbers!,
pwdc.SupportedSymbols!
);

Console.WriteLine(pwd);

return pwdc;
}
}
52 changes: 52 additions & 0 deletions Common.BasicHelper.Cli/Common.BasicHelper.Cli.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Version>0.1.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2005-06-06"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))</Version>
</PropertyGroup>

<PropertyGroup>
<Authors>Dynesshely</Authors>
<Company>Crequency</Company>
<Copyright>Copyright (C) Crequency 2024</Copyright>
</PropertyGroup>

<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<RepositoryUrl>https://github.com/Crequency/Common.BasicHelper/</RepositoryUrl>
<PackageProjectUrl>https://github.com/Crequency/Common.BasicHelper/</PackageProjectUrl>
<PackageLicenseExpression>AGPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageId>$(AssemblyName)</PackageId>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<PropertyGroup>
<PackAsTool>true</PackAsTool>
<ToolCommandName>cbt</ToolCommandName>
</PropertyGroup>

<ItemGroup>
<None Include="icon.png" Pack="True" PackagePath="\" />
<None Include="..\README.md" Pack="True" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common.BasicHelper.Samples\Common.BasicHelper.Samples.csproj" />
<ProjectReference Include="..\Common.BasicHelper\Common.BasicHelper.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions Common.BasicHelper.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using CommandLine;
using Common.BasicHelper.Cli.Arguments.Verbs;

Parser.Default.ParseArguments<VerbPassword, object>(args)
.WithParsed<VerbPassword>(options => options.Execute())
;
Binary file added Common.BasicHelper.Cli/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Common.BasicHelper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.BasicHelper.Test", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.BasicHelper.Samples", "Common.BasicHelper.Samples\Common.BasicHelper.Samples.csproj", "{C363C055-5CDC-4CEB-935C-D099CE0681BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.BasicHelper.Cli", "Common.BasicHelper.Cli\Common.BasicHelper.Cli.csproj", "{3545B3A1-32CE-4404-8749-0E930354F5BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{C363C055-5CDC-4CEB-935C-D099CE0681BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C363C055-5CDC-4CEB-935C-D099CE0681BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C363C055-5CDC-4CEB-935C-D099CE0681BF}.Release|Any CPU.Build.0 = Release|Any CPU
{3545B3A1-32CE-4404-8749-0E930354F5BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3545B3A1-32CE-4404-8749-0E930354F5BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3545B3A1-32CE-4404-8749-0E930354F5BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3545B3A1-32CE-4404-8749-0E930354F5BA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
34 changes: 14 additions & 20 deletions Common.BasicHelper/Common.BasicHelper.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>preview</LangVersion>
Expand All @@ -7,42 +7,36 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<Authors>Dynesshely</Authors>
<Company>Crequency</Company>
<Description>Basic utils for all projects based on CSharp.</Description>
<Copyright>Copyright © Crequency 2022-present</Copyright>
</PropertyGroup>

<PropertyGroup>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Version>
1.3.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2022-01-27"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))
</Version>
<Version>1.3.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2005-06-06"))).TotalDays).$([System.Math]::Floor($([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes)))</Version>
</PropertyGroup>

<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Authors>Dynesshely</Authors>
<Company>Crequency</Company>
<Copyright>Copyright (C) Crequency 2022-present</Copyright>
</PropertyGroup>

<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<RepositoryUrl>https://github.com/Crequency/Common.BasicHelper/</RepositoryUrl>
<PackageProjectUrl>https://github.com/Crequency/Common.BasicHelper/</PackageProjectUrl>
<PackageLicenseExpression>AGPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageId>$(AssemblyName)</PackageId>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>AGPL-3.0-only</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/Crequency/Common.BasicHelper/</PackageProjectUrl>
<RepositoryUrl>https://github.com/Crequency/Common.BasicHelper/</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<None Include="icon.png" Pack="True" PackagePath="\" />
<None Include="..\README.md" Pack="True" PackagePath="\" />
<None Include="icon.png" Pack="True" PackagePath="\"/>
<None Include="..\README.md" Pack="True" PackagePath="\"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>

</Project>
</Project>
23 changes: 15 additions & 8 deletions Common.BasicHelper/Utils/Password.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.BasicHelper.Core.Exceptions;
Expand Down Expand Up @@ -97,18 +98,24 @@ public static string GeneratePassword

var random = new Random();

var chars = new string[4]
{
supportedUppercases,
supportedLowercases,
supportedNumbers,
supportedSymbols
};
var chars = new List<string>();

if (includeUppercase)
chars.Add(supportedUppercases);

if (includeLowercase)
chars.Add(supportedLowercases);

if (includeNumbers)
chars.Add(supportedNumbers);

if (includeSymbols)
chars.Add(supportedSymbols);

var generateLength = length ?? random.Next(lengthRangeStart ?? 0, lengthRangeEnd ?? 13);

var selected = from item in Enumerable.Range(0, generateLength)
let selection = chars[random.Next(0, chars.Length)]
let selection = chars[random.Next(0, chars.Count)]
select selection[random.Next(0, selection.Length)];

foreach (var item in selected)
Expand Down

0 comments on commit e8a52cc

Please sign in to comment.