Splitting passwd.cr into several files.

This commit is contained in:
Luka Vandervelden 2019-11-17 18:44:01 +01:00
parent 59bb8c4d0e
commit 02c12703dd
3 changed files with 98 additions and 88 deletions

24
src/group.cr Normal file
View File

@ -0,0 +1,24 @@
require "csv"
class Passwd::Group
getter name : String
getter password_hash : String
getter gid : Int32
getter users = Array(String).new
def initialize(@name, @password_hash, @gid, @users = [] of String)
end
def initialize(line : Array(String))
@name = line[0]
@password_hash = line[1]
@gid = line[2].to_i
@users = line[3].split ","
end
def to_csv
[@name, @password_hash, @gid, @users.join ","].join ":"
end
end

View File

@ -5,6 +5,12 @@ require "file_utils"
# FIXME: Use split, not CSV.
# FIXME: Prevent using ':' in fields.
class Passwd
end
require "./user.cr"
require "./group.cr"
class Passwd
@passwd : String
@group : String
@ -240,91 +246,3 @@ class Passwd
end
end
class Passwd::Group
getter name : String
getter password_hash : String
getter gid : Int32
getter users = Array(String).new
def initialize(@name, @password_hash, @gid, @users = [] of String)
end
def initialize(line : Array(String))
@name = line[0]
@password_hash = line[1]
@gid = line[2].to_i
@users = line[3].split ","
end
def to_csv
[@name, @password_hash, @gid, @users.join ","].join ":"
end
end
class Passwd::User
getter uid : Int32
getter gid : Int32
getter login : String
getter password_hash : String
getter home : String = "/"
getter shell : String = "/bin/nologin"
getter groups = Array(String).new
getter full_name : String? = nil
getter location : String? = nil
getter office_phone_number : String? = nil
getter home_phone_number : String? = nil
getter other_contact : String? = nil
def initialize(
@login,
@password_hash,
@uid,
@gid,
@home = "",
@shell = "",
@full_name = nil,
@location = nil,
@office_phone_number = nil,
@home_phone_number = nil,
@other_contact = nil
)
end
# Caution: will raise on invalid entries.
def initialize(line : Array(String))
@login = line[0]
@password_hash = line[1]
@uid = line[2].to_i
@gid = line[3].to_i
CSV.parse(line[4], separator: ',')[0]?.try do |gecos|
@full_name = gecos[0]?
@location = gecos[1]?
@office_phone_number = gecos[2]?
@home_phone_number = gecos[3]?
@other_contact = gecos[4]?
end
@home = line[5]
@shell = line[6]
end
def to_csv
[@login, @password_hash, @uid, @gid, gecos, @home, @shell].join ":"
end
def gecos
unless @location || @office_phone_number || @home_phone_number || @other_contact
if @full_name
return @full_name
else
return ""
end
end
[@full_name || "", @location || "", @office_phone_number || "", @home_phone_number || "", @other_contact || ""].join ","
end
end

68
src/user.cr Normal file
View File

@ -0,0 +1,68 @@
require "csv"
class Passwd::User
getter uid : Int32
getter gid : Int32
getter login : String
getter password_hash : String
getter home : String = "/"
getter shell : String = "/bin/nologin"
getter groups = Array(String).new
getter full_name : String? = nil
getter location : String? = nil
getter office_phone_number : String? = nil
getter home_phone_number : String? = nil
getter other_contact : String? = nil
def initialize(
@login,
@password_hash,
@uid,
@gid,
@home = "",
@shell = "",
@full_name = nil,
@location = nil,
@office_phone_number = nil,
@home_phone_number = nil,
@other_contact = nil
)
end
# Caution: will raise on invalid entries.
def initialize(line : Array(String))
@login = line[0]
@password_hash = line[1]
@uid = line[2].to_i
@gid = line[3].to_i
CSV.parse(line[4], separator: ',')[0]?.try do |gecos|
@full_name = gecos[0]?
@location = gecos[1]?
@office_phone_number = gecos[2]?
@home_phone_number = gecos[3]?
@other_contact = gecos[4]?
end
@home = line[5]
@shell = line[6]
end
def to_csv
[@login, @password_hash, @uid, @gid, gecos, @home, @shell].join ":"
end
def gecos
unless @location || @office_phone_number || @home_phone_number || @other_contact
if @full_name
return @full_name
else
return ""
end
end
[@full_name || "", @location || "", @office_phone_number || "", @home_phone_number || "", @other_contact || ""].join ","
end
end