From 35fbaa0969ab01e7b8262489eeda77e8c74517a3 Mon Sep 17 00:00:00 2001 From: Eiji Onchi Date: Thu, 27 Dec 2018 17:15:24 +0900 Subject: [PATCH] Added verboser. Clean up add.go. --- cmd/add.go | 117 +++++++++++++++++------------------------------- cmd/verboser.go | 48 ++++++++++++++++++++ 2 files changed, 89 insertions(+), 76 deletions(-) create mode 100644 cmd/verboser.go diff --git a/cmd/add.go b/cmd/add.go index b175400..42bbe23 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -53,99 +53,54 @@ TODO: Add a flag for manual/auto input of metadata `, Run: func(cmd *cobra.Command, args []string) { var entry *scholar.Entry - - search := strings.Join(args, " ") - file, _ := homedir.Expand(search) + var err error + var file string + doi := doiFlag + + input := strings.Join(args, " ") + file, err = homedir.Expand(input) + if err != nil { + panic(err) + } if _, err := os.Stat(file); os.IsNotExist(err) { - if search == "" { - doi := addDoi - if doi != "" { - fmt.Println("Getting metadata from doi") - entry = addDOI(doi) - } else { - if askYesNo("Would you like to search the web for metadata?") { - doi = query(requestSearch()) - } - if doi != "" { - entry = addDOI(doi) - } else { - fmt.Println() - fmt.Println("Adding the entry manually...") - fmt.Println("What kind of entry is it?") - t := selectType() - fmt.Println() - fmt.Println("Please, add the required fields:") - entry = add(t) - } - } - commit(entry) - edit(entry) - } else if doi := query(search); doi != "" { - entry = addDOI(doi) - commit(entry) - edit(entry) - } + file = "" } else { - fmt.Println("file:", file) - s := filepath.Base(file) - s = strings.TrimSuffix(s, filepath.Ext(s)) - doi := addDoi - if doi == "" { + input = "" + } + + if doi == "" { + if input == "" { if askYesNo("Would you like to search the web for metadata?") { doi = query(requestSearch()) - if doi == "" { - fmt.Println() - if askYesNo("I could not find anything, can you give me a better search variable?") { - doi = query(requestSearch()) - } - if doi != "" { - fmt.Println("Getting metadata from doi") - entry = addDOI(doi) - } else { - fmt.Println() - fmt.Println("Adding the entry manually...") - fmt.Println("What kind of entry is it?") - t := selectType() - fmt.Println() - fmt.Println("Please, add the required fields:") - entry = add(t) - } - - } else { - fmt.Println("Getting metadata from doi") - entry = addDOI(doi) - } - - } else { - fmt.Println() - fmt.Println("Adding the entry manually...") - fmt.Println("What kind of entry is it?") - t := selectType() - fmt.Println() - fmt.Println("Please, add the required fields:") - entry = add(t) } } else { - fmt.Println("Getting metadata from doi") - entry = addDOI(doi) + doi = query(input) } + } + if doi == "" { + entry = manual() + } else { + entry = addDOI(doi) + } - commit(entry) + commit(entry) + if file != "" { + info.println("attaching:", file) attach(entry, file) - edit(entry) } + edit(entry) - fmt.Println() - fmt.Println(entry.Bib()) + info.println() + info.println(entry.Bib()) }, } -var addDoi, addAttach string +var doiFlag, addAttach string func init() { rootCmd.AddCommand(addCmd) - addCmd.Flags().StringVarP(&addDoi, "doi", "d", "", "Specify the DOI to retrieve metadata") + addCmd.Flags().StringVarP(&doiFlag, "doi", "d", "", "Specify the DOI to retrieve metadata") addCmd.Flags().StringVarP(&addAttach, "attach", "a", "", "attach a file to the entry") } @@ -156,6 +111,9 @@ func askYesNo(question string) bool { } res, _ := prompt.Run() + if res == "" { + os.Exit(1) + } return strings.Contains("yesYes", res) } @@ -281,7 +239,6 @@ func query(search string) string { i, _, err := prompt.Run() if err != nil { - fmt.Println("Aborting") os.Exit(1) } @@ -402,3 +359,11 @@ func attach(entry *scholar.Entry, file string) { update(entry) } + +func manual() *scholar.Entry { + info.println("Adding the entry manually...") + info.println("Select the type of entry:") + t := selectType() + info.println("Required fields:") + return add(t) +} diff --git a/cmd/verboser.go b/cmd/verboser.go new file mode 100644 index 0000000..b287be8 --- /dev/null +++ b/cmd/verboser.go @@ -0,0 +1,48 @@ +// Copyright © 2018 Eiji Onchi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package cmd + +import ( + "fmt" + "io" + "os" +) + +type speaker struct { + level int + w io.Writer +} + +var info = &speaker{1, os.Stdout} + +func (s *speaker) print(a ...interface{}) (n int, err error) { + if s.level == 0 { + return 0, nil + } + return fmt.Fprint(s.w, a...) +} + +func (s *speaker) println(a ...interface{}) (n int, err error) { + if s.level == 0 { + return 0, nil + } + return fmt.Fprintln(s.w, a...) +}