Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
new features: add --auto-generate option to insert command
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Mar 24, 2022
1 parent 37fce57 commit 4964222
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 1,009 deletions.
30 changes: 1 addition & 29 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ on:

env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
GITHUB_ACTIONS_NAME: "github-actions[bot]"
GITHUB_ACTIONS_EMAIL: "41898282+github-actions[bot]@users.noreply.github.com"

permissions: write-all

Expand Down Expand Up @@ -41,34 +39,8 @@ jobs:
with:
install-only: true

- name: Set up Task
uses: arduino/setup-task@v1

- name: Set up Date
run: go run ./scripts/date.go >> date.txt

- name: Build
run: |
BuildDate="$(cat date.txt)" goreleaser release --rm-dist --timeout 100m
cd ./scripts/scoop
yarn
cd ../..
task scoop
- name: Commit files
env:
TAG: ${{ steps.ghtag.outputs.tag }}
run: |
cd ./scripts/scoop/tmp/scoop
git config --local user.email "${{ env.GITHUB_ACTIONS_EMAIL }}"
git config --local user.name "${{ env.GITHUB_ACTIONS_NAME }}"
git diff --cached
git add .
git commit -m "Scoop update for secman version ${TAG}"
- name: Push changes
uses: ad-m/github-push-action@master
with:
repository: "scmn-dev/scoop"
github_token: ${{ secrets.ACCESS_TOKEN }}
directory: ./scripts/scoop/tmp/scoop
run: BuildDate="$(cat date.txt)" goreleaser release --rm-dist --timeout 100m
1 change: 1 addition & 0 deletions app/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func InsertCMD() *cobra.Command {
cmd.Flags().BoolVarP(&PwOpts.Emails, "emails", "e", false, "Insert a email to your vault.")
cmd.Flags().BoolVarP(&PwOpts.Notes, "notes", "n", false, "Insert a note to your vault.")
cmd.Flags().BoolVarP(&PwOpts.Servers, "servers", "s", false, "Insert a server to your vault.")
cmd.Flags().BoolVarP(&PwOpts.AutoGenerate, "auto-generate", "g", false, "Auto generate a secure password for password field (Only works with Logins type).")

return cmd
}
1 change: 1 addition & 0 deletions app/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var PwOpts = options.PasswordsOptions{
ShowHidden: false,
ShowJsonView: false,
ShowTreeView: false,
AutoGenerate: false,
}

var GenOpts = options.GenOptions{
Expand Down
938 changes: 0 additions & 938 deletions go.sum

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions pkg/generator/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,10 @@ import (
)

func Generator(opts *options.GenOptions) {
rand.Seed(time.Now().Unix())
charSet := []rune("abcdedfghijklmnopqrstABCDEFGHIJKLMNOP1234567890")

var output strings.Builder

for i := 0; i < opts.Length; i++ {
random := rand.Intn(len(charSet))
randomChar := charSet[random]
output.WriteRune(randomChar)
}

if opts.Raw {
fmt.Println(output.String())
if opts.Raw {
fmt.Println(SMGenerator(opts.Length))
} else {
out, err := glamour.Render("> " + output.String(), "dark")
out, err := glamour.Render("> " + SMGenerator(opts.Length), "dark")

if err != nil {
fmt.Println("could not render")
Expand All @@ -39,3 +28,18 @@ func Generator(opts *options.GenOptions) {
fmt.Print(lipgloss.NewStyle().PaddingLeft(2).SetString(constants.Logo("Secman Generator")).String() + out)
}
}

func SMGenerator(length int) string {
rand.Seed(time.Now().Unix())
charSet := []rune("abcdedfghijklmnopqrstABCDEFGHIJKLMNOP1234567890")

var output strings.Builder

for i := 0; i < length; i++ {
random := rand.Intn(len(charSet))
randomChar := charSet[random]
output.WriteRune(randomChar)
}

return output.String()
}
18 changes: 13 additions & 5 deletions pkg/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@ import (
"fmt"

"github.com/abdfnx/gosh"
"github.com/abdfnx/looker"
"github.com/scmn-dev/secman/api"
"github.com/charmbracelet/glamour"
"github.com/scmn-dev/secman/internal/config"
)

func Info(version string) {
err, smcVersion, _ := gosh.RunOutput("scc -v")
user := "`" + config.Config("config.name") + "`"

if user == "``" {
user = "`No User`"
}

// remove the last line
smcVersion = smcVersion[:len(smcVersion)-1]
var smcVersion string

_, err := looker.LookPath("scc")

if err != nil {
fmt.Println("could not get scc version")
return
smcVersion = "`Unkown`"
} else {
err, out, _ := gosh.RunOutput("scc -v")

if err != nil {
smcVersion = "`Unkown`"
}

smcVersion = out[:len(out)-1]
}

out1 := fmt.Sprintf(`# Secman CLI
Expand Down
1 change: 1 addition & 0 deletions pkg/options/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type PasswordsOptions struct {
ShowHidden bool
ShowJsonView bool
ShowTreeView bool
AutoGenerate bool
}

type GenOptions struct {
Expand Down
44 changes: 21 additions & 23 deletions pkg/pipe/insert/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/scmn-dev/secman/constants"
"github.com/scmn-dev/secman/pkg/options"
tea "github.com/charmbracelet/bubbletea"
"github.com/scmn-dev/secman/pkg/generator"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/scmn-dev/secman/internal/shared"
Expand All @@ -19,14 +20,15 @@ import (
const okButton shared.Index = iota

type model struct {
styles shared.Styles
focusIndex int
index shared.Index
inputs []textinput.Model
spinner spinner.Model
state shared.State
pwType string
message string
styles shared.Styles
focusIndex int
index shared.Index
inputs []textinput.Model
spinner spinner.Model
state shared.State
pwType string
message string
autoGenerateOpt bool
}

func Insert(o *options.PasswordsOptions) model {
Expand All @@ -48,12 +50,15 @@ func Insert(o *options.PasswordsOptions) model {
return 0
}

autoGenerateOpt := o.AutoGenerate

m := model{
styles: st,
inputs: make([]textinput.Model, inps()),
spinner: shared.NewSpinner(),
state: shared.Ready,
pwType: shared.PasswordType(o),
autoGenerateOpt: autoGenerateOpt,
}

var t textinput.Model
Expand Down Expand Up @@ -81,7 +86,7 @@ func Insert(o *options.PasswordsOptions) model {
case 3:
t.Placeholder = "Password"
t.EchoMode = textinput.EchoPassword
t.EchoCharacter = '•'
t.EchoCharacter = '•'

case 4:
t.Placeholder = "Extra"
Expand Down Expand Up @@ -230,7 +235,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
}

if s == "up" || s == "shift+tab" {
m.focusIndex--
} else {
Expand Down Expand Up @@ -309,12 +314,16 @@ func (m *model) updateInputs(msg tea.Msg) tea.Cmd {
}

func (m model) View() string {
s := "\n\nCreate a new password for your account.\n\n"
s := "\n\nCreate a new password for your vault.\n\n"

if m.state == shared.Loading {
s += spinnerView(m)
} else {
for i := range m.inputs {
if m.pwType == "-l" && m.autoGenerateOpt {
m.inputs[3].SetValue(generator.SMGenerator(15))
}

s += m.inputs[i].View() + "\n"
}

Expand All @@ -337,21 +346,10 @@ func smi(m model) tea.Cmd {
return func() tea.Msg {
insCmd := "scc insert "
cmd := ""
extra := "no-extra"

if m.pwType == "-l" {
if m.inputs[4].Value() != "" {
extra = m.inputs[4].Value()
}
} else if m.pwType == "-s" {
if m.inputs[9].Value() != "" {
extra = m.inputs[9].Value()
}
}

var CMD = func() string {
if m.pwType == "-l" {
cmd = insCmd + m.pwType + " -t " + fmt.Sprintf("\"%s\"", m.inputs[0].Value()) + " -u " + fmt.Sprintf("\"%s\"", m.inputs[1].Value()) + " -U " + fmt.Sprintf("\"%s\"", m.inputs[2].Value()) + " -p " + fmt.Sprintf("\"%s\"", m.inputs[3].Value()) + " -x " + extra
cmd = insCmd + m.pwType + " -t " + fmt.Sprintf("\"%s\"", m.inputs[0].Value()) + " -u " + fmt.Sprintf("\"%s\"", m.inputs[1].Value()) + " -U " + fmt.Sprintf("\"%s\"", m.inputs[2].Value()) + " -p " + fmt.Sprintf("\"%s\"", m.inputs[3].Value()) + " -x " + fmt.Sprintf("\"%s\"", m.inputs[4].Value())
} else if m.pwType == "-c" {
cmd = insCmd + m.pwType + " -t " + fmt.Sprintf("\"%s\"", m.inputs[0].Value()) + " -C " + fmt.Sprintf("\"%s\"", m.inputs[1].Value()) + " -T " + fmt.Sprintf("\"%s\"", m.inputs[2].Value()) + " -N " + fmt.Sprintf("\"%s\"", m.inputs[3].Value()) + " -E " + fmt.Sprintf("\"%s\"", m.inputs[4].Value()) + " -V " + fmt.Sprintf("\"%s\"", m.inputs[5].Value())
} else if m.pwType == "-e" {
Expand Down

0 comments on commit 4964222

Please sign in to comment.