@@ -17,6 +17,7 @@ package login1
1717
1818import (
1919 "context"
20+ "errors"
2021 "fmt"
2122 "os"
2223 "strconv"
@@ -111,54 +112,52 @@ type User struct {
111112 Path dbus.ObjectPath
112113}
113114
114- func sessionFromInterfaces (session []any ) ( * Session , error ) {
115+ func sessionFromInterfaces (session []any ) * Session {
115116 if len (session ) < 5 {
116- return nil , fmt . Errorf ( "invalid number of session fields: %d" , len ( session ))
117+ return nil
117118 }
118119 id , ok := session [0 ].(string )
119120 if ! ok {
120- return nil , fmt . Errorf ( "failed to typecast session field 0 to string" )
121+ return nil
121122 }
122123 uid , ok := session [1 ].(uint32 )
123124 if ! ok {
124- return nil , fmt . Errorf ( "failed to typecast session field 1 to uint32" )
125+ return nil
125126 }
126127 user , ok := session [2 ].(string )
127128 if ! ok {
128- return nil , fmt . Errorf ( "failed to typecast session field 2 to string" )
129+ return nil
129130 }
130131 seat , ok := session [3 ].(string )
131132 if ! ok {
132- return nil , fmt . Errorf ( "failed to typecast session field 2 to string" )
133+ return nil
133134 }
134135 path , ok := session [4 ].(dbus.ObjectPath )
135136 if ! ok {
136- return nil , fmt . Errorf ( "failed to typecast session field 4 to ObjectPath" )
137+ return nil
137138 }
138139
139- ret := Session {ID : id , UID : uid , User : user , Seat : seat , Path : path }
140- return & ret , nil
140+ return & Session {ID : id , UID : uid , User : user , Seat : seat , Path : path }
141141}
142142
143- func userFromInterfaces (user []any ) ( * User , error ) {
143+ func userFromInterfaces (user []any ) * User {
144144 if len (user ) < 3 {
145- return nil , fmt . Errorf ( "invalid number of user fields: %d" , len ( user ))
145+ return nil
146146 }
147147 uid , ok := user [0 ].(uint32 )
148148 if ! ok {
149- return nil , fmt . Errorf ( "failed to typecast user field 0 to uint32" )
149+ return nil
150150 }
151151 name , ok := user [1 ].(string )
152152 if ! ok {
153- return nil , fmt . Errorf ( "failed to typecast session field 1 to string" )
153+ return nil
154154 }
155155 path , ok := user [2 ].(dbus.ObjectPath )
156156 if ! ok {
157- return nil , fmt . Errorf ( "failed to typecast user field 2 to ObjectPath" )
157+ return nil
158158 }
159159
160- ret := User {UID : uid , Name : name , Path : path }
161- return & ret , nil
160+ return & User {UID : uid , Name : name , Path : path }
162161}
163162
164163// GetActiveSession may be used to get the session object path for the current active session
@@ -175,20 +174,20 @@ func (c *Conn) GetActiveSession() (dbus.ObjectPath, error) {
175174 }
176175 activeSessionMap , ok := activeSession .Value ().([]any )
177176 if ! ok || len (activeSessionMap ) < 2 {
178- return "" , fmt .Errorf ("failed to typecast active session map" )
177+ return "" , fmt .Errorf ("GetActiveSession: can't parse response %+v" , activeSession )
179178 }
180179
181180 activeSessionPath , ok := activeSessionMap [1 ].(dbus.ObjectPath )
182181 if ! ok {
183- return "" , fmt .Errorf ("failed to typecast dbus active session Path" )
182+ return "" , fmt .Errorf ("GetActiveSession: can't parse response %+v" , activeSessionMap )
184183 }
185184 return activeSessionPath , nil
186185}
187186
188187// GetSessionUser may be used to get the user of specific session
189188func (c * Conn ) GetSessionUser (sessionPath dbus.ObjectPath ) (* User , error ) {
190189 if len (sessionPath ) == 0 {
191- return nil , fmt . Errorf ("empty sessionPath" )
190+ return nil , errors . New ("empty sessionPath" )
192191 }
193192
194193 activeSessionObj := c .conn .Object (dbusDest , sessionPath )
@@ -203,19 +202,19 @@ func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
203202 }
204203 dbusUser , ok := sessionUser .Value ().([]any )
205204 if ! ok {
206- return nil , fmt .Errorf ("failed to typecast dbus session user" )
205+ return nil , fmt .Errorf ("GetSessionUser: can't parse response %+v" , sessionUser )
207206 }
208207
209208 if len (dbusUser ) < 2 {
210- return nil , fmt .Errorf ("invalid number of user fields: %d " , len ( dbusUser ) )
209+ return nil , fmt .Errorf ("GetSessionUser: can't parse response %+v " , dbusUser )
211210 }
212211 uid , ok := dbusUser [0 ].(uint32 )
213212 if ! ok {
214- return nil , fmt .Errorf ("failed to typecast user field 0 to uint32" )
213+ return nil , fmt .Errorf ("GetSessionUser: can't parse response %+v" , dbusUser )
215214 }
216215 path , ok := dbusUser [1 ].(dbus.ObjectPath )
217216 if ! ok {
218- return nil , fmt .Errorf ("failed to typecast user field 1 to ObjectPath" )
217+ return nil , fmt .Errorf ("GetSessionUser: can't parse response %+v" , dbusUser )
219218 }
220219
221220 user := User {UID : uid , Name : strings .Trim (sessionUserName .String (), "\" " ), Path : path }
@@ -226,7 +225,7 @@ func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
226225// GetSessionDisplay may be used to get the display for specific session
227226func (c * Conn ) GetSessionDisplay (sessionPath dbus.ObjectPath ) (string , error ) {
228227 if len (sessionPath ) == 0 {
229- return "" , fmt . Errorf ("empty sessionPath" )
228+ return "" , errors . New ("empty sessionPath" )
230229 }
231230 sessionObj := c .conn .Object (dbusDest , sessionPath )
232231 display , err := sessionObj .GetProperty (dbusDest + ".Session.Display" )
@@ -246,7 +245,7 @@ func (c *Conn) GetSession(id string) (dbus.ObjectPath, error) {
246245
247246 ret , ok := out .(dbus.ObjectPath )
248247 if ! ok {
249- return "" , fmt .Errorf ("failed to typecast session to ObjectPath" )
248+ return "" , fmt .Errorf ("GetSession: can't parse response %+v" , out )
250249 }
251250
252251 return ret , nil
@@ -266,9 +265,9 @@ func (c *Conn) ListSessionsContext(ctx context.Context) ([]Session, error) {
266265
267266 ret := []Session {}
268267 for _ , el := range out {
269- session , err := sessionFromInterfaces (el )
270- if err ! = nil {
271- return nil , err
268+ session := sessionFromInterfaces (el )
269+ if session = = nil {
270+ return nil , fmt . Errorf ( "ListSessions: can't parse response %+v" , el )
272271 }
273272 ret = append (ret , * session )
274273 }
@@ -289,9 +288,9 @@ func (c *Conn) ListUsersContext(ctx context.Context) ([]User, error) {
289288
290289 ret := []User {}
291290 for _ , el := range out {
292- user , err := userFromInterfaces (el )
293- if err ! = nil {
294- return nil , err
291+ user := userFromInterfaces (el )
292+ if user = = nil {
293+ return nil , fmt . Errorf ( "ListUsers: can't parse response %+v" , el )
295294 }
296295 ret = append (ret , * user )
297296 }
0 commit comments