mirror of
https://github.com/gechandesu/pwd.git
synced 2026-03-25 21:13:14 +03:00
all: rename functions, deprecate old ones
Rename get_by_* to user_by_* and get_all to users. users() now can return errors.
This commit is contained in:
26
pwd.c.v
26
pwd.c.v
@@ -76,7 +76,14 @@ fn make_error(name string, uid int) IError {
|
|||||||
|
|
||||||
// get_by_uid returns the passwd database entry by user ID.
|
// get_by_uid returns the passwd database entry by user ID.
|
||||||
// If the entry is not found, the EntryNotFoundError error will be returned.
|
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||||
|
@[deprecated: 'use user_by_uid() instead']
|
||||||
pub fn get_by_uid(uid int) !Passwd {
|
pub fn get_by_uid(uid int) !Passwd {
|
||||||
|
return user_by_uid(uid)!
|
||||||
|
}
|
||||||
|
|
||||||
|
// user_by_uid returns the passwd database entry by user ID.
|
||||||
|
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||||
|
pub fn user_by_uid(uid int) !Passwd {
|
||||||
pwd_mutex.lock()
|
pwd_mutex.lock()
|
||||||
defer {
|
defer {
|
||||||
pwd_mutex.unlock()
|
pwd_mutex.unlock()
|
||||||
@@ -88,9 +95,16 @@ pub fn get_by_uid(uid int) !Passwd {
|
|||||||
return make_passwd(pw)
|
return make_passwd(pw)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get_by_uid returns the passwd database entry by user name.
|
// get_by_name returns the passwd database entry by user name.
|
||||||
// If the entry is not found, the EntryNotFoundError error will be returned.
|
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||||
|
@[deprecated: 'use user_by_name() instead']
|
||||||
pub fn get_by_name(name string) !Passwd {
|
pub fn get_by_name(name string) !Passwd {
|
||||||
|
return user_by_name(name)!
|
||||||
|
}
|
||||||
|
|
||||||
|
// user_by_name returns the passwd database entry by user name.
|
||||||
|
// If the entry is not found, the EntryNotFoundError error will be returned.
|
||||||
|
pub fn user_by_name(name string) !Passwd {
|
||||||
pwd_mutex.lock()
|
pwd_mutex.lock()
|
||||||
defer {
|
defer {
|
||||||
pwd_mutex.unlock()
|
pwd_mutex.unlock()
|
||||||
@@ -103,7 +117,13 @@ pub fn get_by_name(name string) !Passwd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get_all returns all entries from passwd database in arbitrary order.
|
// get_all returns all entries from passwd database in arbitrary order.
|
||||||
|
@[deprecated: 'use users() instead']
|
||||||
pub fn get_all() []Passwd {
|
pub fn get_all() []Passwd {
|
||||||
|
return users() or { []Passwd{} }
|
||||||
|
}
|
||||||
|
|
||||||
|
// users returns all entries from passwd database in arbitrary order.
|
||||||
|
pub fn users() ![]Passwd {
|
||||||
mut pwds := []Passwd{}
|
mut pwds := []Passwd{}
|
||||||
pwd_mutex.lock()
|
pwd_mutex.lock()
|
||||||
C.setpwent()
|
C.setpwent()
|
||||||
@@ -113,6 +133,10 @@ pub fn get_all() []Passwd {
|
|||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
pw := C.getpwent()
|
pw := C.getpwent()
|
||||||
|
err := make_error('', -1)
|
||||||
|
if err.code() != 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if isnil(pw) {
|
if isnil(pw) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
12
pwd_test.v
12
pwd_test.v
@@ -1,17 +1,17 @@
|
|||||||
// vtest build: !windows
|
// vtest build: !windows
|
||||||
import pwd
|
import pwd
|
||||||
|
|
||||||
fn test_get_by_uid() {
|
fn test_user_by_uid() {
|
||||||
pw := pwd.get_by_uid(0)!
|
pw := pwd.user_by_uid(0)!
|
||||||
assert pw.name == 'root'
|
assert pw.name == 'root'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_get_by_name() {
|
fn test_user_by_name() {
|
||||||
pw := pwd.get_by_name('root')!
|
pw := pwd.user_by_name('root')!
|
||||||
assert pw.uid == 0
|
assert pw.uid == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_get_all() {
|
fn test_users() {
|
||||||
pws := pwd.get_all()
|
pws := pwd.users()!
|
||||||
assert pws.len > 0
|
assert pws.len > 0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user