-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (104 loc) · 3.12 KB
/
main.go
File metadata and controls
125 lines (104 loc) · 3.12 KB
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
// Threshold is a simple command line tool to monitor network usage and disable
// network interface automatically when specified threshold is exeeded.
// Copyright (C) 2025-2026 Enindu Alahapperuma
//
// Threshold is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// Threshold is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// Threshold. If not, see <https://www.gnu.org/licenses/>.
// Threshold is a simple command line tool to monitor network usage and disable
// network interface automatically when specified threshold is exeeded.
//
// Usage:
//
// threshold <command>:<subcommand> [arguments]
// threshold [flags]
//
// Available commands:
//
// daemon
// device
//
// Available flags:
//
// -v, --version # Display version message.
// -h, --help # Display help message.
//
// Use "threshold <command>:help" to see more information about commands.
package main
import (
"context"
"errors"
"log"
"os"
"os/user"
"github.com/enindu/palette"
"github.com/enindu/threshold/commands/daemon"
"github.com/enindu/threshold/commands/device"
)
var (
errUserNoPermission error = errors.New("User has no permission, run as \"root\".")
errInstructionNotFound error = errors.New("Instruction is not found, use \"-h\" or \"--help\" to see help message.")
errCommandInvalid error = errors.New("Command is invalid, use \"-h\" or \"--help\" to see help message.")
)
var (
reguPrinter *palette.Printer = palette.NewPrinterRegu()
erroPrinter *palette.Printer = palette.NewPrinterErro()
)
func main() {
current, err := user.Current()
if err != nil {
erroPrinter.Print("%s\n", err.Error())
return
}
if current.Uid != "0" {
erroPrinter.Print("%s\n", errUserNoPermission.Error())
return
}
file, err := os.OpenFile("/var/log/threshold.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
erroPrinter.Print("%s\n", err.Error())
return
}
defer file.Close()
logger := log.New(file, log.Prefix(), log.Flags())
dispatchers := map[string]func([]string, context.Context, *log.Logger){
"daemon:start": daemon.Start,
"daemon:stop": daemon.Stop,
"daemon:status": daemon.Status,
"daemon:help": daemon.Help,
"device:up": device.Up,
"device:down": device.Down,
"device:usage": device.Usage,
"device:help": device.Help,
}
inputs := os.Args
if len(inputs) < 2 {
erroPrinter.Print("%s\n", errInstructionNotFound.Error())
return
}
instruction := inputs[1]
switch instruction {
case "-v", "--version":
version()
return
case "-h", "--help":
help()
return
}
execute, ok := dispatchers[instruction]
if !ok {
erroPrinter.Print("%s\n", errCommandInvalid)
return
}
ctx := context.Background()
arguments := inputs[2:]
execute(arguments, ctx, logger)
}