├── .gitignore ├── README.md ├── config.go └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | hlog 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hlog 2 | 3 | `hlog` is a super-simple Hakka Logs CLI client. Just set up your `~/.hakkarc` 4 | file and then run `hlog ` to post a log. 5 | 6 | ## Config file 7 | 8 | hlog looks for its config in `$HOME/.hakkarc`, in the following format: 9 | 10 | ``` 11 | [logs] 12 | token = your_hakka_logs_webhook_token 13 | default-privacy = private 14 | ``` 15 | 16 | Optionally, you can substitute a different URL for the default one by adding a 17 | `url = http://some.custom.domain/path?foo=bar` - just be sure to leave off the 18 | `&token=your_token..` part, as hlog appends this automatically. 19 | 20 | The `default-privacy` parameter defaults to `private`, but you can also set 21 | it to `public` or `anonymous`. You can also set this per-log with the 22 | `-priv=` command-line flag. 23 | 24 | If you're having TLS handshake issues, please let me know. As a temporary 25 | workaround, you can try turning off TLS certificate validation by adding the 26 | following to your .hakkarc: 27 | 28 | ``` 29 | [connection] 30 | verify-ssl = false 31 | ``` 32 | 33 | ## Installing 34 | 35 | If you've got Go installed, just run `go get github.com/justinian/hlog`. 36 | -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "code.google.com/p/gcfg" 5 | ) 6 | 7 | type Config struct { 8 | Logs struct { 9 | Token string 10 | Url string 11 | Default_Privacy string 12 | } 13 | Connection struct { 14 | Verify_Ssl bool 15 | } 16 | } 17 | 18 | var defaultConfig = ` 19 | [logs] 20 | url = https://www.hakkalabs.co/api/webhooks?service=custom 21 | default-privacy = private 22 | 23 | [connection] 24 | verify-ssl = true 25 | ` 26 | 27 | func getConfig(filename string) (*Config, error) { 28 | var config Config 29 | if err := gcfg.ReadStringInto(&config, defaultConfig); err != nil { 30 | panic("Failed to parse default config: " + err.Error()) 31 | } 32 | 33 | err := gcfg.ReadFileInto(&config, filename) 34 | return &config, err 35 | } 36 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "crypto/tls" 6 | "encoding/json" 7 | "flag" 8 | "log" 9 | "net/http" 10 | "os" 11 | "strings" 12 | ) 13 | 14 | type Log struct { 15 | Message string `json:"log"` 16 | Privacy string `json:"state"` 17 | } 18 | 19 | func main() { 20 | configfile := os.Getenv("HOME") + "/.hakkarc" 21 | config, err := getConfig(configfile) 22 | if err != nil { 23 | log.Fatalf("%s: Could not read config file %s: %s", 24 | os.Args[0], configfile, err.Error()) 25 | } 26 | 27 | privacy := flag.String("priv", config.Logs.Default_Privacy, "Privacy level to log with (public, anonymous, private)") 28 | flag.Parse() 29 | 30 | logItem := Log{ 31 | Message: strings.Join(flag.Args(), " "), 32 | Privacy: *privacy, 33 | } 34 | 35 | url := config.Logs.Url + "&token=" + config.Logs.Token 36 | data, err := json.Marshal(logItem) 37 | if err != nil { 38 | log.Fatal(err) 39 | } 40 | 41 | tr := &http.Transport{} 42 | if !config.Connection.Verify_Ssl { 43 | tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} 44 | } 45 | 46 | cl := &http.Client{Transport: tr} 47 | 48 | buf := bytes.NewBuffer(data) 49 | resp, err := cl.Post(url, "application/json", buf) 50 | if err != nil { 51 | log.Fatal(err) 52 | } 53 | 54 | if resp.Status[:2] != "20" { 55 | log.Fatal(resp.Body) 56 | } 57 | } 58 | --------------------------------------------------------------------------------