Page MenuHomeMusing Studio

posts.go
No OneTemporary

posts.go

package main
import (
"fmt"
"github.com/writeas/writeas-cli/utils"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const (
postsFile = "posts.psv"
separator = `|`
)
// Post holds the basic authentication information for a Write.as post.
type Post struct {
ID string
EditToken string
}
func userDataDir() string {
return filepath.Join(parentDataDir(), dataDirName)
}
func dataDirExists() bool {
return fileutils.Exists(userDataDir())
}
func createDataDir() {
err := os.Mkdir(userDataDir(), 0700)
if err != nil {
if debug {
panic(err)
} else {
fmt.Printf("Error creating data directory: %s\n", err)
return
}
}
}
func addPost(id, token string) error {
f, err := os.OpenFile(filepath.Join(userDataDir(), postsFile), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return fmt.Errorf("Error creating local posts list: %s\n", err)
}
defer f.Close()
l := fmt.Sprintf("%s%s%s\n", id, separator, token)
if _, err = f.WriteString(l); err != nil {
return fmt.Errorf("Error writing to local posts list: %s\n", err)
}
return nil
}
func tokenFromID(id string) string {
post := fileutils.FindLine(filepath.Join(userDataDir(), postsFile), id)
if post == "" {
return ""
}
parts := strings.Split(post, separator)
if len(parts) < 2 {
return ""
}
return parts[1]
}
func removePost(id string) {
fileutils.RemoveLine(filepath.Join(userDataDir(), postsFile), id)
}
func getPosts() *[]Post {
lines := fileutils.ReadData(filepath.Join(userDataDir(), postsFile))
posts := []Post{}
if lines != nil && len(*lines) > 0 {
parts := make([]string, 2)
for _, l := range *lines {
parts = strings.Split(l, separator)
if len(parts) < 2 {
continue
}
posts = append(posts, Post{ID: parts[0], EditToken: parts[1]})
}
}
return &posts
}
func composeNewPost() (string, *[]byte) {
f, err := fileutils.TempFile(os.TempDir(), "WApost", "txt")
if err != nil {
if debug {
panic(err)
} else {
fmt.Printf("Error creating temp file: %s\n", err)
return "", nil
}
}
f.Close()
cmd := editPostCmd(f.Name())
if cmd == nil {
os.Remove(f.Name())
fmt.Println(noEditorErr)
return "", nil
}
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
if err := cmd.Start(); err != nil {
os.Remove(f.Name())
if debug {
panic(err)
} else {
fmt.Printf("Error starting editor: %s\n", err)
return "", nil
}
}
// If something fails past this point, the temporary post file won't be
// removed automatically. Calling function should handle this.
if err := cmd.Wait(); err != nil {
if debug {
panic(err)
} else {
fmt.Printf("Editor finished with error: %s\n", err)
return "", nil
}
}
post, err := ioutil.ReadFile(f.Name())
if err != nil {
if debug {
panic(err)
} else {
fmt.Printf("Error reading post: %s\n", err)
return "", nil
}
}
return f.Name(), &post
}

File Metadata

Mime Type
text/plain
Expires
Fri, Oct 10, 2:15 AM (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3441005

Event Timeline