Skip to content

Commit

Permalink
Option to copy password to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Bollos00 committed Dec 14, 2021
1 parent 2f71e64 commit e00b49b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/Bollos00/passwgen-go
go 1.17

require (
"github.com/ogier/pflag" v0.0.1
github.com/atotto/clipboard v0.1.4
github.com/ogier/pflag v0.0.1

)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/ogier/pflag v0.0.1 h1:RW6JSWSu/RkSatfcLtogGfFgpim5p7ARQ10ECk5O750=
github.com/ogier/pflag v0.0.1/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g=
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
package main

import (
"fmt"

"github.com/Bollos00/passwgen-go/src/passwgen"
flag "github.com/ogier/pflag"
)
Expand All @@ -40,9 +38,10 @@ func initFlags() passwgen.PasswGen {
var max *int = flag.IntP("max", "M", 16, "Maximum size of the password")
var length *int = flag.IntP("length", "l", -1,
"Fixed length of the password (overwrites 'min' and 'max' flags)")

var ignoredChars *string = flag.StringP("ignored", "i", "",
"Characters ignored in the creation of the password")
var copyClipboard *bool = flag.BoolP("clipboard", "C", false,
"Copy the generated password to the clipboard and do not print it")

flag.Parse()

Expand Down Expand Up @@ -76,6 +75,7 @@ func initFlags() passwgen.PasswGen {
MinSize: *min,
MaxSize: *max,
IgnoredChars: []byte(*ignoredChars),
CopyClipboard: *copyClipboard,
}

return passgen_instance
Expand All @@ -85,6 +85,7 @@ func main() {

passgen_instace := initFlags()

password := passgen_instace.Generate()
fmt.Printf("Generated password:\n%v\n", password)
passgen_instace.Generate()
// password := passgen_instace.Generate()
// fmt.Printf("Generated password:\n%v\n", password)
}
11 changes: 10 additions & 1 deletion src/passwgen/passwgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"fmt"
"math/rand"
"time"

"github.com/atotto/clipboard"
)

type PasswGenCharType int
Expand Down Expand Up @@ -104,11 +106,12 @@ type PasswGen struct {
MinSize int
MaxSize int
IgnoredChars []byte
CopyClipboard bool
}

func (psg PasswGen) Validate() PasswGen {
if psg.MinSize <= 0 || psg.MaxSize <= 0 {
fmt.Printf("The size of the password should be a positive integrer.\n")
fmt.Printf("The size of the password should be a positive integer.\n")
fmt.Printf("Changing the password length to a random number between 8 and 16.\n\n")
psg.MinSize = 8
psg.MaxSize = 16
Expand Down Expand Up @@ -160,5 +163,11 @@ func (psg PasswGen) Generate() string {
password[i] = availableChars[rand.Intn(int(len(availableChars)))]
}

if psg.CopyClipboard {
clipboard.WriteAll(string(password))
} else {
fmt.Printf("Generated password:\n%v\n", string(password))
}

return string(password)
}

0 comments on commit e00b49b

Please sign in to comment.