diff --git a/src/group.cr b/src/group.cr new file mode 100644 index 0000000..5ff3ee7 --- /dev/null +++ b/src/group.cr @@ -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 + diff --git a/src/passwd.cr b/src/passwd.cr index 947e760..8e5b2e6 100644 --- a/src/passwd.cr +++ b/src/passwd.cr @@ -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 - diff --git a/src/user.cr b/src/user.cr new file mode 100644 index 0000000..00c4039 --- /dev/null +++ b/src/user.cr @@ -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 +