github.com/monkeswag33/noter-go@v0.0.0-20220505233910-9d72ccb0bdb6/cmd/new/note.go (about) 1 /* 2 Copyright © 2022 NAME HERE <EMAIL ADDRESS> 3 4 */ 5 package new 6 7 import ( 8 "bufio" 9 "fmt" 10 "os" 11 "strings" 12 13 "github.com/manifoldco/promptui" 14 "github.com/monkeswag33/noter-go/db" 15 "github.com/monkeswag33/noter-go/errordef" 16 "github.com/monkeswag33/noter-go/prompt" 17 "github.com/sirupsen/logrus" 18 "github.com/spf13/cobra" 19 ) 20 21 // noteCmd represents the note command 22 var noteCmd = &cobra.Command{ 23 Use: "note <note name>", 24 Short: "A brief description of your command", 25 Long: `A longer description that spans multiple lines and likely contains examples 26 and usage of using your command. For example: 27 28 Cobra is a CLI library for Go that empowers applications. 29 This application is a tool to generate the needed files 30 to quickly create a Cobra application.`, 31 Run: func(cmd *cobra.Command, args []string) { 32 database = db.Database 33 username, _ := cmd.Flags().GetString("user") 34 body, _ := cmd.Flags().GetString("body") 35 note, err := createNote(args, username, body) 36 if err != nil { 37 logrus.Fatal(err) 38 } 39 if err := insertNote(note); err != nil { 40 logrus.Fatal(err) 41 } 42 fmt.Printf("Created note %q\n", note.Name) 43 }, 44 } 45 46 func createNote(args []string, username string, body string) (*db.Note, error) { 47 var name string 48 if len(args) == 1 { 49 logrus.Debug("Note name given as argument, using it") 50 name = args[0] 51 if err := newNoteValidateNoteName(name); err != nil { 52 return nil, err 53 } 54 } else { 55 name = prompt.Prompt(promptui.Prompt{ 56 Label: "Note name:", 57 }, newNoteValidateNoteName) 58 } 59 logrus.Debug("Note name passed validation") 60 if len(username) == 0 { 61 logrus.Debug("Username not given as parameter, prompting for it") 62 username = prompt.Prompt(promptui.Prompt{ 63 Label: "Username:", 64 }, newNoteValidateUsername) 65 } else if err := newNoteValidateUsername(username); err != nil { 66 return nil, err 67 } 68 logrus.Debug("Username passed validation") 69 if len(body) == 0 { 70 logrus.Debug("Body not given as parameter, prompting for it") 71 body = getBody() 72 logrus.Debug("Got body of note") 73 } else { 74 logrus.Debug("Body given as parameter, using it") 75 } 76 var note db.Note = db.Note{ 77 Name: name, 78 Body: body, 79 User: db.User{ 80 Username: username, 81 }, 82 } 83 logrus.Info("Created note") 84 return ¬e, nil 85 } 86 87 func insertNote(note *db.Note) error { 88 if err := database.CreateNote(note); err != nil { 89 return err 90 } 91 logrus.Info("Inserted note") 92 return nil 93 } 94 95 func init() { 96 NewCmd.AddCommand(noteCmd) 97 noteCmd.Flags().StringP("user", "u", "", "User that the note will be added to") 98 noteCmd.Flags().StringP("body", "b", "", "Body of note") 99 } 100 101 func getBody() string { 102 var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin) 103 var lines string 104 fmt.Println("Enter body (CTRL+] when finished): ") 105 for { 106 scanner.Scan() 107 var text string = scanner.Text() 108 if len(text) == 1 && text[0] == '\x1D' { 109 break 110 } 111 lines += text + "\n" 112 } 113 lines = strings.TrimSuffix(lines, "\n") 114 if scanner.Err() != nil { 115 fmt.Println(scanner.Err()) 116 } 117 return lines 118 } 119 120 func newNoteValidateNoteName(noteName string) error { 121 if len(noteName) < 5 { 122 return errordef.ErrNoteNameTooShort 123 } 124 exists, err := database.CheckNoteExists(db.Note{ 125 Name: noteName, 126 }) 127 if err != nil { 128 logrus.Fatal(err) 129 } 130 if exists { 131 return errordef.ErrNoteAlreadyExists 132 } 133 return nil 134 } 135 136 func newNoteValidateUsername(username string) error { 137 exists, err := database.CheckUserExists(db.User{ 138 Username: username, 139 }) 140 if err != nil { 141 logrus.Fatal(err) 142 } 143 if !exists { 144 return errordef.ErrUserDoesntExist 145 } 146 return nil 147 }