Skip to content

Commit ff104cc

Browse files
authored
Merge pull request #41 from GGP1/enhancements_and_fixes
Enhancements and fixes
2 parents 123ea5a + fee2bf8 commit ff104cc

36 files changed

+718
-333
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Set up Go
2727
uses: actions/setup-go@v5
2828
with:
29-
go-version: 1.22
29+
go-version: 1.23
3030

3131
- name: Run tests
3232
run: go test ./... -race

.goreleaser.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ before:
55
- go mod tidy
66
builds:
77
-
8-
ldflags: -s -w -X main.version={{ .Version }} -X main.commit={{ .ShortCommit }} -X main.date={{ .CommitDate }}
8+
ldflags: -s -w -X main.version={{ .Tag }} -X main.commit={{ .ShortCommit }} -X main.date={{ .CommitDate }}
99
mod_timestamp: "{{ .CommitTimestamp }}"
1010
flags:
1111
- -trimpath

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.22-alpine3.20 as builder
1+
FROM golang:1.23-alpine3.20 as builder
22

33
WORKDIR /kure
44

Dockerfile.secure

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.22-alpine3.20 as builder
1+
FROM golang:1.23-alpine3.20 as builder
22

33
WORKDIR /kure
44

commands/2fa/2fa.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ Use the [-i info] flag to display information about the setup key, it also gener
5252
Example: example,
5353
Args: cmdutil.MustExistLs(db, cmdutil.TOTP),
5454
RunE: run2FA(db, &opts),
55-
PostRun: func(cmd *cobra.Command, args []string) {
56-
// Reset variables (session)
57-
opts = tfaOptions{}
58-
},
5955
}
6056

6157
cmd.AddCommand(add.NewCmd(db, os.Stdin), rm.NewCmd(db, os.Stdin))
@@ -65,6 +61,12 @@ Use the [-i info] flag to display information about the setup key, it also gener
6561
f.BoolVarP(&opts.info, "info", "i", false, "display information about the setup key")
6662
f.DurationVarP(&opts.timeout, "timeout", "t", 0, "clipboard clearing timeout")
6763

64+
cmd.PostRun = func(cmd *cobra.Command, args []string) {
65+
// Reset variables (session)
66+
opts = tfaOptions{}
67+
f.Lookup("timeout").Changed = false
68+
}
69+
6870
return cmd
6971
}
7072

commands/2fa/rm/rm.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,55 @@ import (
1414
)
1515

1616
const example = `
17-
kure 2fa rm Sample`
17+
* Remove a TOTP
18+
kure 2fa rm Sample
19+
20+
* Remove a directory
21+
kure 2fa rm SampleDir/
22+
23+
* Remove multiple totp
24+
kure 2fa rm Sample Sample2 Sample3`
1825

1926
// NewCmd returns the a new command.
2027
func NewCmd(db *bolt.DB, r io.Reader) *cobra.Command {
2128
return &cobra.Command{
22-
Use: "rm <name>",
23-
Short: "Remove a two-factor authentication code from an entry",
29+
Use: "rm <names>",
30+
Short: "Remove two-factor authentication codes or directories",
2431
Example: example,
25-
Args: cmdutil.MustExist(db, cmdutil.TOTP),
32+
Args: cmdutil.MustExist(db, cmdutil.TOTP, true),
2633
RunE: runRm(db, r),
2734
}
2835
}
2936

3037
func runRm(db *bolt.DB, r io.Reader) cmdutil.RunEFunc {
3138
return func(cmd *cobra.Command, args []string) error {
32-
name := strings.Join(args, " ")
33-
name = cmdutil.NormalizeName(name)
34-
3539
if !terminal.Confirm(r, "Are you sure you want to proceed?") {
3640
return nil
3741
}
3842

39-
if err := totp.Remove(db, name); err != nil {
40-
return err
43+
names := make([]string, 0, len(args))
44+
for _, name := range args {
45+
name = cmdutil.NormalizeName(name, true)
46+
47+
if !strings.HasSuffix(name, "/") {
48+
names = append(names, name)
49+
fmt.Println("Remove:", name)
50+
continue
51+
}
52+
53+
totps, err := totp.ListNames(db)
54+
if err != nil {
55+
return err
56+
}
57+
58+
for _, c := range totps {
59+
if strings.HasPrefix(c, name) {
60+
names = append(names, c)
61+
fmt.Println("Remove:", c)
62+
}
63+
}
4164
}
4265

43-
fmt.Printf("\n%q TOTP removed\n", name)
44-
return nil
66+
return totp.Remove(db, names...)
4567
}
4668
}

commands/2fa/rm/rm_test.go

+47-19
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,90 @@ import (
1414
func TestRm(t *testing.T) {
1515
db := cmdutil.SetContext(t)
1616

17-
err := totp.Create(db, &pb.TOTP{Name: "test"})
18-
assert.NoError(t, err)
17+
names := []string{"test", "directory/test", "kure", "atoll"}
18+
for _, name := range names {
19+
err := totp.Create(db, &pb.TOTP{Name: name})
20+
assert.NoErrorf(t, err, "Failed creating %q", name)
21+
}
1922

2023
cases := []struct {
21-
desc string
22-
name string
23-
confirmation string
24+
desc string
25+
input string
26+
names []string
2427
}{
2528
{
26-
desc: "Abort",
27-
name: "test",
28-
confirmation: "n",
29+
desc: "Do not proceed",
30+
names: []string{"test"},
31+
input: "n",
2932
},
3033
{
31-
desc: "Proceed",
32-
name: "test",
33-
confirmation: "y",
34+
desc: "Remove one entry",
35+
names: []string{"test"},
36+
input: "y",
37+
},
38+
{
39+
desc: "Remove multiple entries",
40+
names: []string{"kure", "atoll"},
41+
input: "y",
42+
},
43+
{
44+
desc: "Remove directory",
45+
names: []string{"directory/"},
46+
input: "y",
3447
},
3548
}
3649

3750
for _, tc := range cases {
3851
t.Run(tc.desc, func(t *testing.T) {
39-
buf := bytes.NewBufferString(tc.confirmation)
52+
buf := bytes.NewBufferString(tc.input)
4053
cmd := NewCmd(db, buf)
41-
cmd.SetArgs([]string{tc.name})
54+
cmd.SetArgs(tc.names)
4255

4356
err := cmd.Execute()
44-
assert.NoError(t, err, "Failed removing the TOTP")
57+
assert.NoError(t, err)
58+
59+
if tc.input == "y" {
60+
for _, name := range tc.names {
61+
_, err := totp.Get(db, name)
62+
assert.Error(t, err)
63+
}
64+
}
4565
})
4666
}
4767
}
4868

4969
func TestRmErrors(t *testing.T) {
5070
db := cmdutil.SetContext(t)
5171

72+
name := "random"
73+
err := totp.Create(db, &pb.TOTP{Name: name})
74+
assert.NoErrorf(t, err, "Failed creating %q", name)
75+
5276
cases := []struct {
5377
desc string
54-
name string
5578
confirmation string
79+
names []string
5680
}{
5781
{
58-
desc: "Invalid name",
59-
name: "",
82+
desc: "Invalid name",
83+
names: []string{""},
6084
},
6185
{
6286
desc: "Does not exists",
63-
name: "non-existent",
87+
names: []string{"non-existent"},
6488
confirmation: "y",
6589
},
90+
{
91+
desc: "Second name does not exist",
92+
names: []string{"random", "non-existent"},
93+
},
6694
}
6795

6896
for _, tc := range cases {
6997
t.Run(tc.desc, func(t *testing.T) {
7098
buf := bytes.NewBufferString(tc.confirmation)
7199
cmd := NewCmd(db, buf)
72-
cmd.SetArgs([]string{tc.name})
100+
cmd.SetArgs(tc.names)
73101

74102
err := cmd.Execute()
75103
assert.Error(t, err)

commands/backup/backup.go

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func NewCmd(db *bolt.DB) *cobra.Command {
5757
f.StringVar(&opts.path, "path", "", "destination file path")
5858
f.Uint16Var(&opts.port, "port", 8080, "server port")
5959

60+
cmd.MarkFlagsMutuallyExclusive("http", "path")
61+
6062
return cmd
6163
}
6264

commands/backup/backup_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ func TestBackupErrors(t *testing.T) {
8787
desc: "Mkdir error",
8888
path: "backup.go/",
8989
},
90+
{
91+
desc: "Mutually exclusive flags",
92+
http: "true",
93+
path: "backup.db",
94+
},
9095
}
9196

9297
cmd := NewCmd(db)

commands/card/ls/ls.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ls
33
import (
44
"errors"
55
"fmt"
6-
"path/filepath"
6+
"regexp"
77
"strings"
88

99
cmdutil "github.com/GGP1/kure/commands"
@@ -79,7 +79,7 @@ func runLs(db *bolt.DB, opts *lsOptions) cmdutil.RunEFunc {
7979

8080
var matches []string
8181
for _, card := range cards {
82-
matched, err := filepath.Match(name, card)
82+
matched, err := regexp.MatchString(name, card)
8383
if err != nil {
8484
return err
8585
}

commands/card/rm/rm.go

+23-26
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ const example = `
1818
kure card rm Sample
1919
2020
* Remove a directory
21-
kure card rm SampleDir/`
21+
kure card rm SampleDir/
22+
23+
* Remove multiple cards
24+
kure card rm Sample Sample2 Sample3`
2225

2326
// NewCmd returns the a new command.
2427
func NewCmd(db *bolt.DB, r io.Reader) *cobra.Command {
2528
return &cobra.Command{
26-
Use: "rm <name>",
27-
Short: "Remove a card or directory",
29+
Use: "rm <names>",
30+
Short: "Remove cards or directories",
2831
Example: example,
2932
Args: cmdutil.MustExist(db, cmdutil.Card, true),
3033
RunE: runRm(db, r),
@@ -33,39 +36,33 @@ func NewCmd(db *bolt.DB, r io.Reader) *cobra.Command {
3336

3437
func runRm(db *bolt.DB, r io.Reader) cmdutil.RunEFunc {
3538
return func(cmd *cobra.Command, args []string) error {
36-
name := strings.Join(args, " ")
37-
name = cmdutil.NormalizeName(name, true)
38-
3939
if !terminal.Confirm(r, "Are you sure you want to proceed?") {
4040
return nil
4141
}
4242

43-
// Remove single file
44-
if !strings.HasSuffix(name, "/") {
45-
if err := card.Remove(db, name); err != nil {
46-
return err
47-
}
43+
names := make([]string, 0, len(args))
44+
for _, name := range args {
45+
name = cmdutil.NormalizeName(name, true)
4846

49-
fmt.Printf("\n%q removed\n", name)
50-
return nil
51-
}
52-
53-
fmt.Printf("Removing %q directory...\n", name)
47+
if !strings.HasSuffix(name, "/") {
48+
names = append(names, name)
49+
fmt.Println("Remove:", name)
50+
continue
51+
}
5452

55-
cards, err := card.ListNames(db)
56-
if err != nil {
57-
return err
58-
}
53+
cards, err := card.ListNames(db)
54+
if err != nil {
55+
return err
56+
}
5957

60-
for _, c := range cards {
61-
if strings.HasPrefix(c, name) {
62-
if err := card.Remove(db, c); err != nil {
63-
return err
58+
for _, c := range cards {
59+
if strings.HasPrefix(c, name) {
60+
names = append(names, c)
61+
fmt.Println("Remove:", c)
6462
}
65-
fmt.Println("Remove:", c)
6663
}
6764
}
6865

69-
return nil
66+
return card.Remove(db, names...)
7067
}
7168
}

0 commit comments

Comments
 (0)