Selaa lähdekoodia

Save post IDs and tokens locally

Plus only show post URL upon success
tags/v0.1
Matt Baer 9 vuotta sitten
vanhempi
commit
0742ca9909
5 muutettua tiedostoa jossa 104 lisäystä ja 1 poistoa
  1. +21
    -1
      cli.go
  2. +38
    -0
      posts.go
  3. +18
    -0
      posts_nix.go
  4. +14
    -0
      posts_win.go
  5. +13
    -0
      utils/fileutils.go

+ 21
- 1
cli.go Näytä tiedosto

@@ -12,6 +12,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
)

const (
@@ -22,6 +23,9 @@ const (
)

func main() {
initialize()

// Run the app
app := cli.NewApp()
app.Name = "writeas"
app.Version = VERSION
@@ -78,6 +82,13 @@ func main() {
app.Run(os.Args)
}

func initialize() {
// Ensure we have a data directory to use
if !dataDirExists() {
createDataDir()
}
}

func readStdIn() []byte {
numBytes, numChunks := int64(0), int64(0)
r := bufio.NewReader(os.Stdin)
@@ -239,7 +250,16 @@ func DoPost(post []byte, encrypt, tor bool) {
if resp.StatusCode == http.StatusOK {
content, err := ioutil.ReadAll(resp.Body)
check(err)
fmt.Printf("%s\n", string(content))

nlPos := strings.Index(string(content), "\n")
url := content[:nlPos]
idPos := strings.LastIndex(string(url), "/") + 1
id := string(url[idPos:])
token := string(content[nlPos+1:])

addPost(id, token)

fmt.Printf("%s\n", url)
} else {
fmt.Printf("Unable to post: %s\n", resp.Status)
}


+ 38
- 0
posts.go Näytä tiedosto

@@ -0,0 +1,38 @@
package main

import (
"fmt"
"github.com/writeas/writeas-cli/utils"
"os"
)

const (
POSTS_FILE = "posts.psv"
SEPARATOR = `|`
)

func userDataDir() string {
return fmt.Sprintf("%s/%s", parentDataDir(), DATA_DIR_NAME)
}

func dataDirExists() bool {
return fileutils.Exists(userDataDir())
}

func createDataDir() {
os.Mkdir(userDataDir(), 0700)
}

func addPost(id, token string) {
f, err := os.OpenFile(userDataDir()+"/"+POSTS_FILE, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()

l := fmt.Sprintf("%s%s%s\n", id, SEPARATOR, token)

if _, err = f.WriteString(l); err != nil {
panic(err)
}
}

+ 18
- 0
posts_nix.go Näytä tiedosto

@@ -0,0 +1,18 @@
// +build !windows

package main

import (
"github.com/mitchellh/go-homedir"
)

const DATA_DIR_NAME = ".writeas"

func parentDataDir() string {
dir, err := homedir.Dir()
if err != nil {
panic(err)
}

return dir
}

+ 14
- 0
posts_win.go Näytä tiedosto

@@ -0,0 +1,14 @@
// +build windows

package main

import (
"github.com/luisiturrios/gowin"
)

const DATA_DIR_NAME = "Write.as"

func parentDataDir() string {
folders := gowin.ShellFolders{gowin.USER}
return folders.AppData()
}

+ 13
- 0
utils/fileutils.go Näytä tiedosto

@@ -0,0 +1,13 @@
package fileutils

import (
"os"
)

// Exists returns whether or not the given file exists
func Exists(p string) bool {
if _, err := os.Stat(p); err == nil {
return true
}
return false
}

Ladataan…
Peruuta
Tallenna