Documentation ¶
Overview ¶
Package notes is a library which consists notes command.
https://github.com/rhysd/notes-cli/tree/master/cmd/notes
This library is for using notes command programmatically from Go program. It consists structs which represent each subcommands.
1. Create Config instance with NewConfig 2. Create an instance of subcommand you want to run with config 3. Run it with .Do() method. It will return an error if some error occurs
import ( "bytes" "fmt" "github.com/rhysd/notes-cli" "os" "strings" ) var buf bytes.Buffer // Create user configuration cfg, err := notes.NewConfig() if err != nil { panic(err) } // Prepare `notes list` command cmd := ¬es.ListCmd{ Config: cfg, Relative: true, Out: &buf } // Runs the command if err := cmd.Do(); err != nil { fmt.Fprintln(os.Stdout, err) } paths := strings.Split(strings.Trim(buf.String(), "\n"), "\n") fmt.Println("Note paths:", paths)
For usage of `notes` command, please read README of the repository.
https://github.com/rhysd/notes-cli/blob/master/README.md
Example ¶
color.NoColor = true cwd, err := os.Getwd() if err != nil { panic(err) } cfg := ¬es.Config{ HomePath: filepath.Join(cwd, "example", "notes-cli"), } cmd := notes.ListCmd{ Config: cfg, Oneline: true, Out: os.Stdout, } // Shows oneline notes (relative file path, category, tags, title) if err := cmd.Do(); err != nil { panic(err) }
Output: blog/daily/dialy-2018-11-20.md dialy-2018-11-20 blog/daily/dialy-2018-11-18.md notes dialy-2018-11-18 memo/tasks.md My tasks memo/notes-urls.md notes URLs for notes blog/tech/introduction-to-notes-command.md notes introduction-to-notes-command blog/tech/how-to-handle-files.md golang,file How to hanle files in Go
Index ¶
- Variables
- type Categories
- type CategoriesCmd
- type Category
- type CategoryCollectMode
- type Cmd
- type Config
- type ConfigCmd
- type ExternalCmd
- type Git
- func (git *Git) AddAll() error
- func (git *Git) Command(subcmd string, args ...string) *exec.Cmd
- func (git *Git) Commit(msg string) error
- func (git *Git) Exec(subcmd string, args ...string) (string, error)
- func (git *Git) Init() error
- func (git *Git) Push(remote, branch string) error
- func (git *Git) TrackingRemote() (string, string, error)
- type ListCmd
- type MismatchCategoryError
- type NewCmd
- type Note
- type PagerWriter
- type SaveCmd
- type SelfupdateCmd
- type TagsCmd
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Version = "1.6.2"
Version is version string of notes command. It conforms semantic versioning
Functions ¶
This section is empty.
Types ¶
type Categories ¶
Categories is a map from category name to Category instance
func CollectCategories ¶
func CollectCategories(cfg *Config, mode CategoryCollectMode) (Categories, error)
CollectCategories collects all categories under home by default. The behavior of collecting categories can be customized with mode parameter. Default mode value is 0 (nothing specified).
func (Categories) Names ¶
func (cats Categories) Names() []string
Names returns all category names as slice
type CategoriesCmd ¶
type CategoriesCmd struct { Config *Config // Out is a writer to write output of this command. Kind of stdout is expected Out io.Writer // contains filtered or unexported fields }
CategoriesCmd represents `notes categories` command. Each public fields represent options of the command. Out field represents where this command should output.
func (*CategoriesCmd) Do ¶
func (cmd *CategoriesCmd) Do() error
Do runs `notes categories` command and returns an error if occurs
type Category ¶
type Category struct { // Path is a path to the category directory Path string // Name is a name of category Name string // NotePaths are paths to notes of the category NotePaths []string }
Category represents a category directory which contains some notes
type CategoryCollectMode ¶
type CategoryCollectMode uint
CategoryCollectMode customizes the behavior of how to collect categories
const ( // OnlyFirstCategory is a flag to stop collecting categories earlier. If this flag is included // in mode parameter of CollectCategories(), it collects only first category and only first // note and stops finding anymore. OnlyFirstCategory CategoryCollectMode = 1 << iota )
type Config ¶
type Config struct { // HomePath is a file path to directory of home of notes command. If $NOTES_CLI_HOME is set, it is used. // Otherwise, notes-cli directory in XDG data directory is used. This directory is automatically created // when config is created HomePath string // GitPath is a file path to `git` executable. If $NOTES_CLI_GIT is set, it is used. // Otherwise, `git` is used by default. This is optional and can be empty. When empty, some command // and functionality which require Git don't work GitPath string // EditorCmd is a command of your favorite editor. If $NOTES_CLI_EDITOR is set, it is used. This value is // similar to $EDITOR environment variable and can contain command arguments like "vim -g". Otherwise, // this value will be empty. When empty, some functionality which requires an editor to open note doesn't // work EditorCmd string // PagerCmd is a command for paging output from 'list' subcommand. If $NOTES_CLI_PAGER is set, it is used. PagerCmd string }
Config represents user configuration of notes command
type ConfigCmd ¶
type ConfigCmd struct { Config *Config // Name is a name of configuration. Must be one of "", "home", "git" or "editor" Name string // Out is a writer to write output of this command. Kind of stdout is expected Out io.Writer // contains filtered or unexported fields }
ConfigCmd represents `notes config` command. Each public fields represent options of the command. Out field represents where this command should output.
type ExternalCmd ¶
type ExternalCmd struct { // ExePath is a path to executable of the external subcommand ExePath string // Args is arguments passed to external subcommand. Arguments specified to `notes` are forwarded Args []string // NotesPath is an executable path of the `notes` command. This is passed to the first argument of external subcommand NotesPath string }
ExternalCmd represents user-defined subcommand
func NewExternalCmd ¶
func NewExternalCmd(fromErr error, args []string) (*ExternalCmd, bool)
NewExternalCmd creates ExternalCmd instance from given error and arguments. The error must be parse error of kingpin.Parse(). When the missing subcommand is not detected in the error message, this function returns false as 2nd return value
func (*ExternalCmd) Do ¶
func (cmd *ExternalCmd) Do() error
Do invokes external subcommand with exec. If it did not exit successfully this function returns an error
type Git ¶
type Git struct {
// contains filtered or unexported fields
}
Git represents Git command for specific repository
func NewGit ¶
NewGit creates Git instance from Config value. Home directory is assumed to be a root of Git repository
func (*Git) Command ¶
Command returns exec.Command instance which runs given Git subcommand with given arguments
type ListCmd ¶
type ListCmd struct { Config *Config // Full is a flag equivalent to --full Full bool // Category is a regex string equivalent to --cateogry Category string // Tag is a regex string equivalent to --tag Tag string // Relative is a flag equivalent to --relative Relative bool // Oneline is a flag equivalent to --oneline Oneline bool // Tag is a string indicating how to sort the list. This value is equivalent to --sort option SortBy string // Edit is a flag equivalent to --edit Edit bool // Out is a writer to write output of this command. Kind of stdout is expected Out io.Writer // contains filtered or unexported fields }
ListCmd represents `notes list` command. Each public fields represent options of the command Out field represents where this command should output.
type MismatchCategoryError ¶
type MismatchCategoryError struct {
// contains filtered or unexported fields
}
MismatchCategoryError represents an error caused when a user specifies mismatched category
func (*MismatchCategoryError) Error ¶
func (e *MismatchCategoryError) Error() string
func (*MismatchCategoryError) Is ¶
func (e *MismatchCategoryError) Is(target error) bool
Is returns if given error is a MismatchCategoryError or not
type NewCmd ¶
type NewCmd struct { Config *Config // Category is a category name of the new note. This must be a name allowed for directory name Category string // Filename is a file name of the new note Filename string // Tags is a comma-separated string of tags of the new note Tags string // NoInline is a flag equivalent to --no-inline-input NoInline bool // NoEdit is a flag equivalent to --no-edit NoEdit bool // contains filtered or unexported fields }
NewCmd represents `notes new` command. Each public fields represent options of the command
type Note ¶
type Note struct { // Config is a configuration of notes command which was created by NewConfig() Config *Config // Category is a category string. It must not be empty Category string // Tags is tags of note. It can be empty and cannot contain comma Tags []string // Created is a datetime when note was created Created time.Time // File is a file name of the note File string // Title is a title string of the note. When the note is not created yet, it may be empty Title string }
Note represents a note stored on filesystem or will be created
func LoadNote ¶
LoadNote reads note file from given path, parses it and creates Note instance. When given file path does not exist or when the file does note contain mandatory metadata ('Category', 'Tags' and 'Created'), this function returns an error
func NewNote ¶
NewNote creates a new note instance with given parameters and configuration. Category and file name cannot be empty. If given file name lacks file extension, it automatically adds ".md" to file name.
func (*Note) Create ¶
Create creates a file of the note. When title is empty, file name omitting file extension is used for it. This function will fail when the file is already existing.
func (*Note) Open ¶
Open opens the note using an editor command user set. When user did not set any editor command with $NOTES_CLI_EDITOR, this method fails. Otherwise, an editor process is spawned with argument of path to the note file
func (*Note) ReadBodyLines ¶
ReadBodyLines reads body of note until maxLines lines and returns it as string and number of lines as int
func (*Note) RelFilePath ¶
RelFilePath returns the relative file path of the note from home directory
func (*Note) TemplatePath ¶
TemplatePath resolves a path to template file of the note. If no template is found, it returns false as second return value
type PagerWriter ¶
type PagerWriter struct { // Cmdline is a string of command line which was spawned Cmdline string // Err is an error instance which occurred while paging. Err error // contains filtered or unexported fields }
PagerWriter is a wrapper of pager command to paging output to writer with pager command such as `less` or `more`. Starting process, writing input to process, waiting process finishes may cause an error. If one of them causes an error, later operations won't be performed. Instead, the prior error is returned.
func StartPagerWriter ¶
func StartPagerWriter(pagerCmd string, stdout io.Writer) (*PagerWriter, error)
StartPagerWriter creates a new PagerWriter instance and spawns underlying pager process with given command. pagerCmd can contain options like "less -R". When the command cannot be parsed as shell arguments or starting underlying pager process fails, this function returns an error
func (*PagerWriter) Wait ¶
func (pager *PagerWriter) Wait() error
Wait waits until underlying pager process finishes
type SaveCmd ¶
type SaveCmd struct { Config *Config // Message is a message of Git commit which will be created to save notes. If this value is empty, // automatically generated message will be used. Message string // contains filtered or unexported fields }
SaveCmd represents `notes save` command. Each public fields represent options of the command
type SelfupdateCmd ¶
type SelfupdateCmd struct { // Dry is a flag equivalent to --dry Dry bool // Slug is a "user/repo" string where releases are put. This field is useful when you forked // notes-cli into your own repository. Slug string // Out is a writer to write output of this command. Kind of stdout is expected Out io.Writer // contains filtered or unexported fields }
SelfupdateCmd represents `notes selfupdate` subcommand.
func (*SelfupdateCmd) Do ¶
func (cmd *SelfupdateCmd) Do() error
Do method checks the newer version binary. If new version is available, it updates myself with the latest binary.
type TagsCmd ¶
type TagsCmd struct { Config *Config // Category is a category name of tags. If this value is empty, tags of all categories will be output Category string // Out is a writer to write output of this command. Kind of stdout is expected Out io.Writer // contains filtered or unexported fields }
TagsCmd represents `notes tags` command. Each public fields represent options of the command Out field represents where this command should output.