-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcli.go
134 lines (114 loc) · 3.01 KB
/
mcli.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
package main
import (
"log/slog"
"os"
"time"
"github.com/alexflint/go-arg"
"github.com/brokenCursor/usb-modem-cli/config"
"github.com/brokenCursor/usb-modem-cli/drivers"
"github.com/brokenCursor/usb-modem-cli/logging"
"github.com/go-playground/validator/v10"
"github.com/i582/cfmt/cmd/cfmt"
)
var (
validate *validator.Validate
args BaseArgs
logger *slog.Logger
)
func main() {
validate = validator.New(validator.WithRequiredStructEnabled())
logger = logging.GetGeneralLogger()
if err := run(); err != nil {
cfmt.Fprintf(os.Stderr, "{{error:}}::red %v\n", err)
}
}
func run() error {
parser := arg.MustParse(&args)
// Get modem configuration and driver
modemConfig := config.Sub("modem")
model := modemConfig.GetString("model")
if err := validate.Struct(args); err != nil {
// TODO: add actual error output
parser.Fail("invalid value for \"--host\" ")
}
if args.DisableColor {
logger.Debug("Colored output disabled")
cfmt.DisableColors()
}
// If IP has been overridden
if len(args.Host) > 0 {
logger.Debug("IP has been overridden")
modemConfig.Set("host", args.Host)
}
modem, err := drivers.GetModemDriver(model, modemConfig, logging.GetDriverLogger(model))
if err != nil {
return err
}
switch {
case args.Connection != nil:
err := validate.Struct(args.Connection)
if err != nil {
parser.FailSubcommand("Unknown action", "conn")
}
cell, ok := modem.(drivers.ModemCell)
if !ok {
return DriverSupportError{Driver: modem, Function: "cell connection"}
}
switch args.Connection.Action {
case "up":
err := cell.ConnectCell()
if err != nil {
return err
}
case "down":
err := cell.DisconnectCell()
if err != nil {
return err
}
case "status":
status, err := cell.GetCellConnStatus()
if err != nil {
return err
}
// Process and output status
switch status.State {
case 0:
cfmt.Println("Status: {{down}}::red|bold")
case 1:
cfmt.Println("Status: {{disconnecting}}::#FA8100|bold")
case 2:
cfmt.Println("Status: {{connecting}}::yellow|bold")
case 3:
cfmt.Println("Status: {{up}}::green|bold")
}
}
case args.SMS != nil:
sms, ok := modem.(drivers.ModemSMS)
if !ok {
return DriverSupportError{Driver: modem, Function: "SMS"}
}
switch {
case args.SMS.Send != nil:
err := validate.Struct(args.SMS.Send)
if err != nil {
logger.With("err", err.Error()).Debug("sms send validation error")
parser.FailSubcommand("Unknown values or action", "sms")
}
err = sms.SendSMS(args.SMS.Send.PhoneNumber, args.SMS.Send.Message)
if err != nil {
return err
}
case args.SMS.Read != nil:
messages, err := sms.ReadAllSMS()
if err != nil {
return err
}
for i := range messages {
cfmt.Printf("{{ID:}}::cyan %d\n{{Source:}}::green %s\n{{Time:}}::yellow %s\n{{Text:}}::#FA8100\n%s\n---\n", i, messages[i].Sender, messages[i].Time.Format(time.DateTime), messages[i].Message)
}
}
case parser.Subcommand() == nil:
parser.Fail("Missing or unknown command")
}
return nil
}