From 877a9f116ca167e5f2f4b7d1f88f7ac2795e52bf Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Tue, 30 Jan 2018 11:14:34 +0700 Subject: [PATCH] Add flag to only print the index of bookmark --- cmd/print.go | 11 +++++++++++ cmd/search.go | 6 +++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/print.go b/cmd/print.go index 0ec2549..e39ff1a 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -19,6 +19,7 @@ var ( Run: func(cmd *cobra.Command, args []string) { // Read flags useJSON, _ := cmd.Flags().GetBool("json") + indexOnly, _ := cmd.Flags().GetBool("index-only") // Read bookmarks from database bookmarks, err := DB.GetBookmarks(args...) @@ -45,6 +46,8 @@ var ( os.Exit(1) } fmt.Println(string(bt)) + } else if indexOnly { + printBookmarkIndex(bookmarks...) } else { printBookmark(bookmarks...) } @@ -54,9 +57,17 @@ var ( func init() { printCmd.Flags().BoolP("json", "j", false, "Output data in JSON format") + printCmd.Flags().BoolP("index-only", "i", false, "Only print the index of bookmarks") rootCmd.AddCommand(printCmd) } +func printBookmarkIndex(bookmarks ...model.Bookmark) { + for _, bookmark := range bookmarks { + fmt.Printf("%d ", bookmark.ID) + } + fmt.Println() +} + func printBookmark(bookmarks ...model.Bookmark) { for _, bookmark := range bookmarks { // Create bookmark index diff --git a/cmd/search.go b/cmd/search.go index ae846a5..dba29bb 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -19,8 +19,9 @@ var ( Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { // Read flags - useJSON, _ := cmd.Flags().GetBool("json") tags, _ := cmd.Flags().GetStringSlice("tags") + useJSON, _ := cmd.Flags().GetBool("json") + indexOnly, _ := cmd.Flags().GetBool("index-only") // Fetch keyword keyword := "" @@ -48,6 +49,8 @@ var ( os.Exit(1) } fmt.Println(string(bt)) + } else if indexOnly { + printBookmarkIndex(bookmarks...) } else { printBookmark(bookmarks...) } @@ -57,6 +60,7 @@ var ( func init() { searchCmd.Flags().BoolP("json", "j", false, "Output data in JSON format") + searchCmd.Flags().BoolP("index-only", "i", false, "Only print the index of bookmarks") searchCmd.Flags().StringSliceP("tags", "t", []string{}, "Search bookmarks with specified tag(s)") rootCmd.AddCommand(searchCmd) }