7
7
"github.com/alexflint/go-arg"
8
8
"github.com/brokenCursor/usb-modem-cli/drivers"
9
9
"github.com/go-playground/validator/v10"
10
+ "github.com/i582/cfmt/cmd/cfmt"
10
11
"github.com/spf13/viper"
11
12
)
12
13
@@ -32,16 +33,20 @@ func init() {
32
33
config .SetConfigType ("yaml" )
33
34
34
35
sep := string (os .PathSeparator )
35
- config .AddConfigPath (dir + sep + "modem-cli" + sep + "config.yaml" )
36
+ config .AddConfigPath (dir + sep + "modem-cli" )
36
37
37
38
config .SetDefault ("modem.model" , "dummy" )
38
39
config .SetDefault ("modem.ip" , "127.0.0.1" )
39
40
41
+ err = config .ReadInConfig ()
42
+ if err != nil { // Handle errors reading the config file
43
+ panic (fmt .Errorf ("fatal error config file: %w" , err ))
44
+ }
40
45
}
41
46
42
47
func main () {
43
48
if err := run (); err != nil {
44
- fmt .Fprintf (os .Stderr , "error: %v\n " , err )
49
+ cfmt .Fprintf (os .Stderr , "{{ error:}}::red %v\n " , err )
45
50
}
46
51
}
47
52
@@ -54,16 +59,20 @@ func run() error {
54
59
model := modemConfig .GetString ("model" )
55
60
ip := modemConfig .GetString ("ip" )
56
61
57
- // if args.Ip != "" {
58
- // ip = args.Ip
59
- // }
60
-
61
62
if err := validate .Struct (args ); err != nil {
62
63
// TODO: add actual error output
63
- fmt .Printf ("%s\n %v\n " , err .Error (), ip )
64
64
parser .Fail ("invalid value for \" --ip\" " )
65
65
}
66
66
67
+ if args .DisableColor {
68
+ cfmt .DisableColors ()
69
+ }
70
+
71
+ // If IP has been overridden
72
+ if len (args .Ip ) > 0 {
73
+ ip = args .Ip
74
+ }
75
+
67
76
modem , err := drivers .GetModemDriver (model , ip )
68
77
if err != nil {
69
78
return err
@@ -82,40 +91,55 @@ func run() error {
82
91
}
83
92
84
93
switch args .Connection .Action {
94
+ case "up" :
95
+ err := cell .ConnectCell ()
96
+
97
+ if err != nil {
98
+ return err
99
+ }
100
+ case "down" :
101
+ err := cell .DisconnectCell ()
102
+
103
+ if err != nil {
104
+ return err
105
+ }
85
106
case "status" :
86
- isConnected , err := cell .GetCellConnStatus ()
107
+ status , err := cell .GetCellConnStatus ()
87
108
88
109
if err != nil {
89
110
return err
90
111
}
91
112
92
- if isConnected {
93
- fmt .Println ("Status: up" )
94
- } else {
95
- fmt .Println ("Status: down" )
113
+ // Process and output status
114
+ switch {
115
+ case status .Up :
116
+ cfmt .Println ("Status: {{up}}::green|bold" )
117
+ case status .Down :
118
+ cfmt .Println ("Status: {{down}}::red|bold" )
119
+ case status .Connecting :
120
+ cfmt .Println ("Status: {{connecting}}::yellow|bold" )
121
+ case status .Disconnecting :
122
+ cfmt .Println ("Status: {{disconnecting}}::#FA8100|bold" )
123
+ }
124
+ }
125
+ case args .SMS != nil :
126
+ // None of this is implemented :)
127
+ sms , ok := modem .(drivers.ModemSMS )
128
+ if ! ok {
129
+ return DriverSupportError {Driver : modem , Function : "SMS" }
130
+ }
131
+
132
+ switch {
133
+ case args .SMS .Send != nil :
134
+ err := validate .Struct (args .SMS .Send )
135
+ if err != nil {
136
+ parser .FailSubcommand ("Unknown action" , "sms" )
96
137
}
97
- return nil
138
+ sms . SendSMS ( args . SMS . Send . PhoneNumber , args . SMS . Send . PhoneNumber )
98
139
}
99
- // case args.SMS != nil:
100
- // // None of this is implemented :)
101
- // sms, ok := modem.(drivers.ModemSMS)
102
- // if !ok {
103
- // return DriverSupportError{Driver: modem, Function: "SMS"}
104
- // }
105
-
106
- // switch {
107
- // case args.SMS.Read != nil:
108
- // err := validate.Struct(args.SMS.Send)
109
- // if err != nil {
110
- // parser.FailSubcommand("Unknown action", "sms")
111
- // }
112
- // sms.SendSMS(args.SMS.Send.PhoneNumber, args.SMS.Send.PhoneNumber)
113
- // }
114
140
case parser .Subcommand () == nil :
115
141
parser .Fail ("Missing or unknown command" )
116
142
}
117
143
118
- fmt .Printf ("Modem cmd: %s\n " , args .Ip )
119
-
120
144
return nil
121
145
}
0 commit comments