Command line client for Write.as https://write.as/apps/cli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 rivejä
6.7 KiB

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