🌷 the cutsie hackatime helper
1package main 2 3import ( 4 "context" 5 "errors" 6 "os" 7 "runtime" 8 9 "github.com/charmbracelet/fang" 10 "github.com/charmbracelet/lipgloss/v2" 11 "github.com/spf13/cobra" 12 "gopkg.in/ini.v1" 13) 14 15func main() { 16 // init our cobra command with a name and description 17 cmd := &cobra.Command{ 18 Use: "akami", 19 Short: "🌷 the cutsie hackatime helper", 20 } 21 22 // add our lipgloss styles 23 fancy := lipgloss.NewStyle().Foreground(lipgloss.Magenta).Bold(true).Italic(true) 24 muted := lipgloss.NewStyle().Foreground(lipgloss.BrightBlue).Italic(true) 25 26 // root diagnose command 27 cmd.AddCommand(&cobra.Command{ 28 Use: "doc", 29 Short: "diagnose potential hackatime issues", 30 RunE: func(c *cobra.Command, _ []string) error { 31 // check our os 32 os_name := runtime.GOOS 33 34 user_dir, err := os.UserHomeDir() 35 if err != nil { 36 return errors.New("somehow your user doesn't exist? fairly sure this should never happen; plz report this to @krn on slack or via email at me@dunkirk.sh") 37 } 38 hackatime_path := user_dir + "/.wakatime.cfg" 39 40 switch os_name { 41 case "linux": 42 case "darwin": 43 case "windows": 44 default: 45 return errors.New("hmm you don't seem to be running a recognized os? you are listed as running " + fancy.Render(os_name) + "; can you plz report this to @krn on slack or via email at me@dunkirk.sh?") 46 } 47 48 c.Println("Looks like you are running", fancy.Render(os_name), "so lets take a look at", muted.Render(hackatime_path), "for your config") 49 50 rawCfg, err := os.ReadFile(hackatime_path) 51 if errors.Is(err, os.ErrNotExist) { 52 return errors.New("you don't have a wakatime config file! go check https://hackatime.hackclub.com/my/wakatime_setup for the instructions and then try this again") 53 } 54 55 cfg, err := ini.Load(rawCfg) 56 if err != nil { 57 return errors.New(err.Error()) 58 } 59 60 settings, err := cfg.GetSection("settings") 61 if err != nil { 62 return errors.New("wow! your config file seems to be messed up and doesn't have a settings heading; can you follow the instructions at https://hackatime.hackclub.com/my/wakatime_setup to regenerate it?\n\nThe raw error we got was: " + err.Error()) 63 } 64 65 api_key := settings.Key("api_key").String() 66 api_url := settings.Key("api_url").String() 67 if api_key == "" { 68 return errors.New("hmm 🤔 looks like you don't have an api_key in your config file? are you sure you have followed the setup instructions at https://hackatime.hackclub.com/my/wakatime_setup correctly?") 69 } 70 if api_url == "" { 71 return errors.New("hmm 🤔 looks like you don't have an api_url in your config file? are you sure you have followed the setup instructions at https://hackatime.hackclub.com/my/wakatime_setup correctly?") 72 } 73 74 if api_url != "https://hackatime.hackclub.com/api/hackatime/v1" { 75 c.Println("\nYour api url", muted.Render(api_url), "doesn't match the expected url of", muted.Render("https://hackatime.hackclub.com/api/hackatime/v1"), "however if you are using a custom forwarder or are sure you know what you are doing then you are probably fine") 76 } 77 78 return nil 79 }, 80 }) 81 82 // this is where we get the fancy fang magic ✨ 83 if err := fang.Execute( 84 context.Background(), 85 cmd, 86 ); err != nil { 87 os.Exit(1) 88 } 89}