diff --git a/session-main.go b/session-main.go index b72ed08e..ccd571ed 100644 --- a/session-main.go +++ b/session-main.go @@ -89,7 +89,7 @@ func listSessions() *probe.Error { for _, sid := range getSessionIDs() { session, err := loadSessionV7(sid) if err != nil { - return err.Trace(sid) + continue // Skip 'broken' session during listing } session.Close() // Session close right here. bySessions = append(bySessions, session) @@ -110,7 +110,15 @@ type clearSessionMessage struct { // String colorized clear session message. func (c clearSessionMessage) String() string { - return console.Colorize("ClearSession", "Session ‘"+c.SessionID+"’ cleared successfully.") + msg := "Session ‘" + c.SessionID + "’" + var colorizedMsg string + switch c.Status { + case "success": + colorizedMsg = console.Colorize("ClearSession", msg+" cleared succesfully.") + case "forced": + colorizedMsg = console.Colorize("ClearSession", msg+" cleared forcefully.") + } + return colorizedMsg } // JSON jsonified clear session message. @@ -140,7 +148,14 @@ func clearSession(sid string) { } session, err := loadSessionV7(sid) - fatalIf(err.Trace(sid), "Unable to load session ‘"+sid+"’.") + if err != nil { + // `mc session clear ` assumes that user is aware that the session is unuseful + // and wants the associated session files to be removed + removeSessionFile(sid) + removeSessionDataFile(sid) + printMsg(clearSessionMessage{Status: "forced", SessionID: sid}) + return + } if session != nil { fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.") diff --git a/session-v7.go b/session-v7.go index b33b1e55..f2e662fb 100644 --- a/session-v7.go +++ b/session-v7.go @@ -138,10 +138,10 @@ func loadSessionV7(sid string) (*sessionV7, *probe.Error) { return nil, err.Trace(sid, s.Header.Version) } - var e error dataFile, e := os.Open(sessionDataFile) - fatalIf(probe.NewError(e), "Unable to open session data file \""+sessionDataFile+"\".") - + if e != nil { + return nil, probe.NewError(e) + } s.DataFP = &sessionDataFP{false, dataFile} return s, nil diff --git a/session.go b/session.go index d90f3357..4159115c 100644 --- a/session.go +++ b/session.go @@ -115,3 +115,21 @@ func getSessionIDs() (sids []string) { } return sids } + +// removeSessionFile - remove the session file, ending with .json +func removeSessionFile(sid string) { + sessionFile, err := getSessionFile(sid) + if err != nil { + return + } + os.Remove(sessionFile) +} + +// removeSessionDataFile - remove the session data file, ending with .data +func removeSessionDataFile(sid string) { + dataFile, err := getSessionDataFile(sid) + if err != nil { + return + } + os.Remove(dataFile) +} diff --git a/session_test.go b/session_test.go index 33aafa91..f50e430e 100644 --- a/session_test.go +++ b/session_test.go @@ -17,6 +17,7 @@ package main import ( + "os" "regexp" . "gopkg.in/check.v1" @@ -37,9 +38,12 @@ func (s *TestSuite) TestSession(c *C) { session := newSessionV7() c.Assert(session.Header.CommandArgs, IsNil) c.Assert(len(session.SessionID), Equals, 8) + _, e := os.Stat(session.DataFP.Name()) + c.Assert(e, IsNil) err = session.Close() c.Assert(err, IsNil) + c.Assert(isSessionExists(session.SessionID), Equals, true) savedSession, err := loadSessionV7(session.SessionID) c.Assert(err, IsNil) @@ -50,4 +54,7 @@ func (s *TestSuite) TestSession(c *C) { err = savedSession.Delete() c.Assert(err, IsNil) + c.Assert(isSessionExists(session.SessionID), Equals, false) + _, e = os.Stat(session.DataFP.Name()) + c.Assert(e, NotNil) }