Command line client for Write.as https://write.as/apps/cli
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

270 satır
6.5 KiB

  1. package main
  2. import (
  3. "os"
  4. "github.com/writeas/writeas-cli/commands"
  5. "github.com/writeas/writeas-cli/config"
  6. "github.com/writeas/writeas-cli/log"
  7. cli "gopkg.in/urfave/cli.v1"
  8. )
  9. func main() {
  10. initialize(appInfo["configDir"])
  11. cli.VersionFlag = cli.BoolFlag{
  12. Name: "version, V",
  13. Usage: "print the version",
  14. }
  15. // Run the app
  16. app := cli.NewApp()
  17. app.Name = "writeas"
  18. app.Version = config.Version
  19. app.Usage = "Publish text quickly"
  20. app.Authors = []cli.Author{
  21. {
  22. Name: "Write.as",
  23. Email: "hello@write.as",
  24. },
  25. }
  26. app.ExtraInfo = func() map[string]string {
  27. return appInfo
  28. }
  29. app.Action = commands.CmdPost
  30. app.Flags = config.PostFlags
  31. app.Commands = []cli.Command{
  32. {
  33. Name: "post",
  34. Usage: "Alias for default action: create post from stdin",
  35. Action: commands.CmdPost,
  36. Flags: config.PostFlags,
  37. Description: `Create a new post on Write.as from stdin.
  38. Use the --code flag to indicate that the post should use syntax
  39. highlighting. Or use the --font [value] argument to set the post's
  40. appearance, where [value] is mono, monospace (default), wrap (monospace
  41. font with word wrapping), serif, or sans.`,
  42. },
  43. {
  44. Name: "new",
  45. Usage: "Compose a new post from the command-line and publish",
  46. Description: `An alternative to piping data to the program.
  47. On Windows, this will use 'copy con' to start reading what you input from the
  48. prompt. Press F6 or Ctrl-Z then Enter to end input.
  49. On *nix, this will use the best available text editor, starting with the
  50. value set to the WRITEAS_EDITOR or EDITOR environment variable, or vim, or
  51. finally nano.
  52. Use the --code flag to indicate that the post should use syntax
  53. highlighting. Or use the --font [value] argument to set the post's
  54. appearance, where [value] is mono, monospace (default), wrap (monospace
  55. font with word wrapping), serif, or sans.
  56. If posting fails for any reason, 'writeas' will show you the temporary file
  57. location and how to pipe it to 'writeas' to retry.`,
  58. Action: commands.CmdNew,
  59. Flags: config.PostFlags,
  60. },
  61. {
  62. Name: "publish",
  63. Usage: "Publish a file to Write.as",
  64. Action: commands.CmdPublish,
  65. Flags: config.PostFlags,
  66. },
  67. {
  68. Name: "delete",
  69. Usage: "Delete a post",
  70. Action: commands.CmdDelete,
  71. Flags: []cli.Flag{
  72. cli.BoolFlag{
  73. Name: "tor, t",
  74. Usage: "Delete via Tor hidden service",
  75. },
  76. cli.IntFlag{
  77. Name: "tor-port",
  78. Usage: "Use a different port to connect to Tor",
  79. Value: 9150,
  80. },
  81. cli.BoolFlag{
  82. Name: "verbose, v",
  83. Usage: "Make the operation more talkative",
  84. },
  85. },
  86. },
  87. {
  88. Name: "update",
  89. Usage: "Update (overwrite) a post",
  90. Action: commands.CmdUpdate,
  91. Flags: []cli.Flag{
  92. cli.BoolFlag{
  93. Name: "tor, t",
  94. Usage: "Update via Tor hidden service",
  95. },
  96. cli.IntFlag{
  97. Name: "tor-port",
  98. Usage: "Use a different port to connect to Tor",
  99. Value: 9150,
  100. },
  101. cli.BoolFlag{
  102. Name: "code",
  103. Usage: "Specifies this post is code",
  104. },
  105. cli.StringFlag{
  106. Name: "font",
  107. Usage: "Sets post font to given value",
  108. },
  109. cli.BoolFlag{
  110. Name: "verbose, v",
  111. Usage: "Make the operation more talkative",
  112. },
  113. },
  114. },
  115. {
  116. Name: "get",
  117. Usage: "Read a raw post",
  118. Action: commands.CmdGet,
  119. Flags: []cli.Flag{
  120. cli.BoolFlag{
  121. Name: "tor, t",
  122. Usage: "Get from Tor hidden service",
  123. },
  124. cli.IntFlag{
  125. Name: "tor-port",
  126. Usage: "Use a different port to connect to Tor",
  127. Value: 9150,
  128. },
  129. cli.BoolFlag{
  130. Name: "verbose, v",
  131. Usage: "Make the operation more talkative",
  132. },
  133. },
  134. },
  135. {
  136. Name: "add",
  137. Usage: "Add an existing post locally",
  138. Description: `A way to add an existing post to your local store for easy editing later.
  139. This requires a post ID (from https://write.as/[ID]) and an Edit Token
  140. (exported from another Write.as client, such as the Android app).
  141. `,
  142. Action: commands.CmdAdd,
  143. },
  144. {
  145. Name: "posts",
  146. Usage: "List all of your posts",
  147. Description: "This will list only local posts.",
  148. Action: commands.CmdListPosts,
  149. Flags: []cli.Flag{
  150. cli.BoolFlag{
  151. Name: "id",
  152. Usage: "Show list with post IDs (default)",
  153. },
  154. cli.BoolFlag{
  155. Name: "md",
  156. Usage: "Use with --url to return URLs with Markdown enabled",
  157. },
  158. cli.BoolFlag{
  159. Name: "url",
  160. Usage: "Show list with URLs",
  161. },
  162. cli.BoolFlag{
  163. Name: "verbose, v",
  164. Usage: "Show verbose post listing, including Edit Tokens",
  165. },
  166. },
  167. }, {
  168. Name: "blogs",
  169. Usage: "List blogs",
  170. Action: commands.CmdCollections,
  171. Flags: []cli.Flag{
  172. cli.BoolFlag{
  173. Name: "url",
  174. Usage: "Show list with URLs",
  175. },
  176. },
  177. }, {
  178. Name: "claim",
  179. Usage: "Claim local unsynced posts",
  180. Action: commands.CmdClaim,
  181. Description: "This will claim any unsynced posts local to this machine. To see which posts these are run: writeas posts.",
  182. Flags: []cli.Flag{
  183. cli.BoolFlag{
  184. Name: "verbose, v",
  185. Usage: "Make the operation more talkative",
  186. },
  187. },
  188. },
  189. {
  190. Name: "auth",
  191. Usage: "Authenticate with Write.as",
  192. Action: commands.CmdAuth,
  193. Flags: []cli.Flag{
  194. cli.BoolFlag{
  195. Name: "tor, t",
  196. Usage: "Authenticate via Tor hidden service",
  197. },
  198. cli.IntFlag{
  199. Name: "tor-port",
  200. Usage: "Use a different port to connect to Tor",
  201. Value: 9150,
  202. },
  203. cli.BoolFlag{
  204. Name: "verbose, v",
  205. Usage: "Make the operation more talkative",
  206. },
  207. },
  208. },
  209. {
  210. Name: "logout",
  211. Usage: "Log out of Write.as",
  212. Action: commands.CmdLogOut,
  213. Flags: []cli.Flag{
  214. cli.BoolFlag{
  215. Name: "tor, t",
  216. Usage: "Authenticate via Tor hidden service",
  217. },
  218. cli.IntFlag{
  219. Name: "tor-port",
  220. Usage: "Use a different port to connect to Tor",
  221. Value: 9150,
  222. },
  223. cli.BoolFlag{
  224. Name: "verbose, v",
  225. Usage: "Make the operation more talkative",
  226. },
  227. },
  228. },
  229. }
  230. cli.CommandHelpTemplate = `NAME:
  231. {{.Name}} - {{.Usage}}
  232. USAGE:
  233. writeas {{.Name}}{{if .Flags}} [command options]{{end}} [arguments...]{{if .Description}}
  234. DESCRIPTION:
  235. {{.Description}}{{end}}{{if .Flags}}
  236. OPTIONS:
  237. {{range .Flags}}{{.}}
  238. {{end}}{{ end }}
  239. `
  240. app.Run(os.Args)
  241. }
  242. func initialize(dataDirName string) {
  243. // Ensure we have a data directory to use
  244. if !config.DataDirExists(dataDirName) {
  245. err := config.CreateDataDir(dataDirName)
  246. if err != nil {
  247. if config.Debug() {
  248. panic(err)
  249. } else {
  250. log.Errorln("Error creating data directory: %s", err)
  251. return
  252. }
  253. }
  254. }
  255. }