-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (120 loc) · 2.94 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
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
package main
import (
_ "embed"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/DarthBenro008/pigil/internal/database"
"github.com/DarthBenro008/pigil/internal/types"
"github.com/DarthBenro008/pigil/internal/utils"
bolt "go.etcd.io/bbolt"
)
const mainTag = "main"
var Version = "development"
//go:embed secrets.txt
var secrets string
func main() {
dirname, err := os.UserHomeDir()
if err != nil {
utils.ErrorLogger(err, mainTag)
}
secretsArray := strings.Split(secrets, " ")
utils.GoogleClientId = secretsArray[0]
utils.GoogleClientSecret = secretsArray[1]
dirname = fmt.Sprintf("%s/%s", dirname, ".pigil")
if _, err := os.Stat(dirname); errors.Is(err, os.ErrNotExist) {
err := os.Mkdir(dirname, os.ModePerm)
if err != nil {
log.Println(err)
}
}
dirname = fmt.Sprintf("%s/%s", dirname, utils.DatabaseName)
db, err := bolt.Open(dirname, 0666, nil)
if err != nil {
utils.ErrorLogger(err, mainTag)
}
boltLocalDb := database.NewBoltDbService(db, utils.LocalBucket)
boltConfigDb := database.NewBoltDbService(db, utils.ConfigBucket)
dbService := database.NewDatabaseService(boltLocalDb, boltConfigDb)
IsFirstTime(dbService)
if len(os.Args) == 1 {
Help()
os.Exit(0)
}
if os.Args[1] == "bumf" {
cliHandler(os.Args, dbService)
} else {
executor(os.Args, dbService)
}
defer func(db *bolt.DB) {
err := db.Close()
if err != nil {
}
}(db)
}
func cliHandler(args []string, service database.Service) {
switch args[2] {
case utils.CliDb:
ListCommand(service)
case utils.CliAuth:
GoogleAuth(service)
case utils.CliStatus:
Status(service)
case utils.CliLogout:
Logout(service)
case utils.CliDiscord:
if len(args) == 3 {
DiscordToggle(service, true)
} else if args[3] == "disable" {
DiscordToggle(service, true)
} else {
utils.InformationLogger(
"To disable discord webhook run `pigil bumf discord disable`")
}
case utils.CliHelp:
Help()
case utils.CliChannels:
NotificationSelector(service)
default:
utils.InformationLogger("Invalid pigil Command!")
}
}
func executor(args []string, service database.Service) {
cmd := exec.Command(args[1])
cmd.Args = args[1:]
stderr, err := cmd.StderrPipe()
stdout, err := cmd.StdoutPipe()
if err != nil {
utils.ErrorInformation("stderr pipe broke")
}
start := time.Now()
if err := cmd.Start(); err != nil {
utils.ErrorLogger(err, mainTag)
}
ci := types.CommandInformation{
CommandName: args[1],
CommandArguments: args[2:],
TimeOfExecution: time.Now().UnixMicro(),
WasSuccessful: true,
}
results, _ := io.ReadAll(stdout)
fmt.Printf("%s", results)
errorOutput, _ := io.ReadAll(stderr)
fmt.Printf("%s", errorOutput)
err = cmd.Wait()
end := time.Now()
life := end.Sub(start)
utils.GreenPrinter(fmt.Sprintf("runtime: %f seconds", life.Seconds()))
ci.ExecutionTime = life.Seconds()
if err != nil {
ci.WasSuccessful = false
Notify(service, ci)
utils.ErrorInformation(err.Error())
}
InsertCommand(service, ci)
}