Skip to content

Commit 1801b48

Browse files
committed
Add option to reset the lock
1 parent 6690cca commit 1801b48

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

cmd/reminder/main.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func Run() error {
2222
// initialization
2323
var err error
2424
var runID = uuid.New()
25+
var startInteractiveProcess bool = true
2526
// note: setting are loaded before logger is being setup; it will assume only default logrus settings
2627
config, err = settings.LoadConfig()
2728
if err != nil {
@@ -32,22 +33,30 @@ func Run() error {
3233
"app": "reminder",
3334
"run_id": runID,
3435
})
36+
3537
// make sure DataFile exists
3638
if err := model.MakeSureFileExists(config.AppInfo.DataFile, true); err != nil {
3739
return err
3840
}
41+
3942
// read and parse the existing data
4043
reminderData, err := model.ReadDataFile(config.AppInfo.DataFile, false)
4144
if err != nil {
4245
return err
4346
}
47+
48+
// check if the data file is locked by another session
4449
if reminderData.MutexLock {
45-
return model.ErrorMutexLockOn
46-
}
47-
reminderData.MutexLock = true
48-
if err := reminderData.UpdateDataFile("Turning ON the Mutex Lock!"); err != nil {
49-
return err
50+
fmt.Printf("WARNING! %s\n", model.ErrorMutexLockOn.Error())
51+
reset := utils.AskBoolean("But, do you want to force reset the lock?")
52+
if !reset {
53+
// exit now without resetting the lock
54+
return model.ErrorMutexLockOn
55+
}
56+
// proceed forward to reset the lock, but don't start the interactive process
57+
startInteractiveProcess = false
5058
}
59+
5160
// make sure lock is released and any uncommitted data is persisted
5261
defer func() {
5362
if !reminderData.MutexLock {
@@ -60,6 +69,17 @@ func Run() error {
6069
utils.LogError(err)
6170
}
6271
}()
72+
73+
// early exit if conditions are not met
74+
if !startInteractiveProcess {
75+
return model.ErrorInteractiveProcessSkipped
76+
}
77+
78+
reminderData.MutexLock = true
79+
if err := reminderData.UpdateDataFile("Turning ON the Mutex Lock!"); err != nil {
80+
return err
81+
}
82+
6383
// start the repeating interactive process
6484
if err := RepeatInteractiveSession(reminderData); err != nil {
6585
return err

internal/model/errors.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package model
33
import "errors"
44

55
var (
6-
ErrorConflictFile = errors.New("created _CONFLICT file")
7-
ErrorMutexLockOn = errors.New("Mutex Lock is ON; there is already a session running!")
6+
ErrorConflictFile = errors.New("Created _CONFLICT file")
7+
ErrorMutexLockOn = errors.New("Mutex Lock is ON; there is already a session running!")
8+
ErrorInteractiveProcessSkipped = errors.New("Skipped running the interactive process. Try again!")
89
)

pkg/calendar/calendar.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func DeleteEvents(srv *gc.Service, events []*gc.Event, dryMode bool) error {
3636
continue
3737
}
3838
if err := srv.Events.Delete("primary", item.Id).Do(); err != nil {
39-
return fmt.Errorf("Couldn't delete the Calendar event %q | %q | %v", item.Id, item.Summary, err)
39+
return fmt.Errorf("Couldn't delete the Calendar event %q | %q | %w", item.Id, item.Summary, err)
4040
} else {
4141
deletionCount += 1
4242
fmt.Printf(" - Deleted the Calendar event %q | %q\n", item.Id, EventString(item))
@@ -218,12 +218,12 @@ func getTokenFromWeb(config *oauth2.Config) (*oauth2.Token, error) {
218218

219219
var authCode string
220220
if _, err := fmt.Scan(&authCode); err != nil {
221-
return nil, fmt.Errorf("Unable to read authorization code: %v", err)
221+
return nil, fmt.Errorf("Unable to read authorization code: %w", err)
222222
}
223223

224224
tok, err := config.Exchange(context.TODO(), authCode)
225225
if err != nil {
226-
return nil, fmt.Errorf("Unable to retrieve token from web: %v", err)
226+
return nil, fmt.Errorf("Unable to retrieve token from web: %w", err)
227227
}
228228
return tok, nil
229229
}

pkg/utils/functions.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ func YearForDueDateDDMM(dateMonth string) (int, error) {
126126
func StrToTime(tString string, timezone string) (time.Time, error) {
127127
t, err := time.Parse(time.RFC3339, tString)
128128
if err != nil {
129-
return t, fmt.Errorf("Unable to parse the time %v: %v", tString, err)
129+
return t, fmt.Errorf("Unable to parse the time %v: %w", tString, err)
130130
}
131131
if timezone == "" {
132132
return t, nil
133133
}
134134
location, err := time.LoadLocation(timezone)
135135
if err != nil {
136-
return t, fmt.Errorf("Unable to parse the timezone %v: %v", timezone, err)
136+
return t, fmt.Errorf("Unable to parse the timezone %v: %w", timezone, err)
137137
}
138138
return t.In(location), nil
139139
}
@@ -515,3 +515,13 @@ func TryConvertTildaBasedPath(path string) string {
515515
}
516516
return path
517517
}
518+
519+
// AskBoolean asks a boolean question to the user.
520+
func AskBoolean(msg string) bool {
521+
var response string
522+
fmt.Printf("%s (y/n): ", msg)
523+
fmt.Scanln(&response)
524+
response = strings.Trim(response, " \n\t")
525+
response = strings.ToLower(response)
526+
return response == "y"
527+
}

0 commit comments

Comments
 (0)