🌷 the cutsie hackatime helper

feat: add a test command

dunkirk.sh 25ab5c9b c859cbcc

verified
Changed files
+89
handler
+79
handler/main.go
··· 255 255 256 256 return nil 257 257 } 258 + 259 + func TestHeartbeat(c *cobra.Command, args []string) error { 260 + // Initialize a new context with task state 261 + c.SetContext(context.WithValue(context.Background(), "taskState", &taskState{})) 262 + 263 + printTask(c, "Validating arguments") 264 + 265 + configApiKey, _ := c.Flags().GetString("key") 266 + configApiURL, _ := c.Flags().GetString("url") 267 + 268 + // If either value is missing, try to load from config file 269 + if configApiKey == "" || configApiURL == "" { 270 + userDir, err := os.UserHomeDir() 271 + if err != nil { 272 + errorTask(c, "Validating arguments") 273 + return err 274 + } 275 + wakatimePath := filepath.Join(userDir, ".wakatime.cfg") 276 + 277 + cfg, err := ini.Load(wakatimePath) 278 + if err != nil { 279 + errorTask(c, "Validating arguments") 280 + return errors.New("config file not found and you haven't passed all arguments") 281 + } 282 + 283 + settings, err := cfg.GetSection("settings") 284 + if err != nil { 285 + errorTask(c, "Validating arguments") 286 + return errors.New("no settings section in your config") 287 + } 288 + 289 + // Only load from config if not provided as parameter 290 + if configApiKey == "" { 291 + configApiKey = settings.Key("api_key").String() 292 + if configApiKey == "" { 293 + errorTask(c, "Validating arguments") 294 + return errors.New("couldn't find an api_key in your config") 295 + } 296 + } 297 + 298 + if configApiURL == "" { 299 + configApiURL = settings.Key("api_url").String() 300 + if configApiURL == "" { 301 + errorTask(c, "Validating arguments") 302 + return errors.New("couldn't find an api_url in your config") 303 + } 304 + } 305 + } 306 + 307 + completeTask(c, "Arguments look fine!") 308 + 309 + printTask(c, "Loading api client") 310 + 311 + client := wakatime.NewClientWithOptions(configApiKey, configApiURL) 312 + _, err := client.GetStatusBar() 313 + if err != nil { 314 + errorTask(c, "Loading api client") 315 + return err 316 + } 317 + 318 + completeTask(c, "Loading api client") 319 + 320 + c.Println("Sending a test heartbeat to", styles.Muted.Render(configApiURL)) 321 + 322 + printTask(c, "Sending test heartbeat") 323 + 324 + err = client.SendHeartbeat(testHeartbeat) 325 + 326 + if err != nil { 327 + errorTask(c, "Sending test heartbeat") 328 + return err 329 + } 330 + 331 + completeTask(c, "Sending test heartbeat") 332 + 333 + c.Println("❇️ test heartbeat sent!") 334 + 335 + return nil 336 + }
+10
main.go
··· 32 32 RunE: handler.Doctor, 33 33 }) 34 34 35 + cmdTest := &cobra.Command{ 36 + Use: "test", 37 + Short: "send a test heartbeat to hackatime or whatever api url you provide", 38 + RunE: handler.TestHeartbeat, 39 + Args: cobra.NoArgs, 40 + } 41 + cmdTest.Flags().StringP("url", "u", "", "The base url for the hackatime client") 42 + cmdTest.Flags().StringP("key", "k", "", "API key to use for authentication") 43 + cmd.AddCommand(cmdTest) 44 + 35 45 // this is where we get the fancy fang magic ✨ 36 46 if err := fang.Execute( 37 47 context.Background(),