Skip to content

Commit

Permalink
Added verboser. Clean up add.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Dec 27, 2018
1 parent 8bbc22f commit 35fbaa0
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 76 deletions.
117 changes: 41 additions & 76 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -156,6 +111,9 @@ func askYesNo(question string) bool {
}

res, _ := prompt.Run()
if res == "" {
os.Exit(1)
}

return strings.Contains("yesYes", res)
}
Expand Down Expand Up @@ -281,7 +239,6 @@ func query(search string) string {
i, _, err := prompt.Run()

if err != nil {
fmt.Println("Aborting")
os.Exit(1)
}

Expand Down Expand Up @@ -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)
}
48 changes: 48 additions & 0 deletions cmd/verboser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright © 2018 Eiji Onchi <eiji@onchi.me>
//
// 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...)
}

0 comments on commit 35fbaa0

Please sign in to comment.