Skip to content

Commit

Permalink
Rename file and pass flags as data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
stormvirux committed Jun 20, 2022
1 parent d541837 commit 352eefa
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions internal/getdoi/getdoi.go → internal/doi/doi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package getdoi
package doi

import (
"fmt"
Expand All @@ -8,11 +8,15 @@ import (
"strings"
)

type App struct{}
type Doi struct {
Arxiv bool
Clip bool
Verbose bool
}

// TODO: Pass flags to App and initialize with it
// TODO: Pass flags to Doi and initialize with it

func (a *App) Run(query []string, flags []bool) (string, error) {
func (a *Doi) Run(query []string) (string, error) {
var host = "https://api.crossref.org/works"

var (
Expand All @@ -21,51 +25,51 @@ func (a *App) Run(query []string, flags []bool) (string, error) {
extractedDoi string
// arXivID string
)
arxiv := flags[0]
// a.Arxiv := flags[0]
// clip := flags[1]
verbose := flags[2]
// a.Verbose := flags[2]
queryTxt = strings.Join(query, " ")

if ext := filepath.Ext(queryTxt); ext == ".pdf" {
var err error
// _ is arXivID
extractedTitle, extractedDoi, _, err = processPdf(queryTxt, verbose)
extractedTitle, extractedDoi, _, err = processPdf(queryTxt, a.Verbose)
if err != nil {
return "", err
}

if verbose {
if a.Verbose {
fmt.Printf("\nDetected Title: %s\n", extractedTitle)
}

if extractedDoi != "" {
verbosePrint(verbose, fmt.Sprintf("Detected DOI: %s\n", extractedDoi), os.Stdout)
verbosePrint(a.Verbose, fmt.Sprintf("Detected DOI: %s\n", extractedDoi), os.Stdout)
return extractedDoi, nil
}
if extractedTitle != "" {
queryTxt = extractedTitle
}
}

if arxiv {
if a.Arxiv {
host = "https://api.datacite.org/dois"
verbosePrint(verbose, "[Info] Retrieving data from DataCite", os.Stdout)
verbosePrint(a.Verbose, "[Info] Retrieving data from DataCite", os.Stdout)
extractedDoi, err := request.DoiDataCite(host, queryTxt)
if err != nil {
return "", fmt.Errorf("%w", err)
}

verbosePrint(verbose, fmt.Sprintf("\nDetected DOI: %s\n ", extractedDoi), os.Stdout)
fmt.Println(extractedDoi)
verbosePrint(a.Verbose, fmt.Sprintf("\nDetected DOI: %s\n ", extractedDoi), os.Stdout)
// fmt.Println(extractedDoi)
return extractedDoi, nil
}
verbosePrint(verbose, "[Info] Retrieving data from CrossRef", os.Stdout)
verbosePrint(a.Verbose, "[Info] Retrieving data from CrossRef", os.Stdout)
extractedDoi, err := request.DoiCrossRef(host, queryTxt)
if err != nil {
return "", fmt.Errorf("%w", err)
}

verbosePrint(verbose, fmt.Sprintf("\nDetected DOI: %s\n ", extractedDoi), os.Stderr)
fmt.Println(extractedDoi)
verbosePrint(a.Verbose, fmt.Sprintf("\nDetected DOI: %s\n ", extractedDoi), os.Stderr)
// fmt.Println(extractedDoi)
return extractedDoi, nil
}

0 comments on commit 352eefa

Please sign in to comment.