Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10384318
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Subscribers
None
View Options
diff --git a/cmd/writeas/api.go b/cmd/writeas/api.go
index 4faadf7..3c22a26 100644
--- a/cmd/writeas/api.go
+++ b/cmd/writeas/api.go
@@ -1,200 +1,198 @@
package main
import (
"fmt"
+ "path/filepath"
+
"github.com/atotto/clipboard"
- "github.com/writeas/go-writeas"
"github.com/writeas/writeas-cli/fileutils"
- "gopkg.in/urfave/cli.v1"
- "path/filepath"
+ writeas "go.code.as/writeas.v2"
+ cli "gopkg.in/urfave/cli.v1"
)
const (
defaultUserAgent = "writeas-cli v" + version
)
func client(userAgent string, tor bool) *writeas.Client {
var client *writeas.Client
if tor {
client = writeas.NewTorClient(torPort)
} else {
client = writeas.NewClient()
}
client.UserAgent = userAgent
return client
}
func newClient(c *cli.Context) *writeas.Client {
var client *writeas.Client
if isTor(c) {
client = writeas.NewTorClient(torPort)
} else {
client = writeas.NewClient()
}
client.UserAgent = userAgent(c)
u, _ := loadUser()
if u != nil {
client.SetToken(u.AccessToken)
}
return client
}
// DoFetch retrieves the Write.as post with the given friendlyID,
// optionally via the Tor hidden service.
func DoFetch(friendlyID, ua string, tor bool) error {
cl := client(ua, tor)
p, err := cl.GetPost(friendlyID)
if err != nil {
return err
}
fmt.Printf("%s\n", string(p.Content))
return nil
}
// DoPost creates a Write.as post, returning an error if it was
// unsuccessful.
func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) error {
cl := newClient(c)
pp := &writeas.PostParams{
// TODO: extract title
Content: string(post),
Font: getFont(code, font),
Collection: collection(c),
}
p, err := cl.CreatePost(pp)
if err != nil {
return fmt.Errorf("Unable to post: %v", err)
}
var url string
if p.Collection != nil {
url = p.Collection.URL + p.Slug
} else {
if tor {
url = torBaseURL
} else {
url = writeasBaseURL
}
url += "/" + p.ID
}
if cl.Token() == "" {
// Store post locally, since we're not authenticated
addPost(p.ID, p.Token)
}
// Copy URL to clipboard
err = clipboard.WriteAll(string(url))
if err != nil {
Errorln("writeas: Didn't copy to clipboard: %s", err)
} else {
Info(c, "Copied to clipboard.")
}
// Output URL
fmt.Printf("%s\n", url)
return nil
}
// DoUpdate updates the given post on Write.as.
func DoUpdate(c *cli.Context, post []byte, friendlyID, token, font string, tor, code bool) error {
cl := newClient(c)
params := writeas.PostParams{
ID: friendlyID,
Token: token,
Content: string(post),
// TODO: extract title
}
if code || font != "" {
params.Font = getFont(code, font)
}
- _, err := cl.UpdatePost(¶ms)
+ _, err := cl.UpdatePost(params.ID, params.Token, ¶ms)
if err != nil {
if debug {
ErrorlnQuit("Problem updating: %v", err)
}
return fmt.Errorf("Post doesn't exist, or bad edit token given.")
}
if tor {
Info(c, "Post updated via hidden service.")
} else {
Info(c, "Post updated.")
}
return nil
}
// DoDelete deletes the given post on Write.as.
func DoDelete(c *cli.Context, friendlyID, token string, tor bool) error {
cl := newClient(c)
- err := cl.DeletePost(&writeas.PostParams{
- ID: friendlyID,
- Token: token,
- })
+ err := cl.DeletePost(friendlyID, token)
if err != nil {
if debug {
ErrorlnQuit("Problem deleting: %v", err)
}
return fmt.Errorf("Post doesn't exist, or bad edit token given.")
}
if tor {
Info(c, "Post deleted from hidden service.")
} else {
Info(c, "Post deleted.")
}
removePost(friendlyID)
return nil
}
func DoLogIn(c *cli.Context, username, password string) error {
cl := client(userAgent(c), isTor(c))
u, err := cl.LogIn(username, password)
if err != nil {
if debug {
ErrorlnQuit("Problem logging in: %v", err)
}
return err
}
err = saveUser(u)
if err != nil {
return err
}
fmt.Printf("Logged in as %s.\n", u.User.Username)
return nil
}
func DoLogOut(c *cli.Context) error {
cl := newClient(c)
if cl.Token() == "" {
return fmt.Errorf("Not currently logged in. Authenticate with: writeas auth -u <username>")
}
err := cl.LogOut()
if err != nil {
if debug {
ErrorlnQuit("Problem logging out: %v", err)
}
return err
}
// Delete local user data
err = fileutils.DeleteFile(filepath.Join(userDataDir(), userFile))
if err != nil {
return err
}
return nil
}
diff --git a/cmd/writeas/userconfig.go b/cmd/writeas/userconfig.go
index b8b5d64..7c5d280 100644
--- a/cmd/writeas/userconfig.go
+++ b/cmd/writeas/userconfig.go
@@ -1,84 +1,85 @@
package main
import (
"encoding/json"
- "github.com/writeas/go-writeas"
- "github.com/writeas/writeas-cli/fileutils"
- "gopkg.in/ini.v1"
"io/ioutil"
"path/filepath"
+
+ "github.com/writeas/writeas-cli/fileutils"
+ writeas "go.code.as/writeas.v2"
+ ini "gopkg.in/ini.v1"
)
const (
userConfigFile = "config.ini"
userFile = "user.json"
)
type (
APIConfig struct {
}
UserConfig struct {
API APIConfig `ini:"api"`
}
)
func loadConfig() (*UserConfig, error) {
cfg, err := ini.LooseLoad(filepath.Join(userDataDir(), userConfigFile))
if err != nil {
return nil, err
}
// Parse INI file
uc := &UserConfig{}
err = cfg.MapTo(uc)
if err != nil {
return nil, err
}
return uc, nil
}
func saveConfig(uc *UserConfig) error {
cfg := ini.Empty()
err := ini.ReflectFrom(cfg, uc)
if err != nil {
return err
}
return cfg.SaveTo(filepath.Join(userDataDir(), userConfigFile))
}
func loadUser() (*writeas.AuthUser, error) {
fname := filepath.Join(userDataDir(), userFile)
userJSON, err := ioutil.ReadFile(fname)
if err != nil {
if !fileutils.Exists(fname) {
// Don't return a file-not-found error
return nil, nil
}
return nil, err
}
// Parse JSON file
u := &writeas.AuthUser{}
err = json.Unmarshal(userJSON, u)
if err != nil {
return nil, err
}
return u, nil
}
func saveUser(u *writeas.AuthUser) error {
// Marshal struct into pretty-printed JSON
userJSON, err := json.MarshalIndent(u, "", " ")
if err != nil {
return err
}
// Save file
err = ioutil.WriteFile(filepath.Join(userDataDir(), userFile), userJSON, 0600)
if err != nil {
return err
}
return nil
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Nov 23, 11:23 AM (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3104533
Attached To
rWCLI writeas-cli
Event Timeline
Log In to Comment