-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
timer.go
191 lines (159 loc) · 6.29 KB
/
timer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"encoding/json"
"errors"
"fmt"
config "github.com/remotemobprogramming/mob/v5/configuration"
"github.com/remotemobprogramming/mob/v5/httpclient"
"github.com/remotemobprogramming/mob/v5/say"
"runtime"
"strconv"
"time"
)
func StartTimer(timerInMinutes string, configuration config.Configuration) {
if err := startTimer(timerInMinutes, configuration); err != nil {
Exit(1)
}
}
func startTimer(timerInMinutes string, configuration config.Configuration) error {
err, timeoutInMinutes := toMinutes(timerInMinutes)
if err != nil {
return err
}
timeoutInSeconds := timeoutInMinutes * 60
timeOfTimeout := time.Now().Add(time.Minute * time.Duration(timeoutInMinutes)).Format("15:04")
say.Debug(fmt.Sprintf("Starting timer at %s for %d minutes = %d seconds (parsed from user input %s)", timeOfTimeout, timeoutInMinutes, timeoutInSeconds, timerInMinutes))
room := getMobTimerRoom(configuration)
startRemoteTimer := room != ""
startLocalTimer := configuration.TimerLocal
if !startRemoteTimer && !startLocalTimer {
say.Error("No timer configured, not starting timer")
Exit(1)
}
if startRemoteTimer {
timerUser := getUserForMobTimer(configuration.TimerUser)
err := httpPutTimer(timeoutInMinutes, room, timerUser, configuration.TimerUrl, configuration.TimerInsecure)
if err != nil {
say.Error("remote timer couldn't be started")
say.Error(err.Error())
Exit(1)
}
}
if startLocalTimer {
err := executeCommandsInBackgroundProcess(getSleepCommand(timeoutInSeconds), getVoiceCommand(configuration.VoiceMessage, configuration.VoiceCommand), getNotifyCommand(configuration.NotifyMessage, configuration.NotifyCommand), "echo \"mobTimer\"")
if err != nil {
say.Error(fmt.Sprintf("timer couldn't be started on your system (%s)", runtime.GOOS))
say.Error(err.Error())
Exit(1)
}
}
say.Info("It's now " + currentTime() + ". " + fmt.Sprintf("%d min timer ends at approx. %s", timeoutInMinutes, timeOfTimeout) + ". Happy collaborating! :)")
return nil
}
func getMobTimerRoom(configuration config.Configuration) string {
if !isGit() {
say.Debug("timer not in git repository, using MOB_TIMER_ROOM for room name")
return configuration.TimerRoom
}
currentWipBranchQualifier := configuration.WipBranchQualifier
if currentWipBranchQualifier == "" {
currentBranch := gitCurrentBranch()
currentBaseBranch, _ := determineBranches(currentBranch, gitBranches(), configuration)
if currentBranch.IsWipBranch(configuration) {
wipBranchWithoutWipPrefix := currentBranch.removeWipPrefix(configuration).Name
currentWipBranchQualifier = removePrefix(removePrefix(wipBranchWithoutWipPrefix, currentBaseBranch.Name), configuration.WipBranchQualifierSeparator)
}
}
if configuration.TimerRoomUseWipBranchQualifier && currentWipBranchQualifier != "" {
say.Info("Using wip branch qualifier for room name")
return currentWipBranchQualifier
}
return configuration.TimerRoom
}
func StartBreakTimer(timerInMinutes string, configuration config.Configuration) {
if err := startBreakTimer(timerInMinutes, configuration); err != nil {
Exit(1)
}
}
func startBreakTimer(timerInMinutes string, configuration config.Configuration) error {
err, timeoutInMinutes := toMinutes(timerInMinutes)
if err != nil {
return err
}
timeoutInSeconds := timeoutInMinutes * 60
timeOfTimeout := time.Now().Add(time.Minute * time.Duration(timeoutInMinutes)).Format("15:04")
say.Debug(fmt.Sprintf("Starting break timer at %s for %d minutes = %d seconds (parsed from user input %s)", timeOfTimeout, timeoutInMinutes, timeoutInSeconds, timerInMinutes))
room := getMobTimerRoom(configuration)
startRemoteTimer := room != ""
startLocalTimer := configuration.TimerLocal
if !startRemoteTimer && !startLocalTimer {
say.Error("No break timer configured, not starting break timer")
Exit(1)
}
if startRemoteTimer {
timerUser := getUserForMobTimer(configuration.TimerUser)
err := httpPutBreakTimer(timeoutInMinutes, room, timerUser, configuration.TimerUrl, configuration.TimerInsecure)
if err != nil {
say.Error("remote break timer couldn't be started")
say.Error(err.Error())
Exit(1)
}
}
if startLocalTimer {
err := executeCommandsInBackgroundProcess(getSleepCommand(timeoutInSeconds), getVoiceCommand("mob start", configuration.VoiceCommand), getNotifyCommand("mob start", configuration.NotifyCommand), "echo \"mobTimer\"")
if err != nil {
say.Error(fmt.Sprintf("break timer couldn't be started on your system (%s)", runtime.GOOS))
say.Error(err.Error())
Exit(1)
}
}
say.Info("It's now " + currentTime() + ". " + fmt.Sprintf("%d min break timer ends at approx. %s", timeoutInMinutes, timeOfTimeout) + ". So take a break now! :)")
return nil
}
func getUserForMobTimer(userOverride string) string {
if userOverride == "" {
return gitUserName()
}
return userOverride
}
func toMinutes(timerInMinutes string) (error, int) {
timeoutInMinutes, err := strconv.Atoi(timerInMinutes)
if err != nil || timeoutInMinutes < 1 {
say.Error(fmt.Sprintf("The parameter must be an integer number greater then zero"))
return errors.New("The parameter must be an integer number greater then zero"), 0
}
return nil, timeoutInMinutes
}
func httpPutTimer(timeoutInMinutes int, room string, user string, timerService string, disableSSLVerification bool) error {
putBody, _ := json.Marshal(map[string]interface{}{
"timer": timeoutInMinutes,
"user": user,
})
client := httpclient.CreateHttpClient(disableSSLVerification)
_, err := client.SendRequest(putBody, "PUT", timerService+room)
return err
}
func httpPutBreakTimer(timeoutInMinutes int, room string, user string, timerService string, disableSSLVerification bool) error {
putBody, _ := json.Marshal(map[string]interface{}{
"breaktimer": timeoutInMinutes,
"user": user,
})
client := httpclient.CreateHttpClient(disableSSLVerification)
_, err := client.SendRequest(putBody, "PUT", timerService+room)
return err
}
func getSleepCommand(timeoutInSeconds int) string {
return fmt.Sprintf("sleep %d", timeoutInSeconds)
}
func getVoiceCommand(message string, voiceCommand string) string {
if len(voiceCommand) == 0 {
return ""
}
return injectCommandWithMessage(voiceCommand, message)
}
func getNotifyCommand(message string, notifyCommand string) string {
if len(notifyCommand) == 0 {
return ""
}
return injectCommandWithMessage(notifyCommand, message)
}