Skip to content

Commit 1f3c161

Browse files
committed
Base
1 parent 02ff8b7 commit 1f3c161

File tree

12 files changed

+237
-152
lines changed

12 files changed

+237
-152
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ go.work
2222

2323
# bin folder
2424
bin
25+
# coverage folder
2526
coverage

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ linters:
44
- varnamelen
55
- funlen
66
- cyclop
7+
- gocyclo
78
- paralleltest
89
- gochecknoglobals
910
- wsl
1011
- goerr113
1112
- gocognit
12-
- wrapcheck
1313
- dupl
1414

1515
issues:

Dockerfile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
FROM archlinux:latest
22

3-
RUN pacman -Syu --noconfirm
4-
RUN pacman -Syu go --noconfirm
3+
RUN pacman -Sy go --needed --noconfirm
54

65
COPY . /root/vault
76

87
WORKDIR /root/vault
98

10-
RUN go run github.com/go-task/task/v3/cmd/task@latest build
9+
RUN go build -o /usr/local/bin/vault cmd/vault/*.go
1110

12-
ENTRYPOINT ["./bin/vault"]
11+
RUN go clean -r -modcache -cache -i -x -testcache -fuzzcache
12+
13+
WORKDIR /root
14+
15+
RUN rm -rf /root/vault
16+
RUN rm -rf /root/go
17+
18+
RUN pacman -Rnsu go --noconfirm
19+
RUN pacman -Scc
20+
21+
ENTRYPOINT ["vault"]
1322

1423
CMD ["-help"]

Taskfile.yml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ vars:
66
GREETING: Hi!
77
EXECUTABLE_NAME: vault
88
DOCKER_IMAGE_NAME: vault
9-
VAULT_DIR: /home/zeltron/.vault
9+
VAULT_DIR: $HOME/.vault
1010
DOCKER_MOUNT_PATH: /root/.vault
1111

1212
tasks:
@@ -15,6 +15,10 @@ tasks:
1515
- go fmt ./...
1616
- gofumpt -e -l -w -extra .
1717

18+
lint:
19+
cmds:
20+
- golangci-lint run --sort-results
21+
1822
dockerbuild:
1923
cmds:
2024
- docker build -t {{.DOCKER_IMAGE_NAME}} .
@@ -23,34 +27,27 @@ tasks:
2327
cmds:
2428
- docker run -v {{.VAULT_DIR}}:{{.DOCKER_MOUNT_PATH}} -it {{.DOCKER_IMAGE_NAME}} {{.CLI_ARGS}}
2529

26-
lint:
27-
cmds:
28-
- staticcheck -checks all ./...
29-
- golangci-lint run --sort-results
30-
3130
coverprofile:
3231
cmds:
32+
- mkdir -p coverage
3333
- go test -cover -coverprofile=coverage/cover.out ./...
3434

3535
showcoverage:
3636
cmds:
3737
- go tool cover -html coverage/cover.out
3838

39-
vault-help:
40-
cmds:
41-
- go run cmd/vault/* -help
42-
4339
docs:
4440
cmds:
4541
- go run golang.org/x/pkgsite/cmd/pkgsite@latest
4642

4743
build:
4844
cmds:
4945
- go generate ./...
46+
- mkdir -p bin
5047
- rm -rf bin/*
51-
- GOOS=linux GOARCH=amd64 go build -o bin/{{.EXECUTABLE_NAME}}-linux cmd/vault/*
52-
- GOOS=windows GOARCH=amd64 go build -o bin/{{.EXECUTABLE_NAME}}-windows cmd/vault/*
53-
- GOOS=darwin GOARCH=amd64 go build -o bin/{{.EXECUTABLE_NAME}}-darwin cmd/vault/*
48+
- GOOS=linux GOARCH=amd64 go build -o bin/{{.EXECUTABLE_NAME}}-linux cmd/vault/*.go
49+
- GOOS=windows GOARCH=amd64 go build -o bin/{{.EXECUTABLE_NAME}}-windows cmd/vault/*.go
50+
- GOOS=darwin GOARCH=amd64 go build -o bin/{{.EXECUTABLE_NAME}}-darwin cmd/vault/*.go
5451
- zip -r -j bin/vault.zip bin/*
5552

5653
buildrun:
@@ -60,7 +57,7 @@ tasks:
6057
run:
6158
cmds:
6259
- go generate ./...
63-
- go run cmd/vault/* {{.CLI_ARGS}}
60+
- go run cmd/vault/*.go {{.CLI_ARGS}}
6461

6562
test:
6663
cmds:

cmd/vault/README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@ With proper go installation, run the command `go install -v github.com/231tr0n/v
99

1010
## Usage
1111
Use the flag `-help` for knowing all the options of the vault.
12-
```
13-
-change
14-
Changes the vault password
15-
-clear
16-
Clears all the passwords in the vault
17-
-delete string
18-
Deletes the password in the vault
19-
-generate int
20-
Generates a new random password of length given
21-
-get string
22-
Gets the password from the vault
23-
-list
24-
Lists all the passwords in the vault
25-
-put string
26-
Puts the password in the vault
27-
```
2812
**Note:** You have to set the password using the `-change` argument initially since it is not set. Give an empty password when prompted for vault's old password.
2913

3014
## Backup

cmd/vault/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
fmt.Println(err)
1717
//nolint
1818
fmt.Println("-----------------")
19-
os.Exit(1)
19+
os.Exit(0)
2020
}
2121

2222
err = cli.Parse()
@@ -27,6 +27,6 @@ func main() {
2727
fmt.Println(err)
2828
//nolint
2929
fmt.Println("-----------------")
30-
os.Exit(1)
30+
os.Exit(0)
3131
}
3232
}

internal/cli/cli.go

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
_ "bufio"
66
"flag"
77
"fmt"
8+
89
// Used by the commented code.
910
_ "os"
1011
"syscall"
@@ -18,7 +19,7 @@ import (
1819

1920
// Init initlialises the passwdstore.
2021
func Init() error {
21-
return errorwrap.ErrWrap(passwdstore.Init(config.GetPasswdStoreFilePath()))
22+
return errorwrap.Wrap(passwdstore.Init(config.GetPasswdStoreFilePath()))
2223
}
2324

2425
func readSecureInput(c string) ([]byte, error) {
@@ -35,18 +36,23 @@ func readSecureInput(c string) ([]byte, error) {
3536
//nolint
3637
s, err := term.ReadPassword(int(syscall.Stdin))
3738

38-
return s, errorwrap.ErrWrap(err)
39+
//nolint
40+
fmt.Println()
41+
42+
return s, errorwrap.Wrap(err)
3943
}
4044

4145
// Parse parses the command line arguments and runs the respective functions accordingly.
4246
func Parse() error {
43-
change := flag.Bool("change", false, "Changes the vault password")
44-
list := flag.Bool("list", false, "Lists all the passwords in the vault")
45-
clear := flag.Bool("clear", false, "Clears all the passwords in the vault")
46-
get := flag.String("get", "", "Gets the password from the vault")
47-
put := flag.String("put", "", "Puts the password in the vault")
48-
del := flag.String("delete", "", "Deletes the password in the vault")
49-
generate := flag.Int("generate", 0, "Generates a new random password of length given")
47+
change := flag.Bool("change", false, "Changes the vault password.")
48+
list := flag.Bool("list", false, "Lists all the password names in the vault.")
49+
listAll := flag.Bool("list-all", false, "Lists all the passwords with names(dangerous) in the vault.")
50+
clear := flag.Bool("clear", false, "Clears all the passwords in the vault.")
51+
get := flag.String("get", "", "Gets the password from the vault.")
52+
put := flag.String("put", "", "Puts the password in the vault.")
53+
del := flag.String("delete", "", "Deletes the password in the vault.")
54+
//nolint
55+
generate := flag.Int("generate", 0, "Generates a new random password of length given. If this flag is passed along with put flag, it generates a random password and stores that in the vault.")
5056

5157
flag.Parse()
5258

@@ -62,11 +68,11 @@ switch1:
6268
case *clear:
6369
pwd, err := readSecureInput("Enter vault password: ")
6470
if err != nil {
65-
return errorwrap.ErrWrap(err)
71+
return errorwrap.Wrap(err)
6672
}
6773
err = passwdstore.Clear(pwd)
6874
if err != nil {
69-
return errorwrap.ErrWrap(err)
75+
return errorwrap.Wrap(err)
7076
}
7177

7278
//nolint
@@ -77,47 +83,70 @@ switch1:
7783
case *change:
7884
oldPwd, err := readSecureInput("Enter old vault password: ")
7985
if err != nil {
80-
return errorwrap.ErrWrap(err)
86+
return errorwrap.Wrap(err)
8187
}
8288

8389
newPwd, err := readSecureInput("Enter new vault password: ")
8490
if err != nil {
85-
return errorwrap.ErrWrap(err)
91+
return errorwrap.Wrap(err)
8692
}
8793

88-
newPwdCheck, err := readSecureInput("Enter new vault password once again: ")
94+
newPwdCheck, err := readSecureInput("Re-Enter new vault password: ")
8995
if err != nil {
90-
return errorwrap.ErrWrap(err)
96+
return errorwrap.Wrap(err)
9197
}
9298

9399
if string(newPwd) != string(newPwdCheck) {
94100
//nolint
95101
fmt.Println("-----------------")
96102
//nolint
97-
fmt.Println("Passwords don't match")
103+
fmt.Println("New vault passwords don't match")
98104

99105
break switch1
100106
}
101107

102108
err = passwdstore.ChangePasswd(newPwd, oldPwd)
103109
if err != nil {
104-
return errorwrap.ErrWrap(err)
110+
return errorwrap.Wrap(err)
105111
}
106112

107113
//nolint
108114
fmt.Println("-----------------")
109115
//nolint
110116
fmt.Println("Vault password changed")
111117

118+
case *listAll:
119+
pwd, err := readSecureInput("Enter vault password: ")
120+
if err != nil {
121+
return errorwrap.Wrap(err)
122+
}
123+
124+
list, err := passwdstore.ListEntries(pwd)
125+
if err != nil {
126+
return errorwrap.Wrap(err)
127+
}
128+
129+
//nolint
130+
fmt.Println("-----------------")
131+
//nolint
132+
fmt.Println("List of passwords")
133+
//nolint
134+
fmt.Println("-----------------")
135+
136+
for i, val := range list {
137+
//nolint
138+
fmt.Println(i, val[0], val[1])
139+
}
140+
112141
case *list:
113142
pwd, err := readSecureInput("Enter vault password: ")
114143
if err != nil {
115-
return errorwrap.ErrWrap(err)
144+
return errorwrap.Wrap(err)
116145
}
117146

118-
list, err := passwdstore.List(pwd)
147+
list, err := passwdstore.ListKeys(pwd)
119148
if err != nil {
120-
return errorwrap.ErrWrap(err)
149+
return errorwrap.Wrap(err)
121150
}
122151

123152
//nolint
@@ -135,12 +164,12 @@ switch1:
135164
case *get != "":
136165
pwd, err := readSecureInput("Enter vault password: ")
137166
if err != nil {
138-
return errorwrap.ErrWrap(err)
167+
return errorwrap.Wrap(err)
139168
}
140169

141170
value, err := passwdstore.Get(*get, pwd)
142171
if err != nil {
143-
return errorwrap.ErrWrap(err)
172+
return errorwrap.Wrap(err)
144173
}
145174

146175
//nolint
@@ -149,19 +178,32 @@ switch1:
149178
fmt.Println("Password:", value)
150179

151180
case *put != "":
152-
value, err := readSecureInput("Enter password for " + *put + ": ")
181+
pwd, err := readSecureInput("Enter vault password: ")
153182
if err != nil {
154-
return errorwrap.ErrWrap(err)
183+
return errorwrap.Wrap(err)
155184
}
156185

157-
pwd, err := readSecureInput("Enter vault password: ")
158-
if err != nil {
159-
return errorwrap.ErrWrap(err)
186+
var value []byte
187+
188+
if *generate > 0 {
189+
value, err = crypto.Generate(*generate)
190+
if err != nil {
191+
return errorwrap.Wrap(err)
192+
}
193+
//nolint
194+
fmt.Println("-----------------")
195+
//nolint
196+
fmt.Println("Generated password:", string(value))
197+
} else {
198+
value, err = readSecureInput("Enter password for '" + *put + "': ")
199+
if err != nil {
200+
return errorwrap.Wrap(err)
201+
}
160202
}
161203

162204
err = passwdstore.Put(*put, string(value), pwd)
163205
if err != nil {
164-
return errorwrap.ErrWrap(err)
206+
return errorwrap.Wrap(err)
165207
}
166208

167209
//nolint
@@ -172,12 +214,12 @@ switch1:
172214
case *del != "":
173215
pwd, err := readSecureInput("Enter vault password: ")
174216
if err != nil {
175-
return errorwrap.ErrWrap(err)
217+
return errorwrap.Wrap(err)
176218
}
177219

178220
err = passwdstore.Delete(*del, pwd)
179221
if err != nil {
180-
return errorwrap.ErrWrap(err)
222+
return errorwrap.Wrap(err)
181223
}
182224

183225
//nolint
@@ -188,7 +230,7 @@ switch1:
188230
case *generate > 0:
189231
pwd, err := crypto.Generate(*generate)
190232
if err != nil {
191-
return errorwrap.ErrWrap(err)
233+
return errorwrap.Wrap(err)
192234
}
193235

194236
//nolint

0 commit comments

Comments
 (0)