-
Notifications
You must be signed in to change notification settings - Fork 0
/
cover.html
726 lines (624 loc) · 27 KB
/
cover.html
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>database: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/vigneshrajj/gofind/internal/database/commands.go (100.0%)</option>
<option value="file1">github.com/vigneshrajj/gofind/internal/database/db.go (100.0%)</option>
<option value="file2">github.com/vigneshrajj/gofind/internal/handlers/commands.go (100.0%)</option>
<option value="file3">github.com/vigneshrajj/gofind/internal/handlers/query.go (100.0%)</option>
<option value="file4">github.com/vigneshrajj/gofind/internal/handlers/utilities.go (100.0%)</option>
<option value="file5">github.com/vigneshrajj/gofind/internal/helpers/helpers.go (100.0%)</option>
<option value="file6">github.com/vigneshrajj/gofind/internal/server/server.go (100.0%)</option>
<option value="file7">github.com/vigneshrajj/gofind/internal/templates/templates.go (100.0%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package database
import (
"errors"
"github.com/vigneshrajj/gofind/models"
"gorm.io/gorm"
)
func CreateCommand(db *gorm.DB, command models.Command) error <span class="cov8" title="1">{
if err := db.Create(&command).Error; err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">return nil</span>
}
func FirstOrCreateCommand(db *gorm.DB, command models.Command) <span class="cov8" title="1">{
db.FirstOrCreate(&command)
}</span>
func DeleteCommand(db *gorm.DB, alias string) error <span class="cov8" title="1">{
if rowsAffected := db.Delete(&models.Command{}, "alias=? AND is_default=?", alias, false).RowsAffected; rowsAffected == 0 </span><span class="cov8" title="1">{
return errors.New("Command not found")
}</span>
<span class="cov8" title="1">return nil</span>
}
func ListCommands(db *gorm.DB) []models.Command <span class="cov8" title="1">{
var commands []models.Command
db.Find(&commands)
return commands
}</span>
func SearchCommand(db *gorm.DB, alias string, partialMatch bool) models.Command <span class="cov8" title="1">{
var command models.Command
if partialMatch </span><span class="cov8" title="1">{
db.Where("alias LIKE ?", alias+"%").Order("LENGTH(alias) ASC").Find(&command)
}</span> else<span class="cov8" title="1"> {
db.Where("alias=?", alias).Find(&command)
}</span>
<span class="cov8" title="1">return command</span>
}
func GetDefaultCommand(db *gorm.DB) models.Command <span class="cov8" title="1">{
var command models.Command
db.Where("is_default=?", true).Find(&command)
return command
}</span>
func SetDefaultCommand(db *gorm.DB, alias string) error <span class="cov8" title="1">{
var command models.Command
var defaultCommand models.Command
if db.Where("alias=?", alias).Find(&command); command == (models.Command{}) </span><span class="cov8" title="1">{
return errors.New("Command not found")
}</span>
<span class="cov8" title="1">if db.Where("is_default=?", true).Find(&defaultCommand); defaultCommand == (models.Command{}) </span><span class="cov8" title="1">{
return errors.New("Default Command not found")
}</span>
<span class="cov8" title="1">command.IsDefault = true
defaultCommand.IsDefault = false
db.Save(&command)
db.Save(&defaultCommand)
return nil</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package database
import (
"database/sql"
"github.com/vigneshrajj/gofind/models"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func NewDBConnection(dbFileName string) (*sql.DB, *gorm.DB, error) <span class="cov8" title="1">{
db, err := gorm.Open(sqlite.Open(dbFileName), &gorm.Config{})
if err != nil </span><span class="cov8" title="1">{
return nil, nil, err
}</span>
<span class="cov8" title="1">EnsureCommandTableExists(db)
dbSql, _ := db.DB()
return dbSql, db, nil</span>
}
func EnsureCommandTableExists(db *gorm.DB) <span class="cov8" title="1">{
db.AutoMigrate(&models.Command{})
}</span>
func EnsureDefaultCommandsExist(db *gorm.DB) <span class="cov8" title="1">{
defaultCommands := []models.Command{
{
Alias: "g",
Query: "https://www.google.com/search?q=%s",
Type: models.SearchCommand,
Description: sql.NullString{String: "Google Search", Valid: true},
IsDefault: true,
},
{
Alias: "b64",
Query: "",
Type: models.UtilCommand,
Description: sql.NullString{String: "Convert To Base 64 string", Valid: true},
IsDefault: false,
},
{
Alias: "d64",
Query: "",
Type: models.UtilCommand,
Description: sql.NullString{String: "Decode Base 64 string", Valid: true},
IsDefault: false,
},
{
Alias: "sha256",
Query: "",
Type: models.UtilCommand,
Description: sql.NullString{String: "Convert To SHA 256 string", Valid: true},
IsDefault: false,
},
}
for _, command := range defaultCommands </span><span class="cov8" title="1">{
FirstOrCreateCommand(db, command)
}</span>
}
func EnsureAdditionalCommandsExist(db *gorm.DB) <span class="cov8" title="1">{
additionalCommands := []models.Command{
{
Alias: "y",
Query: "https://www.youtube.com/results?search_query=%s",
Type: models.SearchCommand,
Description: sql.NullString{String: "Youtube", Valid: true},
IsDefault: false,
},
{
Alias: "ddg",
Query: "https://duckduckgo.com/?q=%s",
Type: models.SearchCommand,
Description: sql.NullString{String: "DuckDuckGo", Valid: true},
IsDefault: false,
},
{
Alias: "ddl",
Query: "https://lite.duckduckgo.com/lite/?q=%s",
Type: models.SearchCommand,
Description: sql.NullString{String: "DuckDuckGo Lite", Valid: true},
IsDefault: false,
},
{
Alias: "gh",
Query: "https://github.com/search?q=%s&type=repositories",
Type: models.SearchCommand,
Description: sql.NullString{String: "Github Repos", Valid: true},
IsDefault: false,
},
{
Alias: "npm",
Query: "https://www.npmjs.com/search?q=%s",
Type: models.SearchCommand,
Description: sql.NullString{String: "Node Package Manager (NPM)", Valid: true},
IsDefault: false,
},
{
Alias: "m",
Query: "https://mail.google.com/mail/u/{r:0,vr:1}/#inbox",
Type: models.SearchCommand,
Description: sql.NullString{String: "GMail", Valid: true},
IsDefault: false,
},
}
for _, command := range additionalCommands </span><span class="cov8" title="1">{
FirstOrCreateCommand(db, command)
}</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package handlers
import (
"database/sql"
"fmt"
"github.com/vigneshrajj/gofind/internal/database"
"github.com/vigneshrajj/gofind/internal/templates"
"github.com/vigneshrajj/gofind/models"
"net/http"
"strings"
"gorm.io/gorm"
)
func HandleAddCommand(w http.ResponseWriter, data []string, db *gorm.DB) <span class="cov8" title="1">{
if len(data) < 3 </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid number of arguments provided. Add command usage:\n#a <alias> <url-with-args> <description(optional)>")
return
}</span>
<span class="cov8" title="1">command := models.Command{
Alias: data[1],
Query: data[2],
}
if len(data) > 3 </span><span class="cov8" title="1">{
command.Description = sql.NullString{String: strings.Join(data[3:], " "), Valid: true}
}</span>
<span class="cov8" title="1">err := database.CreateCommand(db, command)
if err != nil </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, err.Error())
return
}</span>
<span class="cov8" title="1">templates.MessageTemplate(w, "Added Command: "+data[1]+", URL: "+data[2])</span>
}
func HandleListCommands(w http.ResponseWriter, data []string, db *gorm.DB) <span class="cov8" title="1">{
if len(data) != 1 </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid number of arguments provided. List command usage: #l")
return
}</span>
<span class="cov8" title="1">commands := database.ListCommands(db)
templates.ListCommandsTemplate(w, commands)</span>
}
func ChangeDefaultCommand(w http.ResponseWriter, r *http.Request, db *gorm.DB) <span class="cov8" title="1">{
alias := r.URL.Query().Get("default")
response := "Default Command has been changed successfully to " + alias
err := database.SetDefaultCommand(db, alias)
if err != nil </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
response = "Error setting default command."
}</span>
<span class="cov8" title="1">w.Write([]byte(response))</span>
}
func HandleDeleteCommand(w http.ResponseWriter, data []string, db *gorm.DB) <span class="cov8" title="1">{
if len(data) != 2 </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid number of arguments provided. Delete command usage: #d <alias>")
return
}</span>
<span class="cov8" title="1">command := database.SearchCommand(db, data[1], false)
if command == (models.Command{}) </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Command not found.")
return
}</span>
<span class="cov8" title="1">if command.Type == models.ApiCommand || command.Type == models.UtilCommand </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Cannot delete built-in utilities or api commands.")
return
}</span>
<span class="cov8" title="1">database.DeleteCommand(db, data[1])
fmt.Fprintf(w, "Deleted Command: %s", data[1])</span>
}
</pre>
<pre class="file" id="file3" style="display: none">package handlers
import (
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/vigneshrajj/gofind/internal/database"
"github.com/vigneshrajj/gofind/internal/templates"
"github.com/vigneshrajj/gofind/models"
"gorm.io/gorm"
)
func isKeyValueArg(query string) bool <span class="cov8" title="1">{
bracketIndex := strings.Index(query, "{")
if bracketIndex == -1 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">colonIndex := strings.Index(query[bracketIndex:], ":")
if colonIndex == -1 </span><span class="cov8" title="1">{
return false
}</span>
<span class="cov8" title="1">return true</span>
}
func replaceKeyWithValue(input string, choice string) (string, error) <span class="cov8" title="1">{
re := regexp.MustCompile(`{([^}]*)}`)
matches := re.FindStringSubmatch(input)
content := matches[1]
keyValuePairs := strings.Split(content, ",")
kvMap := make(map[string]string)
for _, pair := range keyValuePairs </span><span class="cov8" title="1">{
parts := strings.SplitN(pair, ":", 2)
if len(parts) == 2 </span><span class="cov8" title="1">{
kvMap[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}</span>
}
<span class="cov8" title="1">if value, found := kvMap[choice]; found </span><span class="cov8" title="1">{
return strings.Replace(input, matches[0], value, 1), nil
}</span> else<span class="cov8" title="1"> {
return input, errors.New("Invalid Argument Provided.")
}</span>
}
func HandleRedirectQuery(w http.ResponseWriter, r *http.Request, data []string, db *gorm.DB) <span class="cov8" title="1">{
alias := data[0]
command := database.SearchCommand(db, alias, true)
if command == (models.Command{}) </span><span class="cov8" title="1">{
defaultCommand := database.GetDefaultCommand(db)
command = defaultCommand
data = append([]string{command.Alias}, data...)
}</span>
<span class="cov8" title="1">if command.Type == models.UtilCommand </span><span class="cov8" title="1">{
HandleUtilCommand(w, data)
return
}</span>
<span class="cov8" title="1">query := command.Query
startsWithHttp := strings.HasPrefix(query, "http://") || strings.HasPrefix(query, "https://")
if !startsWithHttp </span><span class="cov8" title="1">{
query = "https://" + query
}</span>
<span class="cov8" title="1">if strings.Contains(query, "%s") </span><span class="cov8" title="1">{
query = fmt.Sprintf(query, url.QueryEscape(strings.Join(data[1:], " ")))
http.Redirect(w, r, query, http.StatusFound)
return
}</span>
<span class="cov8" title="1">if isKeyValueArg(query) </span><span class="cov8" title="1">{
argsCount := strings.Count(query, "{")
inputArgsCount := len(data) - 1
if argsCount != inputArgsCount </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid arguments provided")
return
}</span>
<span class="cov8" title="1">for i := 1; i < len(data); i++ </span><span class="cov8" title="1">{
var err error
query, err = replaceKeyWithValue(query, data[i])
if err != nil </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, err.Error())
return
}</span>
}
<span class="cov8" title="1">http.Redirect(w, r, query, http.StatusFound)
return</span>
}
<span class="cov8" title="1">argCount := len(data) - 1
for i := argCount; i >= 1; i-- </span><span class="cov8" title="1">{
query = strings.Replace(query, fmt.Sprintf("{%d}", i), data[i], -1)
query = strings.Replace(query, fmt.Sprintf("{%d}", i), data[i], -1)
}</span>
<span class="cov8" title="1">argCountInQuery := strings.Count(query, "{")
isNArgQuery := strings.Count(query, "%s") == 1
if argCountInQuery > 0 && !isNArgQuery </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid number of arguments provided")
return
}</span>
<span class="cov8" title="1">http.Redirect(w, r, query, http.StatusFound)</span>
}
func HandleQuery(w http.ResponseWriter, r *http.Request, query string, db *gorm.DB) <span class="cov8" title="1">{
if query == "" </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Query cannot be empty")
return
}</span>
<span class="cov8" title="1">data := strings.Split(query, " ")
switch data[0] </span>{
case "#a":<span class="cov8" title="1">
HandleAddCommand(w, data, db)</span>
case "#d":<span class="cov8" title="1">
HandleDeleteCommand(w, data, db)</span>
case "#l":<span class="cov8" title="1">
HandleListCommands(w, data, db)</span>
default:<span class="cov8" title="1">
HandleRedirectQuery(w, r, data, db)</span>
}
}
</pre>
<pre class="file" id="file4" style="display: none">package handlers
import (
"net/http"
"github.com/vigneshrajj/gofind/internal/helpers"
"github.com/vigneshrajj/gofind/internal/templates"
)
func HandleUtilCommand(w http.ResponseWriter, data []string) <span class="cov8" title="1">{
alias := data[0]
switch alias </span>{
case "b64":<span class="cov8" title="1">
HandleB64Util(w, data)</span>
case "d64":<span class="cov8" title="1">
HandleD64Util(w, data)</span>
case "sha256":<span class="cov8" title="1">
HandleSha256Util(w, data)</span>
}
}
func HandleSha256Util(w http.ResponseWriter, data []string) <span class="cov8" title="1">{
if len(data) != 2 </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid number of arguments provided. SHA256 encode command usage: sha256 <string>")
return
}</span>
<span class="cov8" title="1">encoded := helpers.Sha256(data[1])
templates.Sha256Template(w, encoded)</span>
}
func HandleD64Util(w http.ResponseWriter, data []string) <span class="cov8" title="1">{
if len(data) != 2 </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid number of arguments provided. Base64 Decode command usage: d64 <string>")
return
}</span>
<span class="cov8" title="1">decoded := helpers.GetB64Decode(data[1])
templates.Base64DecodeTemplate(w, decoded)</span>
}
func HandleB64Util(w http.ResponseWriter, data []string) <span class="cov8" title="1">{
if len(data) != 2 </span><span class="cov8" title="1">{
w.WriteHeader(http.StatusBadRequest)
templates.MessageTemplate(w, "Invalid number of arguments provided. Base64 command usage: b64 <string>")
return
}</span>
<span class="cov8" title="1">encoded := helpers.GetB64(data[1])
templates.Base64Template(w, encoded)</span>
}
</pre>
<pre class="file" id="file5" style="display: none">package helpers
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
)
func GetB64(data string) string <span class="cov8" title="1">{
return base64.StdEncoding.EncodeToString([]byte(data))
}</span>
func GetB64Decode(data string) string <span class="cov8" title="1">{
decoded, _ := base64.StdEncoding.DecodeString(data)
return string(decoded)
}</span>
func Sha256(data string) string <span class="cov8" title="1">{
hash := sha256.New()
hash.Write([]byte(data))
hashedBytes := hash.Sum(nil)
hashedHex := hex.EncodeToString(hashedBytes)
return hashedHex
}</span>
</pre>
<pre class="file" id="file6" style="display: none">package server
import (
"fmt"
"log"
"net/http"
"github.com/vigneshrajj/gofind/config"
"github.com/vigneshrajj/gofind/internal/database"
"github.com/vigneshrajj/gofind/internal/handlers"
"gorm.io/gorm"
)
func HandleRoutes(db *gorm.DB) <span class="cov8" title="1">{
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) </span><span class="cov8" title="1">{
query := r.URL.Query().Get("query")
handlers.HandleQuery(w, r, query, db)
}</span>)
<span class="cov8" title="1">http.HandleFunc("/set-default-command", func(w http.ResponseWriter, r *http.Request) </span><span class="cov8" title="1">{
handlers.ChangeDefaultCommand(w, r, db)
}</span>)
<span class="cov8" title="1">http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) </span><span class="cov8" title="1">{
fmt.Fprintf(w, "Server running on %s", config.Port)
}</span>)
}
func StartServer(DbPath string, Port string) error <span class="cov8" title="1">{
_, db, err := database.NewDBConnection(DbPath)
if err != nil </span><span class="cov8" title="1">{
return err
}</span>
<span class="cov8" title="1">database.EnsureDefaultCommandsExist(db)
if config.EnableAdditionalCommands </span><span class="cov8" title="1">{
database.EnsureAdditionalCommandsExist(db)
}</span>
<span class="cov8" title="1">HandleRoutes(db)
log.Printf("Starting server on %s", Port)
log.Fatal(http.ListenAndServe(Port, nil))
return nil</span>
}
</pre>
<pre class="file" id="file7" style="display: none">package templates
import (
"github.com/vigneshrajj/gofind/models"
"html/template"
"net/http"
)
type ListCommandsPageData struct {
GroupedCommands map[models.CommandType][]models.Command
}
func groupByType(commands []models.Command) map[models.CommandType][]models.Command <span class="cov8" title="1">{
groupedCommands := make(map[models.CommandType][]models.Command)
for _, command := range commands </span><span class="cov8" title="1">{
groupedCommands[command.Type] = append(groupedCommands[command.Type], command)
}</span>
<span class="cov8" title="1">return groupedCommands</span>
}
func ListCommandsTemplate(w http.ResponseWriter, commands []models.Command) <span class="cov8" title="1">{
data := ListCommandsPageData{
GroupedCommands: groupByType(commands),
}
tmpl := template.Must(template.ParseFiles("static/templates/list_commands.html"))
tmpl.Execute(w, data)
}</span>
type MessagePageData struct {
Message string
}
func MessageTemplate(w http.ResponseWriter, msg string) <span class="cov8" title="1">{
data := MessagePageData{
Message: msg,
}
tmpl := template.Must(template.ParseFiles("static/templates/message.html"))
tmpl.Execute(w, data)
}</span>
type B64PageType string
const (
Encoded B64PageType = "encoded"
Decoded B64PageType = "decoded"
)
type B64PageData struct {
Value string
Type B64PageType
}
func Base64Template(w http.ResponseWriter, encoded string) <span class="cov8" title="1">{
data := B64PageData{
Value: encoded,
Type: Encoded,
}
tmpl := template.Must(template.ParseFiles("static/templates/base64.html"))
tmpl.Execute(w, data)
}</span>
func Base64DecodeTemplate(w http.ResponseWriter, decoded string) <span class="cov8" title="1">{
data := B64PageData{
Value: decoded,
Type: Decoded,
}
tmpl := template.Must(template.ParseFiles("static/templates/base64.html"))
tmpl.Execute(w, data)
}</span>
type Sha256PageData struct {
Value string
}
func Sha256Template(w http.ResponseWriter, hashed string) <span class="cov8" title="1">{
data := Sha256PageData{
Value: hashed,
}
tmpl := template.Must(template.ParseFiles("static/templates/sha256.html"))
tmpl.Execute(w, data)
}</span>
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>