示例#1
0
// ListCommand lists Notes
func ListCommand(c *cli.Context, i storage.Impl) (rnotes []string) {
	nName := strings.Join(c.Args(), " ")
	notes := i.ListNotes(nName)

	return FormatNoteList(notes)

}
示例#2
0
// AddCommand adds a Note
func AddCommand(c *cli.Context, i storage.Impl) (n storage.Note, err error) {
	nName, err := NoteName(c)
	if err != nil {
		return n, err
	}

	if exists := i.NoteExists(nName); exists == true {
		return n, fmt.Errorf("Note already exists")
	}

	n.Name = nName
	n.Temporary = c.Bool("t")

	// Only open editor if -p (read from clipboard) isnt set
	if c.IsSet("p") {
		nText, err := clipboard.ReadAll()
		if err != nil {
			return n, err
		}
		n.Text = nText
	} else {
		if err := writer.WriteNote(&n); err != nil {
			return n, err
		}
	}

	if err := i.SaveNote(&n); err != nil {
		return n, err
	}

	return n, nil
}
示例#3
0
// SearchCommand searches for Notes
func SearchCommand(c *cli.Context, i storage.Impl) (rnotes []string, err error) {
	nName, err := NoteName(c)
	if err != nil {
		return rnotes, err
	}

	notes := i.SearchNotes(nName)

	return FormatNoteList(notes), nil
}
示例#4
0
// InfoCommand shows information about a Note
func InfoCommand(c *cli.Context, i storage.Impl) (n storage.Note, err error) {
	nName, err := NoteName(c)
	if err != nil {
		return storage.Note{}, err
	}

	n, err = i.LoadNote(nName)
	if err != nil {
		return storage.Note{}, err
	}

	return n, nil
}
示例#5
0
// DeleteCommand deletes a Note
func DeleteCommand(c *cli.Context, i storage.Impl) (n storage.Note, err error) {
	nName, err := NoteName(c)
	if err != nil {
		return n, err
	}

	n, err = i.LoadNote(nName)
	if err != nil {
		return n, err
	}

	if i.DeleteNote(n) != nil {
		return n, err
	}

	return n, nil
}
示例#6
0
// ReadCommand reads a Note
func ReadCommand(c *cli.Context, i storage.Impl) (err error) {
	nName, err := NoteName(c)
	if err != nil {
		return err
	}

	n, err := i.LoadNote(nName)
	if err != nil {
		return err
	}

	if err := reader.ReadNote(n); err != nil {
		return err
	}

	return nil
}
示例#7
0
// KeepCommand keeps/unkeeps a Note
func KeepCommand(c *cli.Context, i storage.Impl, k bool) (n storage.Note, err error) {
	nName, err := NoteName(c)
	if err != nil {
		return n, err
	}

	n, err = i.LoadNote(nName)
	if err != nil {
		return n, err
	}

	n.Temporary = !k
	if err := i.SaveNote(&n); err != nil {
		return n, err
	}

	return n, err
}
示例#8
0
// ShareCommand shares a Note
func ShareCommand(c *cli.Context, i storage.Impl) (n storage.Note, url string, err error) {
	nName, err := NoteName(c)
	if err != nil {
		return n, url, err
	}

	n, err = i.LoadNote(nName)
	if err != nil {
		return n, url, err
	}

	url, err = reader.ShareNote(n, "http://hastebin.com/documents")

	if err != nil {
		return n, url, err
	}

	return n, url, nil
}
示例#9
0
// RenameCommand renames a Note
func RenameCommand(c *cli.Context, i storage.Impl) (nName string, newName string, err error) {
	nName, err = NoteName(c)
	if err != nil {
		return nName, newName, err
	}

	n, err := i.LoadNote(nName)
	if err != nil {
		return nName, newName, err
	}

	reader := bufio.NewReader(os.Stdin)
	fmt.Print(fmt.Sprintf("Rename note '%s' to: ", n.Name))
	newName, err = reader.ReadString('\n')
	if err != nil {
		return nName, newName, err
	}

	newName = strings.Trim(newName, "\n")

	if newName == "" {
		return nName, newName, errors.New("Note name can't be blank")
	}

	if i.NoteExists(newName) == true {
		return nName, newName, fmt.Errorf("Note '%s' already exists", newName)
	}

	if err = i.RenameNote(n.ID, newName); err != nil {
		return nName, newName, fmt.Errorf("Failed to rename note '%s' to '%s'.", nName, newName)
	}

	return nName, newName, nil
}
示例#10
0
// EditCommand edits a Note
func EditCommand(c *cli.Context, i storage.Impl) (n storage.Note, err error) {
	nName, err := NoteName(c)
	if err != nil {
		return n, err
	}

	n, err = i.LoadNote(nName)
	if err != nil {
		return n, err
	}

	if err := writer.WriteNote(&n); err != nil {
		return n, err
	}

	if err := i.SaveNote(&n); err != nil {
		return n, err
	}

	return n, nil

}
示例#11
0
func main() {
	// Init
	i := storage.Impl{}
	if err := i.InitDB(); err != nil {
		panic(err)
	}

	i.InitSchema()
	if err := i.RemoveExpiredNotes(); err != nil {
		panic(err)
	}

	app := cli.NewApp()
	app.Name = "forgetful"
	app.Usage = "For your notes/cheat sheets"
	app.Version = version
	app.Commands = []cli.Command{
		{
			Name:    "add",
			Aliases: []string{"a"},
			Usage:   "Add a note",
			Flags: []cli.Flag{
				cli.BoolFlag{
					Name:  "t",
					Usage: "Mark as temporary (expires after 24 hours)",
				},
				cli.BoolFlag{
					Name:  "p",
					Usage: "Create note with contents from clipboard",
				},
			},
			Action: func(c *cli.Context) error {
				n, err := commands.AddCommand(c, i)
				if err != nil {
					return err
				}

				fmt.Println(fmt.Sprintf("Added note: %s", n.Name))
				return nil
			},
		},
		{
			Name:    "delete",
			Aliases: []string{"d"},
			Usage:   "Delete a note",
			Action: func(c *cli.Context) error {
				n, err := commands.DeleteCommand(c, i)
				if err != nil {
					return err
				}

				fmt.Println(fmt.Sprintf("Deleted note: %s", n.Name))
				return nil
			},
		},
		{
			Name:    "edit",
			Aliases: []string{"e"},
			Usage:   "Edit/read a note",
			Action: func(c *cli.Context) error {
				n, err := commands.EditCommand(c, i)
				if err != nil {
					return err
				}

				fmt.Println(fmt.Sprintf("Updated note: %s", n.Name))
				return nil
			},
		},
		{
			Name:    "info",
			Aliases: []string{"i"},
			Usage:   "Prints information about a note",
			Action: func(c *cli.Context) error {
				n, err := commands.InfoCommand(c, i)
				if err != nil {
					return err
				}

				printInfo(n)

				return nil
			},
		},
		{
			Name:    "read",
			Aliases: []string{"r"},
			Usage:   "Read a note",
			Action: func(c *cli.Context) error {
				if err := commands.ReadCommand(c, i); err != nil {
					return err
				}
				return nil
			},
		},
		{
			Name:    "rename",
			Aliases: []string{"mv"},
			Usage:   "Rename a note",
			Action: func(c *cli.Context) error {
				nName, newName, err := commands.RenameCommand(c, i)
				if err != nil {
					return err
				}

				fmt.Println(fmt.Sprintf("Renamed %s to %s", nName, newName))

				return nil
			},
		},
		{
			Name:    "list",
			Aliases: []string{"l"},
			Usage:   "List all notes, filter result if argument i present",
			Action: func(c *cli.Context) error {
				notes := commands.ListCommand(c, i)

				printList(notes)

				return nil
			},
		},
		{
			Name:    "search",
			Aliases: []string{"s"},
			Usage:   "Search notes for argument",
			Action: func(c *cli.Context) error {
				notes, err := commands.SearchCommand(c, i)
				if err != nil {
					return err
				}

				printList(notes)

				return nil
			},
		},
		{
			Name:  "share",
			Usage: "Share a note (publicly) on hastebin.com",
			Action: func(c *cli.Context) error {
				n, url, err := commands.ShareCommand(c, i)
				if err != nil {
					return err
				}

				fmt.Println(fmt.Sprintf("Shared note '%s': %s", n.Name, url))
				return nil
			},
		},
		{
			Name:    "keep",
			Aliases: []string{"k"},
			Usage:   "Sets a temporary note as permanent",
			Action: func(c *cli.Context) error {
				n, err := commands.KeepCommand(c, i, true)
				if err != nil {
					return err
				}

				fmt.Println(fmt.Sprintf("Keeping note: %s", n.Name))
				return nil
			},
		},
		{
			Name:    "unkeep",
			Aliases: []string{"u"},
			Usage:   "Sets a permanent note as temporary",
			Action: func(c *cli.Context) error {
				n, err := commands.KeepCommand(c, i, false)
				if err != nil {
					return err
				}

				fmt.Println(fmt.Sprintf("Unkeeping note: %s", n.Name))
				return nil
			},
		},
	}

	app.Run(os.Args)

}