// support for fpaste package paste import ( "bytes" "fmt" "io" "net/http" "net/url" "os" "os/user" "strings" ) var ( // paste.centos.org does things in minutes fpasteTimeMap = map[string]string{ "1hour": "60", "1day": "1440", "1week": "10080", } rawSlug = "view/raw" // I'll ask the maintainer of the paste site if I could potentially obtain // our own API key instead of using the one from the fpaste script fpasteBaseURL = "https://paste.centos.org" fpasteAPIKey = "5uZ30dTZE1a5V0WYhNwcMddBRDpk6UzuzMu-APKM38iMHacxdA0n4vCqA34avNyt" fpasteURL = "https://paste.centos.org/api/create" fpasteUserAgent = "rpaste/0.2.0" fpasteTitle = "UNTITLED" // we'll need to make this changeable in the future fpastePrivate = "1" fpasteUserName = "user" ) func Fpaste(life string, lexer string, fileContent string) error { // Set username as the author user, err := user.Current() if err != nil { fmt.Println("Username could not be found") } else { fpasteUserName = user.Name } // Stikkit doesn't seem to take in json data. It takes in form data and // general headers. form := url.Values{} form.Add("title", fpasteTitle) form.Add("expire", fpasteTimeMap[life]) form.Add("private", fpastePrivate) form.Add("lang", lexer) form.Add("name", fpasteUserName) form.Add("text", fileContent) rurl, err := url.Parse(fpasteURL) if err != nil { fmt.Println("Failed to construct URL") os.Exit(1) } // This helps us do a proper urlencode for the API key, instead of just // stashing it in the var itself. vs := rurl.Query() vs.Add("apikey", fpasteAPIKey) rurl.RawQuery = vs.Encode() fmt.Println("Uploading...") resp, err := http.PostForm(rurl.String(), form) // The response comes back as bytes, so we copy the response buffer into io // and string it out buf := &bytes.Buffer{} io.Copy(buf, resp.Body) pasteURL := buf.String() if resp.StatusCode == 200 { urlish := strings.Replace(pasteURL, "\n", "", -1) ss := strings.Split(urlish, "/") slug := ss[len(ss)-1] fmt.Printf("Paste URL: %s", pasteURL) fmt.Printf("Raw URL: %s/%s/%s\n", fpasteBaseURL, rawSlug, slug) } else { fmt.Printf("%s", http.StatusText(resp.StatusCode)) } return nil }