diff options
author | Matthew Lemon <y@yulqen.org> | 2023-08-14 04:46:48 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2023-08-14 04:46:48 +0100 |
commit | a26430129851958ef8bb79e5e30d4887445685fd (patch) | |
tree | 21b5d76fd00a573277c578d1e01cf8b6d4e560f0 | |
parent | 7bdedbffc726bed5510c2bb3d2240bacf20a1fce (diff) |
Simplifies the interface - adds comments
I never used the file selection stuff with this tool so I removed the
old argument processing and replaced it with a single option to add a
comment, which is what I do actually need. Uses the flags library from
go. Very simple.
-rw-r--r-- | quicknote.go | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/quicknote.go b/quicknote.go index 8629a49..456a589 100644 --- a/quicknote.go +++ b/quicknote.go @@ -2,8 +2,9 @@ package main import ( "bufio" + "flag" "fmt" - "io/ioutil" + "io" "net/http" "os" "os/user" @@ -14,36 +15,36 @@ import ( "github.com/atotto/clipboard" ) -func main() { - if len(os.Args) < 2 { - fmt.Println("Usage: quicknote [-c] <url> [markdown-file]") - os.Exit(1) - } +var commentVar string +var mainContent string - url := "" - markdownFile := "" +func init() { + const ( + defaultComment = "" + usage = "add a comment to the quicknote" + ) + flag.StringVar(&commentVar, "c", defaultComment, usage+" (shorthand)") + flag.StringVar(&commentVar, "comment", defaultComment, usage) + flag.Parse() + mainContent = flag.Arg(0) +} - if os.Args[1] == "-c" { - urlFromClipboard, err := clipboard.ReadAll() - if err != nil { - fmt.Printf("Error getting URL from clipboard: %v\n", err) - os.Exit(1) - } - url = strings.TrimSpace(urlFromClipboard) - } else { - url = os.Args[1] +func main() { + var url, markdownFile string + + urlFromClipboard, err := clipboard.ReadAll() + if err != nil { + fmt.Printf("Error getting URL from clipboard: %v\n", err) + os.Exit(1) } + url = strings.TrimSpace(urlFromClipboard) - if len(os.Args) == 2 || (len(os.Args) == 3 && os.Args[1] == "-c") { - usr, err := user.Current() - if err != nil { - fmt.Printf("Error getting user home directory: %v\n", err) - os.Exit(1) - } - markdownFile = filepath.Join(usr.HomeDir, "Documents", "Notes", "quicknote.md") - } else { - markdownFile = os.Args[2] + usr, err := user.Current() + if err != nil { + fmt.Printf("Error getting user home directory: %v\n", err) + os.Exit(1) } + markdownFile = filepath.Join(usr.HomeDir, "Documents", "Notes", "quicknote.md") resp, err := http.Get(url) if err != nil { @@ -52,7 +53,7 @@ func main() { } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { fmt.Printf("Error reading response body: %v\n", err) os.Exit(1) @@ -67,6 +68,9 @@ func main() { } markdownLink := fmt.Sprintf("- [%s](%s)\n", title, url) + if commentVar != "" { + markdownLink = fmt.Sprintf("- %s: [%s](%s)\n", strings.TrimSpace(commentVar), title, url) + } err = appendToFile(markdownFile, markdownLink) if err != nil { fmt.Printf("Error appending to file: %v\n", err) @@ -94,6 +98,7 @@ func promptForTitle() string { return strings.TrimSpace(title) } +// appendToFile writes content to the file filename. func appendToFile(filename, content string) error { f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { |