Skip to content

Commit

Permalink
Moved library selection to root and added templates
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Nov 1, 2018
1 parent 3de70a3 commit a88b1ac
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 41 deletions.
1 change: 0 additions & 1 deletion scholar/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func init() {
rootCmd.AddCommand(addCmd)

addCmd.Flags().StringVarP(&addDoi, "doi", "d", "", "Specify the DOI to retrieve metadata")
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
2 changes: 1 addition & 1 deletion scholar/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func configure() {

path, _ := homedir.Dir()
path = filepath.Join(path, ".config", "scholar", "config.yaml")
if err := ioutil.WriteFile(path, configDefault, 0644); err != nil {
if err := ioutil.WriteFile(path, configTemplate, 0644); err != nil {
panic(nil)
}

Expand Down
4 changes: 1 addition & 3 deletions scholar/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ TODO: add flag to specify which library to export
func init() {
rootCmd.AddCommand(exportCmd)

exportCmd.Flags().StringVarP(&currentLibrary, "from", "f", "", "Specify which library to export")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
Expand All @@ -70,7 +68,7 @@ func init() {
}

func export() {
path := viper.GetString("deflib")
path := libraryPath()
if currentLibrary != "" {
path = viper.Sub("LIBRARIES").GetString(currentLibrary)
}
Expand Down
25 changes: 21 additions & 4 deletions scholar/cmd/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func edit(entry *scholar.Entry) {
key := entry.GetKey()
saveTo := filepath.Join(viper.GetString("deflib"), key)
saveTo := filepath.Join(libraryPath(), key)

file := filepath.Join(saveTo, "entry.yaml")

Expand All @@ -37,7 +37,7 @@ func edit(entry *scholar.Entry) {

func update(entry *scholar.Entry) {
key := entry.GetKey()
saveTo := filepath.Join(viper.GetString("deflib"), key)
saveTo := filepath.Join(libraryPath(), key)

file := filepath.Join(saveTo, "entry.yaml")

Expand Down Expand Up @@ -97,8 +97,25 @@ func clean(filename string) string {
return strings.ToLower(filename)
}

func libraryPath() string {
if currentLibrary != "" {
if !viper.Sub("LIBRARIES").IsSet(currentLibrary) {
fmt.Println("No library called", currentLibrary, "was found!")
fmt.Println("Available libraries:")
for k, v := range viper.GetStringMapString("LIBRARIES") {
fmt.Println(" ", k)
fmt.Println(" ", v)
}
os.Exit(1)
}

return viper.Sub("LIBRARIES").GetString(currentLibrary)
}
return viper.Sub("LIBRARIES").GetString(viper.GetString("GENERAL.default"))
}

func entryQuery(search string) *scholar.Entry {
dirs, err := ioutil.ReadDir(viper.GetString("deflib"))
dirs, err := ioutil.ReadDir(libraryPath())
if err != nil {
panic(err)
}
Expand All @@ -107,7 +124,7 @@ func entryQuery(search string) *scholar.Entry {

for _, dir := range dirs {
if dir.IsDir() {
d, err := ioutil.ReadFile(filepath.Join(viper.GetString("deflib"), dir.Name(), "entry.yaml"))
d, err := ioutil.ReadFile(filepath.Join(libraryPath(), dir.Name(), "entry.yaml"))
if err != nil {
panic(err)
}
Expand Down
1 change: 0 additions & 1 deletion scholar/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ Import a bibtex/biblatex file into a library in Scholar.
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
5 changes: 2 additions & 3 deletions scholar/cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/cgxeiji/scholar"
"github.com/spf13/cobra"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -67,14 +66,14 @@ func init() {
}

func entryFromKey(key string) *scholar.Entry {
dirs, err := ioutil.ReadDir(viper.GetString("deflib"))
dirs, err := ioutil.ReadDir(libraryPath())
if err != nil {
panic(err)
}

for _, dir := range dirs {
if dir.IsDir() && dir.Name() == strings.TrimSpace(key) {
d, err := ioutil.ReadFile(filepath.Join(viper.GetString("deflib"), dir.Name(), "entry.yaml"))
d, err := ioutil.ReadFile(filepath.Join(libraryPath(), dir.Name(), "entry.yaml"))
if err != nil {
panic(err)
}
Expand Down
68 changes: 40 additions & 28 deletions scholar/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/cgxeiji/scholar"
homedir "github.com/mitchellh/go-homedir"
Expand Down Expand Up @@ -66,28 +67,11 @@ func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&confFile, "config", "", "config file (default $HOME/.config/scholar/config.yaml)")
rootCmd.PersistentFlags().StringVar(&typesFile, "types", "", "entry types file (default $HOME/.config/scholar/types.yaml)")
//rootCmd.PersistentFlags().StringVar(&confFile, "config", "", "config file (default $HOME/.config/scholar/config.yaml)")
//rootCmd.PersistentFlags().StringVar(&typesFile, "types", "", "entry types file (default $HOME/.config/scholar/types.yaml)")
rootCmd.PersistentFlags().StringVarP(&currentLibrary, "library", "l", "", "specify the library")
}

var configDefault = []byte(`
# General Settings
GENERAL:
# Set the default library.
# Scholar will retrieve and save entries from this library.
default: scholar
# Set the default text editor
editor: vi
# Set the email for polite use of CrossRef
mailto: mail@example.com
# Path locations for the libraries.
# You can add as many libraries as you want.
# You can name the library however you want.
LIBRARIES:
scholar: ~/ScholarLibrary
`)

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if confFile != "" && confFile != "which" {
Expand All @@ -102,11 +86,25 @@ func initConfig() {

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
// Load the configuration file. If not found, auto-generate one.
if err := viper.ReadInConfig(); err != nil {
fmt.Println(err)
fmt.Println("Using default values")
viper.ReadConfig(bytes.NewBuffer(configDefault))
fmt.Println("Setting up a new configuration file")

path, _ := homedir.Dir()
path = filepath.Join(path, ".config", "scholar")
if err := os.MkdirAll(path, os.ModePerm); err != nil {
panic(err)
}

path = filepath.Join(path, "config.yaml")
if err := ioutil.WriteFile(path, configTemplate, 0644); err != nil {
panic(err)
}

if err := viper.ReadInConfig(); err != nil {
panic(err)
}
}

if confFile == "which" {
Expand Down Expand Up @@ -142,11 +140,25 @@ func initConfig() {
et.AddConfigPath("$HOME/.config/scholar")
}

err = et.ReadInConfig()
if err != nil {
// Load the configuration file. If not found, auto-generate one.
if err := et.ReadInConfig(); err != nil {
fmt.Println(err)
fmt.Println("Please, set an entry types file at any of those locations.")
panic("no types.yaml found")
fmt.Println("Setting up a new types file")

path, _ := homedir.Dir()
path = filepath.Join(path, ".config", "scholar")
if err := os.MkdirAll(path, os.ModePerm); err != nil {
panic(err)
}

path = filepath.Join(path, "types.yaml")
if err := ioutil.WriteFile(path, typesTemplate, 0644); err != nil {
panic(err)
}

if err := et.ReadInConfig(); err != nil {
panic(err)
}
}

if typesFile == "which" {
Expand Down
Loading

0 comments on commit a88b1ac

Please sign in to comment.