Skip to content

Commit

Permalink
Merge pull request #1 from Bollos00/clipboard
Browse files Browse the repository at this point in the history
option to copy the generated password into the Clipboard
  • Loading branch information
Bollos00 authored Dec 14, 2021
2 parents 2f71e64 + 5334a47 commit 3641e50
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# Credits https://gist.github.com/thatisuday/b3b6096457e64cdfa20796315ed29fbd

name: Build

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]


# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build-linux:
name: Build the application in Linux environment
runs-on: ubuntu-latest
steps:

# step 1: set up go
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13

# step 2: checkout repository code
- name: Checkout code into workspace directory
uses: actions/checkout@v2

# step 3: install dependencies
- name: Install all Go dependencies
run: go get

# step 4: run the application
- name: go run
run: go run ./main.go -l 30
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)
}
15 changes: 14 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,15 @@ func (psg PasswGen) Generate() string {
password[i] = availableChars[rand.Intn(int(len(availableChars)))]
}

if psg.CopyClipboard {
if err := clipboard.WriteAll(string(password)); err == nil {
fmt.Printf("Successfully copied the password to the clipboard.\n\n")
} else {
fmt.Printf("Error when trying to copy the password to the clipboard\n%v", err.Error())
}
} else {
fmt.Printf("Generated password:\n%v\n", string(password))
}

return string(password)
}

0 comments on commit 3641e50

Please sign in to comment.