Skip to content

Commit

Permalink
Improved import
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Nov 1, 2018
1 parent 3ba7fe2 commit 42c17b6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
7 changes: 6 additions & 1 deletion scholar.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ func TypesInfo(level int) {
// NewEntry returns an empty copy of an entry according to the types of entries
// loaded.
func NewEntry(label string) *Entry {
return EntryTypes[label].get()
if entryType, ok := EntryTypes[label]; ok {
// Return the entry type only if it exists
return entryType.get()
}
// Otherwise, default to misc type
return EntryTypes["misc"].get()
}

// Parse parses the information for a work to an entry.
Expand Down
5 changes: 4 additions & 1 deletion scholar/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func init() {
rootCmd.AddCommand(addCmd)

addCmd.Flags().StringVarP(&addDoi, "doi", "d", "", "Specify the DOI to retrieve metadata")
addCmd.Flags().StringVar(&curentLibrary, "to", "", "specify library to use")
addCmd.Flags().StringVar(&currentLibrary, "to", "", "Specify which library to add")
addCmd.Flags().StringVarP(&addAttach, "attach", "a", "", "attach a file to the entry")

// Here you will define your flags and configuration settings.
Expand Down Expand Up @@ -164,6 +164,9 @@ func requestSearch() string {
func commit(entry *scholar.Entry) {
key := entry.GetKey()
saveTo := filepath.Join(viper.GetString("deflib"), key)
if currentLibrary != "" {
saveTo := filepath.Join(viper.GetSub("LIBRARIES").GetString(currentLibrary), key)
}

if _, err := os.Stat(saveTo); !os.IsNotExist(err) {
//TODO: make a better algorithm for unique keys
Expand Down
26 changes: 18 additions & 8 deletions scholar/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@
package cmd

import (
"fmt"
"os"

"github.com/cgxeiji/bib"
"github.com/cgxeiji/scholar"
"github.com/kr/pretty"
"github.com/spf13/cobra"
)

// importCmd represents the import command
var importCmd = &cobra.Command{
Use: "import",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Short: "Imports a bibtex/biblatex file",
Long: `Scholar: a CLI Reference Manager
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Import a bibtex/biblatex file into a library in Scholar.
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
importParse(args[0])
Expand All @@ -49,6 +48,7 @@ to quickly create a Cobra application.`,
func init() {
rootCmd.AddCommand(importCmd)

importCmd.Flags().StringVarP(&currentLibrary, "to", "t", "", "Specify which library to import to")
// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand Down Expand Up @@ -80,6 +80,11 @@ func importParse(filename string) {
e.Key = entry["key"]
delete(entry, "key")

if file, ok := entry["file"]; ok {
e.File = file
delete(entry, "file")
}

for req := range e.Required {
e.Required[req] = entry[req]
delete(entry, req)
Expand All @@ -93,5 +98,10 @@ func importParse(filename string) {
es = append(es, e)
}

pretty.Println(es)
// Make sure all entries are correctly parsed before commiting
for _, e := range es {
commit(e)
}

fmt.Println("Import from", filename, "successful!")
}
2 changes: 1 addition & 1 deletion scholar/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/spf13/viper"
)

var confFile, typesFile, curentLibrary string
var confFile, typesFile, currentLibrary string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down

0 comments on commit 42c17b6

Please sign in to comment.