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

Golang tool #92

Merged
merged 40 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
45a28f3
go mod init
milliorn Feb 14, 2024
a5fa893
hello world go
milliorn Feb 14, 2024
641665b
go get github.com/urfave/cli/v2
milliorn Feb 14, 2024
e49ae7c
urfave hello world
milliorn Feb 14, 2024
1bea9cd
Add error handling to app.Run() function
milliorn Feb 14, 2024
6906f2e
help script
milliorn Feb 14, 2024
cad3d60
Update app name and usage message
milliorn Feb 14, 2024
2b16956
comments
milliorn Feb 15, 2024
f0ac7f8
Flags
milliorn Feb 15, 2024
ee09f1b
Add sorting to CLI flags and commands
milliorn Feb 15, 2024
8384807
Update password length validation in CLI application
milliorn Feb 15, 2024
7389c60
Update password length validation
milliorn Feb 15, 2024
7b9e0ac
Add version flag to CLI application
milliorn Feb 16, 2024
0f654a1
Refactor CLI flags and add createPassword function
milliorn Feb 17, 2024
4304c96
add const
milliorn Feb 18, 2024
db04c40
create password
milliorn Feb 19, 2024
6b11f63
Refactor password generation flags and error handling
milliorn Feb 20, 2024
da5cef6
Add override-length flag to CLI application
milliorn Feb 20, 2024
c797759
Refactor password generation and flag handling
milliorn Feb 20, 2024
99b1a9a
Refactor generateAlpha function to improve readability and maintainab…
milliorn Feb 20, 2024
05e3df0
Refactor password generation logic
milliorn Feb 20, 2024
da44cb5
Refactor character generation functions***
milliorn Feb 20, 2024
5010b72
Refactor code to improve performance and readability
milliorn Feb 20, 2024
1ce8286
Add error handling to getCharacterSet function
milliorn Feb 20, 2024
d0fdecd
Refactor error handling in main.go
milliorn Feb 20, 2024
4514e59
Refactor getCharacterSet function to simplify code
milliorn Feb 20, 2024
b41a965
removed unused code
milliorn Feb 20, 2024
692cdb9
Add comments and improve code structure
milliorn Feb 20, 2024
de8d5b8
Refactor character set constants in main.go
milliorn Feb 20, 2024
b30b51e
Add buffer for character set in getCharacterSet function
milliorn Feb 20, 2024
1dbc818
Refactor getCharacterSet function signature and update comments
milliorn Feb 20, 2024
27adc4f
sample scripts
milliorn Feb 20, 2024
b52d3d7
examples
milliorn Feb 20, 2024
873be0a
added test script
milliorn Feb 20, 2024
b21f870
Add tests for command line flags
milliorn Feb 20, 2024
681d62b
Add unit tests for getCharacterSet function
milliorn Feb 20, 2024
e4e0272
Add tests for password generation function
milliorn Feb 20, 2024
feff56f
removed comments
milliorn Feb 20, 2024
cc0c4f7
Update module name to password-generator
milliorn Feb 20, 2024
195c0f3
Merge branch 'main' into golang
milliorn Feb 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions golang/examples/help.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# This script is used to display the help message for the script.
cd ../
go run main.go -h
5 changes: 5 additions & 0 deletions golang/examples/length.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Run the application
cd ../
go run main.go -length 100
5 changes: 5 additions & 0 deletions golang/examples/letters.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Run the application
cd ../
go run main.go -ns -nn
5 changes: 5 additions & 0 deletions golang/examples/lowercase.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Run the application
cd ../
go run main.go -ns -nn -nu
5 changes: 5 additions & 0 deletions golang/examples/numbers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Run the application
cd ../
go run main.go -nle -ns
5 changes: 5 additions & 0 deletions golang/examples/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Run the application
cd ../
go run main.go
4 changes: 4 additions & 0 deletions golang/examples/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

cd ..
go test -v
5 changes: 5 additions & 0 deletions golang/examples/uppercase.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Run the application
cd ../
go run main.go -ns -nn -nl
10 changes: 10 additions & 0 deletions golang/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module password-generator

go 1.22.0

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
)
8 changes: 8 additions & 0 deletions golang/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
194 changes: 194 additions & 0 deletions golang/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package main

import (
"bytes"
"errors"
"fmt"
"log"
"math/rand"
"os"
"sort"
"time"

"github.com/urfave/cli/v2"
)

// Define character sets
const (
upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowerChars = "abcdefghijklmnopqrstuvwxyz"
numberChars = "0123456789"
symbolChars = "!@#$%^&*_-+="
alphaChars = upperChars + lowerChars
)

// main is the entry point of the program.
func main() {
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "print only the version",
}

// The app variable represents the command-line application.
app := &cli.App{
Name: "create-password",
Version: "1.0.0",
Usage: "Generate a password involves creating a random mix of uppercase and lowercase letters, numbers, and special symbols.",

// Define the flags that the application accepts.
Flags: []cli.Flag{
&cli.IntFlag{
Name: "length",
Aliases: []string{"l"},
Value: 8,
Usage: "Length of the password",
},

&cli.BoolFlag{
Name: "no-numbers",
Aliases: []string{"nn"},
Value: false,
Usage: "Exclude numbers from the password",
},

&cli.BoolFlag{
Name: "no-symbols",
Aliases: []string{"ns"},
Value: false,
Usage: "Exclude special symbols from the password",
},

&cli.BoolFlag{
Name: "no-uppercase",
Aliases: []string{"nu"},
Value: false,
Usage: "Exclude uppercase letters from the password",
},

&cli.BoolFlag{
Name: "no-lowercase",
Aliases: []string{"nl"},
Value: false,
Usage: "Exclude lowercase letters from the password",
},

&cli.BoolFlag{
Name: "no-letters",
Aliases: []string{"nle"},
Value: false,
Usage: "Exclude letters from the password",
},
},

// Define the action that the application should take when it is run.
Action: func(cCtx *cli.Context) error {
// Get the values of the flags
length := cCtx.Int("length")
noLetters := cCtx.Bool("no-letters")
noLowercase := cCtx.Bool("no-lowercase")
noNumbers := cCtx.Bool("no-numbers")
noSymbols := cCtx.Bool("no-symbols")
noUppercase := cCtx.Bool("no-uppercase")

// Get the character set
chars, err := getCharacterSet(noLetters, noLowercase, noNumbers, noSymbols, noUppercase)

// If there is an error, return it
if err != nil {
return fmt.Errorf("failed to get character set: %w", err)
}

// Create a new random number generator
randSeed := rand.New(rand.NewSource(time.Now().UnixNano()))

// Generate a password
password := generatePassword(length, randSeed, chars)

// Print the password
fmt.Println(password)

// Return nil to indicate that the action executed successfully
return nil
},
}

// Sorting flags and commands
sortFlagsAndCommands(app)

// Run the application
if err := app.Run(os.Args); err != nil {
log.Fatalf("error: %s", err)
}
}

// getCharacterSet returns the character set to use for generating a password.
// The noLetters, noLowercase, noNumbers, noSymbols, and noUppercase parameters
// specify whether to exclude letters, lowercase letters, numbers, symbols, and
// uppercase letters from the character set, respectively.
// The function returns the character set to use for generating a password.
// If there is an error, the function returns the error.
func getCharacterSet(noLetters, noLowercase, noNumbers, noSymbols, noUppercase bool) (string, error) {
if noLetters && noNumbers && noSymbols {
return "", errors.New("all character types are excluded, unable to generate password")
}

// Create a buffer to store the character set
var buf bytes.Buffer

// If no letters are excluded, add them to the buffer
if !noLetters {
// If no uppercase or lowercase letters are excluded, add them to the buffer
if !noLowercase && !noUppercase {
buf.WriteString(alphaChars)
} else {
// If no letters are excluded, add them to the buffer
if !noLowercase {
buf.WriteString(lowerChars)
}
// If no letters are excluded, add them to the buffer
if !noUppercase {
buf.WriteString(upperChars)
}
}
}

// If no numbers or symbols are excluded, add them to the buffer
if !noNumbers {
buf.WriteString(numberChars)
}

// If no symbols are excluded, add them to the buffer
if !noSymbols {
buf.WriteString(symbolChars)
}

// If no characters are included, return an error
if buf.Len() == 0 {
return "", errors.New("no characters available for password generation")
}

return buf.String(), nil
}

// generatePassword returns a randomly generated password.
// The length parameter specifies the length of the password to generate.
// The randSeed parameter is the random number generator to use.
// The chars parameter is the character set to use for generating the password.
// The function returns a randomly generated password.
func generatePassword(length int, randSeed *rand.Rand, chars string) string {
password := make([]byte, length)

for i := 0; i < length; i++ {
password[i] = chars[randSeed.Intn(len(chars))]
}

return string(password)
}

// sortFlagsAndCommands sorts the flags and commands of the application.
// The app parameter is the application to sort.
func sortFlagsAndCommands(app *cli.App) {
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
}
Loading
Loading