-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (64 loc) · 1.75 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"slices"
"strings"
)
var gist, codeword, cmd, logins string
type Comment struct {
Body string `json:"body"`
User struct {
Login string `json:"login"`
} `json:"user"`
}
func init() {
flag.StringVar(&gist, "gist", "", "gist id to look on")
flag.StringVar(&codeword, "codeword", "", "codeword to work with")
flag.StringVar(&cmd, "cmd", "", "command to run")
flag.StringVar(&logins, "logins", "", "possible name restrictions")
flag.Parse()
}
func main() {
if gist == "" || codeword == "" || cmd == "" {
log.Fatalf("please provide all: gist, codeword and cmd")
}
comments, err := listGistComments(gist)
if err != nil {
log.Fatalf("error retrieving gist comments: %v", err)
}
command := strings.Split(cmd, " ")
for _, comment := range comments {
if strings.Contains(comment.Body, codeword) {
if logins != "" && !slices.Contains(strings.Split(logins, ","), comment.User.Login) {
break
}
if err := exec.Command(command[0], command[1:]...).Start(); err != nil {
log.Fatalf("error executing command: %v", err)
}
log.Print("dead man trigger pulled!")
os.Exit(0)
}
}
log.Print("dead man still walking :3")
}
func listGistComments(id string) ([]Comment, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/gists/%s/comments", id), nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
var comments []Comment
if err := json.NewDecoder(res.Body).Decode(&comments); err != nil {
return nil, fmt.Errorf("error decoding request: %w", err)
}
return comments, nil
}