/
cmdline.go
120 lines (102 loc) · 3.67 KB
/
cmdline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright © 2019, 2020 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <support@mwat.de>
*/
package nele
//lint:file-ignore ST1017 - I prefer Yoda conditions
/*
* This file provides functions to add postings from the commandline
* and maintain the user/password list.
*/
import (
"bufio"
"io"
"io/ioutil"
"os"
"github.com/mwat56/passlist"
)
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// `addMarkdown()` saves `aMarkdown` as a new posting,
// returning the number of bytes written and a possible I/O error.
//
// `aMarkdown` The text to store as a new posting.
func addMarkdown(aMarkdown []byte) (int, error) {
return NewPosting("").Set(aMarkdown).Store()
} // addMarkdown()
// AddConsolePost reads data from `StdIn` and saves it as a new posting,
// returning the number of bytes written and a possible I/O error.
func AddConsolePost() (int, error) {
markdown, err := bufio.NewReader(os.Stdin).ReadBytes(0x03)
if (nil != err) && (io.EOF != err) {
return 0, err
}
return addMarkdown(markdown)
} // AddConsolePost()
// AddFilePost reads `aFilename` and adds it as a new posting,
// returning the number of bytes written and a possible I/O error.
//
// `aFilename` The text file to add as a new posting.
func AddFilePost(aFilename string) (int, error) {
markdown, err := ioutil.ReadFile(aFilename) /* #nosec G304 */
if nil != err {
return 0, err
}
return addMarkdown(markdown)
} // AddFilePost()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// UserAdd reads a password for `aUser` from the commandline
// and adds it to `aFilename`.
//
// NOTE: This function does not return but terminates the program
// with error code `0` (zero) if successful, or `1` (one) otherwise.
//
// `aUser` The username to add to the password file.
// `aFilename` The name of the password file to use.
func UserAdd(aUser, aFilename string) {
passlist.AddUser(aUser, aFilename)
} // UserAdd()
// UserCheck reads a password for `aUser` from the commandline
// and compares it with the one stored in `aFilename`.
//
// NOTE: This function does not return but terminates the program
// with error code `0` (zero) if successful, or `1` (one) otherwise.
//
// `aUser` The username to check in the password file.
// `aFilename` The name of the password file to use.
func UserCheck(aUser, aFilename string) {
passlist.CheckUser(aUser, aFilename)
} // UserCheck()
// UserDelete removes the entry for `aUser` from the password
// list `aFilename`.
//
// NOTE: This function does not return but terminates the program
// with error code `0` (zero) if successful, or `1` (one) otherwise.
//
// `aUser` The username to remove from the password file.
// `aFilename` The name of the password file to use.
func UserDelete(aUser, aFilename string) {
passlist.DeleteUser(aUser, aFilename)
} // UserDelete()
// UserList reads `aFilename` and lists all users stored in there.
//
// NOTE: This function does not return but terminates the program
// with error code `0` (zero) if successful, or `1` (one) otherwise.
//
// `aFilename` The name of the password file to use.
func UserList(aFilename string) {
passlist.ListUsers(aFilename)
} // UserList()
// UserUpdate reads a password for `aUser` from the commandline
// and updates the entry in the password list `aFilename`.
//
// NOTE: This function does not return but terminates the program
// with error code `0` (zero) if successful, or `1` (one) otherwise.
//
// `aUser` The username to remove from the password file.
//
// `aFilename` The name of the password file to use.
func UserUpdate(aUser, aFilename string) {
passlist.UpdateUser(aUser, aFilename)
} // UserUpdate()
/* _EoF_ */