diff options
| author | Christine Dodrill <me@christine.website> | 2017-03-29 11:14:59 -0700 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2017-03-29 11:14:59 -0700 |
| commit | e7824cb2609cc32d4229c9a7bc0de7b7153d36ef (patch) | |
| tree | e14b68f2ea178ca1d96fa87d5cb1686e71a41695 /tools | |
| parent | 77416787c09e1a45f487e5f932a5c688469c730d (diff) | |
| download | x-e7824cb2609cc32d4229c9a7bc0de7b7153d36ef.tar.xz x-e7824cb2609cc32d4229c9a7bc0de7b7153d36ef.zip | |
reorganization
Diffstat (limited to 'tools')
48 files changed, 2303 insertions, 0 deletions
diff --git a/tools/atheme/.gitignore b/tools/atheme/.gitignore new file mode 100644 index 0000000..72dd7a3 --- /dev/null +++ b/tools/atheme/.gitignore @@ -0,0 +1 @@ +atheme diff --git a/tools/atheme/main.go b/tools/atheme/main.go new file mode 100644 index 0000000..b291158 --- /dev/null +++ b/tools/atheme/main.go @@ -0,0 +1,95 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "log" + "os" + + "github.com/Xe/Tetra/atheme" + "github.com/howeyc/gopass" +) + +var ( + client *atheme.Atheme + serverString = flag.String("server", "http://127.0.0.1:8080/xmlrpc", "what http://server:port to connect to") + cookie = flag.String("cookie", "", "authentication cookie to use") + user = flag.String("user", "", "username to use") +) + +func main() { + flag.Parse() + + command := flag.Arg(0) + if command == "" { + flag.Usage() + os.Exit(1) + } + + var rest []string + + if n := flag.NArg(); n > 1 { + rest = flag.Args()[1:] + } + + _ = rest + + var err error + + client, err = atheme.NewAtheme(*serverString) + if err != nil { + log.Fatal(err) + } + + switch command { + case "login": + var username string + var password string + scanner := bufio.NewScanner(os.Stdin) + + fmt.Print("Username: ") + + for { + scanner.Scan() + username = scanner.Text() + + if scanner.Err() == nil { + break + } + + fmt.Print("Username: ") + } + + fmt.Print("Password: ") + for { + password = string(gopass.GetPasswdMasked()) + if password != "" { + break + } + fmt.Print("Password: ") + } + + err = client.Login(username, password) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Authcookie: %s\n", client.Authcookie) + + case "command": + if *cookie == "" { + log.Fatal("specify cookie") + } + + client.Authcookie = *cookie + client.Account = *user + + output, err := client.Command(rest...) + if err != nil { + log.Fatal(err) + } + + fmt.Println(output) + } +} diff --git a/tools/dbupload/.gitignore b/tools/dbupload/.gitignore new file mode 100644 index 0000000..fa5221a --- /dev/null +++ b/tools/dbupload/.gitignore @@ -0,0 +1 @@ +dbupload diff --git a/tools/dbupload/db.go b/tools/dbupload/db.go new file mode 100644 index 0000000..5307d04 --- /dev/null +++ b/tools/dbupload/db.go @@ -0,0 +1,49 @@ +package main + +type UploadImage struct { + Image struct { + SourceURL string `json:"source_url"` + Tags string `json:"tag_list"` + ImageURL string `json:"image_url"` + } `json:"image"` +} + +type Image struct { + ID string `json:"id"` + IDNumber int `json:"id_number"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + DuplicateReports []interface{} `json:"duplicate_reports"` + FileName string `json:"file_name"` + Description string `json:"description"` + Uploader string `json:"uploader"` + Image string `json:"image"` + Score int `json:"score"` + Upvotes int `json:"upvotes"` + Downvotes int `json:"downvotes"` + Faves int `json:"faves"` + CommentCount int `json:"comment_count"` + Tags string `json:"tags"` + TagIds []string `json:"tag_ids"` + Width int `json:"width"` + Height int `json:"height"` + AspectRatio float64 `json:"aspect_ratio"` + OriginalFormat string `json:"original_format"` + MimeType string `json:"mime_type"` + Sha512Hash string `json:"sha512_hash"` + OrigSha512Hash string `json:"orig_sha512_hash"` + SourceURL string `json:"source_url"` + License string `json:"license"` + Representations struct { + ThumbTiny string `json:"thumb_tiny"` + ThumbSmall string `json:"thumb_small"` + Thumb string `json:"thumb"` + Small string `json:"small"` + Medium string `json:"medium"` + Large string `json:"large"` + Tall string `json:"tall"` + Full string `json:"full"` + } `json:"representations"` + IsRendered bool `json:"is_rendered"` + IsOptimized bool `json:"is_optimized"` +} diff --git a/tools/dbupload/dbmanifestgen/.gitignore b/tools/dbupload/dbmanifestgen/.gitignore new file mode 100644 index 0000000..fb82ff6 --- /dev/null +++ b/tools/dbupload/dbmanifestgen/.gitignore @@ -0,0 +1 @@ +dbmanifestgen diff --git a/tools/dbupload/dbmanifestgen/db.go b/tools/dbupload/dbmanifestgen/db.go new file mode 120000 index 0000000..18b3e05 --- /dev/null +++ b/tools/dbupload/dbmanifestgen/db.go @@ -0,0 +1 @@ +../db.go
\ No newline at end of file diff --git a/tools/dbupload/dbmanifestgen/main.go b/tools/dbupload/dbmanifestgen/main.go new file mode 100644 index 0000000..c5bbb2b --- /dev/null +++ b/tools/dbupload/dbmanifestgen/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" + "strings" +) + +var ( + serverloc = flag.String("serverloc", "http://static.xeserv.us/", "server to prepend to url paths") + sourceloc = flag.String("sourceloc", "", "source URL for metadata generation") + set = flag.String("set", "", "comic set") +) + +func main() { + flag.Parse() + + if flag.NArg() == 0 { + fmt.Printf("%s: <dir>\n", os.Args[0]) + flag.Usage() + } + + if *sourceloc == "" { + log.Fatal("Need a source location") + } + + images, err := ioutil.ReadDir(flag.Arg(0)) + if err != nil { + panic(err) + } + + for _, image := range images { + if strings.HasSuffix(image.Name(), ".json") { + log.Printf("Skipped %s...", image) + continue + } + + fout, err := os.Create(image.Name() + ".json") + + var i UploadImage + i.Image.SourceURL = *sourceloc + i.Image.ImageURL = *serverloc + filepath.Base(image.Name()) + i.Image.Tags = "explicit" + + if *set != "" { + i.Image.Tags = i.Image.Tags + ", comic:" + *set + } + + outdata, err := json.MarshalIndent(&i, "", "\t") + if err != nil { + panic(err) + } + + fout.Write(outdata) + fout.Close() + } +} diff --git a/tools/dbupload/dbsimplegen/dbsimplegen b/tools/dbupload/dbsimplegen/dbsimplegen new file mode 100755 index 0000000..e8939e8 --- /dev/null +++ b/tools/dbupload/dbsimplegen/dbsimplegen @@ -0,0 +1,11 @@ +#!/bin/sh + +cat << EOF +{ + "image": { + "source_url": "$1", + "tag_list": "$2", + "image_url": "$3" + } +} +EOF diff --git a/tools/dbupload/main.go b/tools/dbupload/main.go new file mode 100644 index 0000000..cc07cd7 --- /dev/null +++ b/tools/dbupload/main.go @@ -0,0 +1,116 @@ +package main + +import ( + "bytes" + "encoding/json" + "flag" + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "strings" + "time" +) + +var ( + apiKeyLocation = flag.String("apikeyloc", "/home/xena/.local/share/within/db.key", "Derpibooru API key location") + cookieLocation = flag.String("cookieloc", "/home/xena/.db.cookie", "Location for magic cookie") +) + +func main() { + flag.Parse() + + if flag.NArg() == 0 { + fmt.Printf("%s: <image to upload>\n", os.Args[0]) + fmt.Printf("All files must have a manifest json file.\n") + flag.Usage() + } + + dbkey, err := ioutil.ReadFile(*apiKeyLocation) + if err != nil { + panic(err) + } + + mydbkey := strings.Split(string(dbkey), "\n")[0] + + cookie, err := ioutil.ReadFile(*cookieLocation) + if err != nil { + panic(err) + } + + image := flag.Arg(0) + + if strings.HasSuffix(image, ".json") { + log.Printf("Skipped %s...", image) + return + } + + metafin, err := os.Open(image + ".json") + if err != nil { + log.Fatal("image " + image + " MUST have description manifest for derpibooru") + } + defer metafin.Close() + + metabytes, err := ioutil.ReadAll(metafin) + if err != nil { + log.Fatal(err) + } + + var meta UploadImage + err = json.Unmarshal(metabytes, &meta) + if err != nil { + log.Fatal(err) + } + + imfin, err := os.Open(image) + if err != nil { + log.Fatal("cannot open " + image) + } + defer imfin.Close() + + if meta.Image.ImageURL == "" { + panic("need file uploaded somewhere?") + } + + outmetabytes, err := json.Marshal(&meta) + + req, err := http.NewRequest("POST", "https://derpibooru.org/images.json?key="+mydbkey, bytes.NewBuffer(outmetabytes)) + if err != nil { + panic(err) + } + + c := &http.Client{} + + req.Header = http.Header{ + "User-Agent": {"Xena's crappy upload tool"}, + "Cookie": {string(cookie)}, + "Content-Type": {"application/json"}, + } + + resp, err := c.Do(req) + if err != nil { + log.Printf("%#v", err) + fmt.Printf("Request ID: %s\n", resp.Header.Get("X-Request-Id")) + return + } + + if resp.StatusCode != 201 { + io.Copy(os.Stdout, resp.Body) + return + } + + respbytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Println(err) + return + } + + var i Image + json.Unmarshal(respbytes, &i) + + fmt.Printf("Uploaded as https://derpibooru.org/%d\n", i.IDNumber) + + time.Sleep(20 * time.Second) +} diff --git a/tools/discordaway/main.go b/tools/discordaway/main.go new file mode 100644 index 0000000..7b56f9e --- /dev/null +++ b/tools/discordaway/main.go @@ -0,0 +1,94 @@ +package main + +import ( + "bytes" + "log" + "os/exec" + "strings" + "time" + + "github.com/bwmarrin/discordgo" + "github.com/namsral/flag" +) + +var ( + cfg = flag.String("config", "/home/xena/.local/share/within/discordaway.cfg", "configuration file") + username = flag.String("username", "", "Discord username to use") + password = flag.String("password", "", "Discord password to use") +) + +func main() { + flag.Parse() + + dg, err := discordgo.New(*username, *password) + if err != nil { + log.Fatal(err) + } + + dg.AddHandler(messageCreate) + + err = dg.Open() + if err != nil { + log.Fatal(err) + } + + log.Println("monitoring tmux status...") + t := time.NewTicker(300 * time.Second) + + ok, err := isTmuxAttached() + log.Println(ok, err) + + for { + select { + case <-t.C: + at, err := isTmuxAttached() + if err != nil { + log.Println(err) + return + } + + if at { + log.Println("Cadey is away, marking as away on Discord") + dg.UpdateStatus(600, "around with reality for some reason") + } else { + log.Println("Cadey is back!!!") + dg.UpdateStatus(0, "") + } + } + } +} + +func isTmuxAttached() (bool, error) { + cmd := exec.Command("/usr/bin/tmux", "ls", "-F", "#{?session_attached,attached,not attached}") + output, err := cmd.Output() + if err != nil { + return false, err + } + + return bytes.HasPrefix(output, []byte("attached")), nil +} + +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + if m.Author.ID == "72838115944828928" { + content := m.ContentWithMentionsReplaced() + + if strings.HasPrefix(content, "TODO: ") { + todoBody := strings.SplitN(content, "TODO: ", 2)[1] + + log.Printf("todo added: %s", todoBody) + todoFields := strings.Fields(todoBody) + + cmd := exec.Command("/home/xena/go/bin/todo", append([]string{"add"}, todoFields...)...) + err := cmd.Start() + if err != nil { + log.Println(err) + return + } + + err = cmd.Wait() + if err != nil { + log.Println(err) + } + } + } +} diff --git a/tools/dokku/.gitignore b/tools/dokku/.gitignore new file mode 100644 index 0000000..ae9366d --- /dev/null +++ b/tools/dokku/.gitignore @@ -0,0 +1 @@ +dokku diff --git a/tools/dokku/README.md b/tools/dokku/README.md new file mode 100644 index 0000000..a017917 --- /dev/null +++ b/tools/dokku/README.md @@ -0,0 +1,55 @@ +Dokku +===== + +This is a simple command line tool to interface with Dokku servers. This is +a port of my shell extension +[`dokku.zsh`](https://github.com/Xe/dotfiles/blob/master/.zsh/dokku.zsh) to +a nice Go binary. + +This takes a configuration file for defining multiple servers: + +```ini +[server "default"] +user = dokku +host = panel.apps.xeserv.us +sshkey = /.ssh/id_rsa +``` + +By default it will imply that the SSH key is `~/.ssh/id_rsa` and that the +username is `dokku`. By default the server named `default` will be used for +command execution. + +TODO +---- + +- [ ] Allow interactive commands +- [ ] Directly pipe stdin and stdout to the ssh connection + +--- + +``` +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to <http://unlicense.org/> +``` diff --git a/tools/dokku/config.go b/tools/dokku/config.go new file mode 100644 index 0000000..74c5e0b --- /dev/null +++ b/tools/dokku/config.go @@ -0,0 +1,11 @@ +package main + +type Config struct { + Server map[string]*Server +} + +type Server struct { + SSHKey string // if blank default key will be used. + Host string // hostname of the dokku server + User string // if blank username will be dokku +} diff --git a/tools/dokku/dokku.cfg.sample b/tools/dokku/dokku.cfg.sample new file mode 100644 index 0000000..b914ebe --- /dev/null +++ b/tools/dokku/dokku.cfg.sample @@ -0,0 +1,2 @@ +[server "default"] +host = panel.apps.xeserv.us diff --git a/tools/dokku/main.go b/tools/dokku/main.go new file mode 100644 index 0000000..659050f --- /dev/null +++ b/tools/dokku/main.go @@ -0,0 +1,61 @@ +package main // christine.website/go/tools/dokku + +import ( + "flag" + "fmt" + "log" + "os" + "strings" + + "code.google.com/p/gcfg" + "github.com/hypersleep/easyssh" +) + +var ( + cfgPath = flag.String("cfg", "", "configuration path, default is ~/.dokku.cfg") + serverName = flag.String("server", "default", "server to use out of dokku config") +) + +func main() { + flag.Parse() + + if *cfgPath == "" { + *cfgPath = os.Getenv("HOME") + "/.dokku.cfg" + } + + var cfg Config + err := gcfg.ReadFileInto(&cfg, *cfgPath) + if err != nil { + log.Fatal(err) + } + + var server *Server + var ok bool + + if server, ok = cfg.Server[*serverName]; !ok { + log.Fatalf("server %s not defined in configuration file %s", *serverName, *cfgPath) + } + + if server.User == "" { + server.User = "dokku" + } + + if server.SSHKey == "" { + server.SSHKey = "/.ssh/id_rsa" + } + + ssh := &easyssh.MakeConfig{ + User: server.User, + Server: server.Host, + Key: server.SSHKey, + } + + command := strings.Join(flag.Args(), " ") + + res, err := ssh.Run(command) + if err != nil { + log.Fatal(err) + } + + fmt.Print(res) +} diff --git a/tools/ghstat/.gitignore b/tools/ghstat/.gitignore new file mode 100644 index 0000000..7e9712b --- /dev/null +++ b/tools/ghstat/.gitignore @@ -0,0 +1 @@ +ghstat diff --git a/tools/ghstat/README.md b/tools/ghstat/README.md new file mode 100644 index 0000000..0ff7b52 --- /dev/null +++ b/tools/ghstat/README.md @@ -0,0 +1,9 @@ +# ghstat +-- +Command ghstat shows the status of GitHub via their status API. + + Usage of ./ghstat: + -message=false: show last message? + +This follows https://status.github.com/api for all but the list of all recent +status messages. diff --git a/tools/ghstat/doc.go b/tools/ghstat/doc.go new file mode 100644 index 0000000..634c3bc --- /dev/null +++ b/tools/ghstat/doc.go @@ -0,0 +1,10 @@ +/* +Command ghstat shows the status of GitHub via their status API. + + Usage of ./ghstat: + -message=false: show last message? + +This follows https://status.github.com/api for all but the list of all +recent status messages. +*/ +package main diff --git a/tools/ghstat/main.go b/tools/ghstat/main.go new file mode 100644 index 0000000..f86861e --- /dev/null +++ b/tools/ghstat/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + "time" +) + +var ( + messageFlag = flag.Bool("message", false, "show last message?") + + // TODO: implement + //shellFlag = flag.Bool("s", false, "show as shell prompt artifact") +) + +func main() { + flag.Parse() + + if *messageFlag { + m, err := getMessage() + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Last message:\n") + fmt.Printf("Status: %s\n", m.Status) + fmt.Printf("Message: %s\n", m.Body) + + t, err := time.Parse(time.RFC3339, m.CreatedOn) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Time: %s\n", t.Format(time.ANSIC)) + } else { + s, err := getStatus() + if err != nil { + log.Fatal(err) + } + + t, err := time.Parse(time.RFC3339, s.LastUpdated) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Status: %s (%s)\n", s.Status, t.Format(time.ANSIC)) + + if s.Status != "good" { + os.Exit(1) + } + } +} diff --git a/tools/ghstat/types.go b/tools/ghstat/types.go new file mode 100644 index 0000000..ef824bd --- /dev/null +++ b/tools/ghstat/types.go @@ -0,0 +1,60 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "net/http" +) + +type Status struct { + Status string `json:"status"` + LastUpdated string `json:"last_updated"` +} + +type Message struct { + Status string `json:"status"` + Body string `json:"body"` + CreatedOn string `json:"created_on"` +} + +func getMessage() (Message, error) { + m := Message{} + + resp, err := http.Get("https://status.github.com/api/last-message.json") + if err != nil { + return Message{}, err + } + defer resp.Body.Close() + content, err := ioutil.ReadAll(resp.Body) + if err != nil { + return Message{}, err + } + + err = json.Unmarshal(content, &m) + if err != nil { + return Message{}, err + } + + return m, nil +} + +func getStatus() (Status, error) { + s := Status{} + + resp, err := http.Get("https://status.github.com/api/status.json") + if err != nil { + return Status{}, err + } + defer resp.Body.Close() + content, err := ioutil.ReadAll(resp.Body) + if err != nil { + return Status{}, err + } + + err = json.Unmarshal(content, &s) + if err != nil { + return Status{}, err + } + + return s, nil +} diff --git a/tools/glue/CHEATSHEET.md b/tools/glue/CHEATSHEET.md new file mode 100644 index 0000000..0536567 --- /dev/null +++ b/tools/glue/CHEATSHEET.md @@ -0,0 +1,661 @@ +## `json` + +```lua +local json = require "json" +``` + +Json encoder/decoder + +The following functions are exposed by the library: + + decode(string): Decodes a JSON string. Returns nil and an error string if + the string could not be decoded. + encode(value): Encodes a value into a JSON string. Returns nil and an error + string if the value could not be encoded. + +## `xmlpath` + +```lua +local xmlpath = require "xmlpath" +``` + +XMLPath style iteration + + xml ="<bookist><book>x1</book><book>x2</book><book>x3</book></booklist>" + local xmlpath = require("xmlpath") + node,err = xmlpath.loadxml(xml) + path,err = xmlpath.compile("//book") + + it = path:iter(node) + for k,v in pairs(it) do + print(k,v:string()) + end + +## `http` + +```lua +local http = require("http") +``` + +HTTP client library + +### API + +- [`http.delete(url [, options])`](#httpdeleteurl--options) +- [`http.get(url [, options])`](#httpgeturl--options) +- [`http.head(url [, options])`](#httpheadurl--options) +- [`http.patch(url [, options])`](#httppatchurl--options) +- [`http.post(url [, options])`](#httpposturl--options) +- [`http.put(url [, options])`](#httpputurl--options) +- [`http.request(method, url [, options])`](#httprequestmethod-url--options) +- [`http.request_batch(requests)`](#httprequest_batchrequests) +- [`http.response`](#httpresponse) + +#### http.delete(url [, options]) + +**Attributes** + +| Name | Type | Description | +| ------- | ------ | ----------- | +| url | String | URL of the resource to load | +| options | Ta |
