Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10433305
posts.go
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
posts.go
View Options
package
main
import
(
"fmt"
"github.com/writeas/writeas-cli/utils"
"os"
"path/filepath"
"strings"
)
const
(
POSTS_FILE
=
"posts.psv"
SEPARATOR
=
`|`
)
type
Post
struct
{
ID
string
EditToken
string
}
func
userDataDir
()
string
{
return
filepath
.
Join
(
parentDataDir
(),
DATA_DIR_NAME
)
}
func
dataDirExists
()
bool
{
return
fileutils
.
Exists
(
userDataDir
())
}
func
createDataDir
()
{
err
:=
os
.
Mkdir
(
userDataDir
(),
0700
)
if
err
!=
nil
&&
DEBUG
{
panic
(
err
)
}
}
func
addPost
(
id
,
token
string
)
{
f
,
err
:=
os
.
OpenFile
(
filepath
.
Join
(
userDataDir
(),
POSTS_FILE
),
os
.
O_WRONLY
|
os
.
O_APPEND
|
os
.
O_CREATE
,
0600
)
if
err
!=
nil
{
if
DEBUG
{
panic
(
err
)
}
else
{
return
}
}
defer
f
.
Close
()
l
:=
fmt
.
Sprintf
(
"%s%s%s\n"
,
id
,
SEPARATOR
,
token
)
if
_
,
err
=
f
.
WriteString
(
l
);
err
!=
nil
&&
DEBUG
{
panic
(
err
)
}
}
func
tokenFromID
(
id
string
)
string
{
post
:=
fileutils
.
FindLine
(
filepath
.
Join
(
userDataDir
(),
POSTS_FILE
),
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
(),
POSTS_FILE
),
id
)
}
func
getPosts
()
*
[]
Post
{
lines
:=
fileutils
.
ReadData
(
filepath
.
Join
(
userDataDir
(),
POSTS_FILE
))
posts
:=
[]
Post
{}
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
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Jan 20, 2:47 AM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3135545
Attached To
rWCLI writeas-cli
Event Timeline
Log In to Comment