1
0

note.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "github.com/AlecAivazis/survey/v2"
  8. "github.com/olekukonko/tablewriter"
  9. "github.com/urfave/cli/v3"
  10. )
  11. // NoteCommand returns the note command
  12. func NoteCommand() *cli.Command {
  13. return &cli.Command{
  14. Name: "note",
  15. Usage: "Manage notes",
  16. Description: `Manage ARP notes. Notes are text entries belonging to users and associated with services.
  17. Use this command to create, list, update, and delete notes.`,
  18. Commands: []*cli.Command{
  19. {
  20. Name: "list",
  21. Aliases: []string{"ls"},
  22. Usage: "List all notes",
  23. Flags: []cli.Flag{
  24. &cli.BoolFlag{
  25. Name: "json",
  26. Aliases: []string{"j"},
  27. Usage: "Output as JSON",
  28. },
  29. },
  30. Action: noteList,
  31. },
  32. {
  33. Name: "get",
  34. Usage: "Get a note by ID",
  35. Flags: []cli.Flag{
  36. &cli.StringFlag{
  37. Name: "id",
  38. Aliases: []string{"i"},
  39. Usage: "Note ID",
  40. Required: true,
  41. },
  42. &cli.BoolFlag{
  43. Name: "json",
  44. Aliases: []string{"j"},
  45. Usage: "Output as JSON",
  46. },
  47. },
  48. Action: noteGet,
  49. },
  50. {
  51. Name: "create",
  52. Usage: "Create a new note",
  53. Action: noteCreate,
  54. Flags: []cli.Flag{
  55. &cli.StringFlag{
  56. Name: "title",
  57. Aliases: []string{"t"},
  58. Usage: "Note title",
  59. },
  60. &cli.StringFlag{
  61. Name: "content",
  62. Aliases: []string{"c"},
  63. Usage: "Note content",
  64. },
  65. &cli.StringFlag{
  66. Name: "user",
  67. Aliases: []string{"u"},
  68. Usage: "User ID",
  69. },
  70. &cli.StringFlag{
  71. Name: "service",
  72. Aliases: []string{"s"},
  73. Usage: "Service ID",
  74. },
  75. },
  76. },
  77. {
  78. Name: "update",
  79. Usage: "Update a note",
  80. Action: noteUpdate,
  81. Flags: []cli.Flag{
  82. &cli.StringFlag{
  83. Name: "id",
  84. Aliases: []string{"i"},
  85. Usage: "Note ID",
  86. Required: true,
  87. },
  88. &cli.StringFlag{
  89. Name: "title",
  90. Aliases: []string{"t"},
  91. Usage: "Note title",
  92. },
  93. &cli.StringFlag{
  94. Name: "content",
  95. Aliases: []string{"c"},
  96. Usage: "Note content",
  97. },
  98. &cli.StringFlag{
  99. Name: "user",
  100. Aliases: []string{"u"},
  101. Usage: "User ID",
  102. },
  103. &cli.StringFlag{
  104. Name: "service",
  105. Aliases: []string{"s"},
  106. Usage: "Service ID",
  107. },
  108. },
  109. },
  110. {
  111. Name: "delete",
  112. Usage: "Delete a note",
  113. Action: noteDelete,
  114. Flags: []cli.Flag{
  115. &cli.StringFlag{
  116. Name: "id",
  117. Aliases: []string{"i"},
  118. Usage: "Note ID",
  119. Required: true,
  120. },
  121. &cli.BoolFlag{
  122. Name: "yes",
  123. Aliases: []string{"y"},
  124. Usage: "Skip confirmation",
  125. },
  126. },
  127. },
  128. },
  129. }
  130. }
  131. type Note struct {
  132. ID string `json:"id"`
  133. Title string `json:"title"`
  134. Content string `json:"content"`
  135. UserID string `json:"userId"`
  136. User *User `json:"user"`
  137. ServiceID string `json:"serviceId"`
  138. Service *Service `json:"service"`
  139. CreatedAt string `json:"createdAt"`
  140. UpdatedAt string `json:"updatedAt"`
  141. }
  142. func noteList(ctx context.Context, cmd *cli.Command) error {
  143. c, cfg, err := GetClient(ctx, cmd)
  144. if err != nil {
  145. return err
  146. }
  147. if err := RequireAuth(cfg); err != nil {
  148. return err
  149. }
  150. query := "query Notes { notes { id title content userId user { id email } serviceId createdAt updatedAt } }"
  151. resp, err := c.Query(query, nil)
  152. if err != nil {
  153. return err
  154. }
  155. var result struct {
  156. Notes []Note `json:"notes"`
  157. }
  158. if err := json.Unmarshal(resp.Data, &result); err != nil {
  159. return err
  160. }
  161. if cmd.Bool("json") {
  162. enc := json.NewEncoder(os.Stdout)
  163. enc.SetIndent("", " ")
  164. return enc.Encode(result.Notes)
  165. }
  166. if len(result.Notes) == 0 {
  167. fmt.Println("No notes found.")
  168. return nil
  169. }
  170. table := tablewriter.NewWriter(os.Stdout)
  171. table.Header([]string{"ID", "Title", "User", "Service ID", "Created At"})
  172. for _, n := range result.Notes {
  173. userEmail := ""
  174. if n.User != nil {
  175. userEmail = n.User.Email
  176. }
  177. table.Append([]string{n.ID, n.Title, userEmail, n.ServiceID, n.CreatedAt})
  178. }
  179. table.Render()
  180. return nil
  181. }
  182. func noteGet(ctx context.Context, cmd *cli.Command) error {
  183. c, cfg, err := GetClient(ctx, cmd)
  184. if err != nil {
  185. return err
  186. }
  187. if err := RequireAuth(cfg); err != nil {
  188. return err
  189. }
  190. id := cmd.String("id")
  191. query := "query Note($id: ID!) { note(id: $id) { id title content userId user { id email } serviceId service { id name } createdAt updatedAt } }"
  192. resp, err := c.Query(query, map[string]interface{}{"id": id})
  193. if err != nil {
  194. return err
  195. }
  196. var result struct {
  197. Note *Note `json:"note"`
  198. }
  199. if err := json.Unmarshal(resp.Data, &result); err != nil {
  200. return err
  201. }
  202. if result.Note == nil {
  203. return fmt.Errorf("note not found")
  204. }
  205. if cmd.Bool("json") {
  206. enc := json.NewEncoder(os.Stdout)
  207. enc.SetIndent("", " ")
  208. return enc.Encode(result.Note)
  209. }
  210. n := result.Note
  211. fmt.Printf("ID: %s\n", n.ID)
  212. fmt.Printf("Title: %s\n", n.Title)
  213. fmt.Printf("Content: %s\n", n.Content)
  214. if n.User != nil {
  215. fmt.Printf("User: %s (%s)\n", n.User.Email, n.UserID)
  216. } else {
  217. fmt.Printf("User ID: %s\n", n.UserID)
  218. }
  219. if n.Service != nil {
  220. fmt.Printf("Service: %s (%s)\n", n.Service.Name, n.ServiceID)
  221. } else {
  222. fmt.Printf("Service ID: %s\n", n.ServiceID)
  223. }
  224. fmt.Printf("Created At: %s\n", n.CreatedAt)
  225. fmt.Printf("Updated At: %s\n", n.UpdatedAt)
  226. return nil
  227. }
  228. func noteCreate(ctx context.Context, cmd *cli.Command) error {
  229. c, cfg, err := GetClient(ctx, cmd)
  230. if err != nil {
  231. return err
  232. }
  233. if err := RequireAuth(cfg); err != nil {
  234. return err
  235. }
  236. title := cmd.String("title")
  237. content := cmd.String("content")
  238. userID := cmd.String("user")
  239. serviceID := cmd.String("service")
  240. if title == "" {
  241. prompt := &survey.Input{Message: "Title:"}
  242. if err := survey.AskOne(prompt, &title, survey.WithValidator(survey.Required)); err != nil {
  243. return err
  244. }
  245. }
  246. if content == "" {
  247. prompt := &survey.Multiline{Message: "Content:"}
  248. if err := survey.AskOne(prompt, &content, survey.WithValidator(survey.Required)); err != nil {
  249. return err
  250. }
  251. }
  252. if userID == "" {
  253. prompt := &survey.Input{Message: "User ID:"}
  254. if err := survey.AskOne(prompt, &userID, survey.WithValidator(survey.Required)); err != nil {
  255. return err
  256. }
  257. }
  258. if serviceID == "" {
  259. prompt := &survey.Input{Message: "Service ID:"}
  260. if err := survey.AskOne(prompt, &serviceID, survey.WithValidator(survey.Required)); err != nil {
  261. return err
  262. }
  263. }
  264. mutation := `mutation CreateNote($input: NewNote!) { createNote(input: $input) { id title content userId user { id email } serviceId createdAt updatedAt } }`
  265. input := map[string]interface{}{
  266. "title": title,
  267. "content": content,
  268. "userId": userID,
  269. "serviceId": serviceID,
  270. }
  271. resp, err := c.Mutation(mutation, map[string]interface{}{"input": input})
  272. if err != nil {
  273. return err
  274. }
  275. var result struct {
  276. CreateNote *Note `json:"createNote"`
  277. }
  278. if err := json.Unmarshal(resp.Data, &result); err != nil {
  279. return err
  280. }
  281. if result.CreateNote == nil {
  282. return fmt.Errorf("failed to create note")
  283. }
  284. fmt.Printf("Note created successfully!\n")
  285. fmt.Printf("ID: %s\n", result.CreateNote.ID)
  286. fmt.Printf("Title: %s\n", result.CreateNote.Title)
  287. return nil
  288. }
  289. func noteUpdate(ctx context.Context, cmd *cli.Command) error {
  290. c, cfg, err := GetClient(ctx, cmd)
  291. if err != nil {
  292. return err
  293. }
  294. if err := RequireAuth(cfg); err != nil {
  295. return err
  296. }
  297. id := cmd.String("id")
  298. title := cmd.String("title")
  299. content := cmd.String("content")
  300. userID := cmd.String("user")
  301. serviceID := cmd.String("service")
  302. if title == "" && content == "" && userID == "" && serviceID == "" {
  303. fmt.Println("No updates provided. Use flags to specify what to update.")
  304. return nil
  305. }
  306. input := make(map[string]interface{})
  307. if title != "" {
  308. input["title"] = title
  309. }
  310. if content != "" {
  311. input["content"] = content
  312. }
  313. if userID != "" {
  314. input["userId"] = userID
  315. }
  316. if serviceID != "" {
  317. input["serviceId"] = serviceID
  318. }
  319. mutation := `mutation UpdateNote($id: ID!, $input: UpdateNoteInput!) { updateNote(id: $id, input: $input) { id title content userId serviceId createdAt updatedAt } }`
  320. resp, err := c.Mutation(mutation, map[string]interface{}{"id": id, "input": input})
  321. if err != nil {
  322. return err
  323. }
  324. var result struct {
  325. UpdateNote *Note `json:"updateNote"`
  326. }
  327. if err := json.Unmarshal(resp.Data, &result); err != nil {
  328. return err
  329. }
  330. if result.UpdateNote == nil {
  331. return fmt.Errorf("note not found")
  332. }
  333. fmt.Printf("Note updated successfully!\n")
  334. fmt.Printf("ID: %s\n", result.UpdateNote.ID)
  335. fmt.Printf("Title: %s\n", result.UpdateNote.Title)
  336. return nil
  337. }
  338. func noteDelete(ctx context.Context, cmd *cli.Command) error {
  339. c, cfg, err := GetClient(ctx, cmd)
  340. if err != nil {
  341. return err
  342. }
  343. if err := RequireAuth(cfg); err != nil {
  344. return err
  345. }
  346. id := cmd.String("id")
  347. skipConfirm := cmd.Bool("yes")
  348. if !skipConfirm {
  349. confirm := false
  350. prompt := &survey.Confirm{
  351. Message: fmt.Sprintf("Are you sure you want to delete note %s?", id),
  352. Default: false,
  353. }
  354. if err := survey.AskOne(prompt, &confirm); err != nil {
  355. return err
  356. }
  357. if !confirm {
  358. fmt.Println("Deletion cancelled.")
  359. return nil
  360. }
  361. }
  362. mutation := `mutation DeleteNote($id: ID!) { deleteNote(id: $id) }`
  363. resp, err := c.Mutation(mutation, map[string]interface{}{"id": id})
  364. if err != nil {
  365. return err
  366. }
  367. var result struct {
  368. DeleteNote bool `json:"deleteNote"`
  369. }
  370. if err := json.Unmarshal(resp.Data, &result); err != nil {
  371. return err
  372. }
  373. if result.DeleteNote {
  374. fmt.Printf("Note %s deleted successfully.\n", id)
  375. } else {
  376. fmt.Printf("Failed to delete note %s.\n", id)
  377. }
  378. return nil
  379. }