@@ -49,6 +49,7 @@ func newRootCommand() *cobra.Command {
4949 Use : "rfms" ,
5050 Short : "rFMS CLI" ,
5151 }
52+ cmd .PersistentFlags ().Bool ("debug" , false , "Enable debug logging" )
5253 cmd .AddGroup (& cobra.Group {
5354 ID : "rfms" ,
5455 Title : "rFMS Commands" ,
@@ -67,10 +68,9 @@ func newRootCommand() *cobra.Command {
6768 })
6869 cmd .SetHelpCommandGroupID ("utils" )
6970 cmd .SetCompletionCommandGroupID ("utils" )
70- cmd .PersistentFlags ().BoolP ("debug" , "d" , false , "enable debug logging" )
7171 cmd .PersistentPreRunE = func (cmd * cobra.Command , args []string ) error {
7272 level := slog .LevelInfo
73- if cmd .Flags ().Changed ("debug" ) {
73+ if cmd .Root (). PersistentFlags ().Changed ("debug" ) {
7474 level = slog .LevelDebug
7575 }
7676 slog .SetDefault (slog .New (slog .NewTextHandler (os .Stdout , & slog.HandlerOptions {
@@ -81,6 +81,17 @@ func newRootCommand() *cobra.Command {
8181 return cmd
8282}
8383
84+ func newClient (cmd * cobra.Command ) (* rfms.Client , error ) {
85+ debug , _ := cmd .Root ().PersistentFlags ().GetBool ("debug" )
86+ client , err := auth .NewClient (
87+ rfms .WithDebug (debug ),
88+ )
89+ if err != nil {
90+ return nil , err
91+ }
92+ return client , nil
93+ }
94+
8495func newVehiclesCommand () * cobra.Command {
8596 cmd := & cobra.Command {
8697 Use : "vehicles" ,
@@ -89,7 +100,7 @@ func newVehiclesCommand() *cobra.Command {
89100 }
90101 limit := cmd .Flags ().Int ("limit" , 100 , "max vehicles queried" )
91102 cmd .RunE = func (cmd * cobra.Command , args []string ) error {
92- client , err := auth . NewClient ( )
103+ client , err := newClient ( cmd )
93104 if err != nil {
94105 return err
95106 }
@@ -115,30 +126,42 @@ func newVehiclesCommand() *cobra.Command {
115126
116127func newVehiclePositionsCommand () * cobra.Command {
117128 cmd := & cobra.Command {
118- Use : "vehicle-positions" ,
129+ Use : "vehicle-positions [VIN] " ,
119130 Short : "List vehicle positions" ,
120131 GroupID : "rfms" ,
132+ Args : cobra .MaximumNArgs (1 ),
121133 }
122- limit := cmd .Flags ().Int ("limit" , 100 , "max vehicle positions queried" )
134+ startTime := cmd .Flags ().Time ("start" , time.Time {}, []string {time .DateOnly , time .RFC3339 }, "start time" )
135+ stopTime := cmd .Flags ().Time ("stop" , time.Time {}, []string {time .DateOnly , time .RFC3339 }, "stop time" )
123136 cmd .RunE = func (cmd * cobra.Command , args []string ) error {
124- client , err := auth . NewClient ( )
137+ client , err := newClient ( cmd )
125138 if err != nil {
126139 return err
127140 }
128- moreDataAvailable , lastVIN , count := true , "" , 0
129- for moreDataAvailable && count < * limit {
130- response , err := client .VehiclePositions (cmd .Context (), rfms.VehiclePositionsRequest {
141+ var vin string
142+ if len (args ) > 0 {
143+ vin = args [0 ]
144+ }
145+ moreDataAvailable , lastVIN := true , ""
146+ for moreDataAvailable {
147+ request := rfms.VehiclePositionsRequest {
131148 LastVIN : lastVIN ,
132- LatestOnly : true ,
133- })
149+ VIN : vin ,
150+ LatestOnly : startTime .IsZero () && stopTime .IsZero (),
151+ StartTime : * startTime ,
152+ StopTime : * stopTime ,
153+ }
154+ response , err := client .VehiclePositions (cmd .Context (), request )
134155 if err != nil {
135156 return err
136157 }
137158 for _ , vehiclePosition := range response .VehiclePositions {
138159 fmt .Println (protojson .Format (vehiclePosition ))
139160 }
140- count += len (response .VehiclePositions )
141161 moreDataAvailable = response .MoreDataAvailable
162+ if ! moreDataAvailable {
163+ break
164+ }
142165 lastVIN = response .VehiclePositions [len (response .VehiclePositions )- 1 ].GetVin ()
143166 }
144167 return nil
@@ -156,7 +179,7 @@ func newVehicleStatusesCommand() *cobra.Command {
156179 startTime := cmd .Flags ().Time ("start" , time.Time {}, []string {time .DateOnly , time .RFC3339 }, "start time" )
157180 stopTime := cmd .Flags ().Time ("stop" , time.Time {}, []string {time .DateOnly , time .RFC3339 }, "stop time" )
158181 cmd .RunE = func (cmd * cobra.Command , args []string ) error {
159- client , err := auth . NewClient ( )
182+ client , err := newClient ( cmd )
160183 if err != nil {
161184 return err
162185 }
0 commit comments