Skip to content

Commit

Permalink
rename package
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Apr 22, 2024
1 parent 62cc488 commit 3a00660
Show file tree
Hide file tree
Showing 26 changed files with 142 additions and 96 deletions.
2 changes: 1 addition & 1 deletion cff/cff.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cff

import "github.com/front-matter/commonmeta-go/types"
import "commonmeta-go/types"

type Content struct {
ID string `json:"id"`
Expand Down
8 changes: 4 additions & 4 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"fmt"
"strings"

"github.com/front-matter/commonmeta-go/datacite"
"github.com/front-matter/commonmeta-go/doiutils"
"github.com/front-matter/commonmeta-go/types"
"commonmeta-go/datacite"
"commonmeta-go/doiutils"
"commonmeta-go/types"

"github.com/front-matter/commonmeta-go/crossref"
"commonmeta-go/crossref"

"github.com/spf13/cobra"
)
Expand Down
66 changes: 49 additions & 17 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,68 @@ Copyright © 2024 Front Matter <[email protected]>
package cmd

import (
"bytes"
"encoding/json"
"fmt"

"commonmeta-go/commonmeta"

"commonmeta-go/crossref"

"commonmeta-go/datacite"

"commonmeta-go/types"

"github.com/spf13/cobra"
)

// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
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: "A list of works",
Long: `A list of works. Currently only available for
the Crossref and DataCite provider. Options include numnber of works,
work type, and Crossref member id or DataCite client id. For example:
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.`,
commonmeta list --number 10 --member 78 --type journal-article,
commonmeta list --number 10 --member cern.zenodo --type dataset`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("list called")
number, _ := cmd.Flags().GetInt("number")
from, _ := cmd.Flags().GetString("from")

member, _ := cmd.Flags().GetString("member")
type_, _ := cmd.Flags().GetString("type")
hasORCID, _ := cmd.Flags().GetBool("has-orcid")
hasROR, _ := cmd.Flags().GetBool("has-ror-id")
hasReferences, _ := cmd.Flags().GetBool("has-references")
hasRelation, _ := cmd.Flags().GetBool("has-relation")
hasAbstract, _ := cmd.Flags().GetBool("has-abstract")
hasAward, _ := cmd.Flags().GetBool("has-award")
hasLicense, _ := cmd.Flags().GetBool("has-license")
hasArchive, _ := cmd.Flags().GetBool("has-archive")

var data []types.Data
var err error
sample := false
if from == "crossref" {
data, err = crossref.FetchCrossrefList(number, member, type_, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive)
} else if from == "datacite" {
data, err = datacite.FetchDataciteList(number, sample)
}
if err != nil {
fmt.Println(err)
}
output, jsErr := commonmeta.WriteCommonmetaList(data)
var out bytes.Buffer
json.Indent(&out, output, "=", "\t")
fmt.Println(out.String())

if jsErr != nil {
fmt.Println(jsErr)
}
},
}

func init() {
rootCmd.AddCommand(listCmd)

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

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func init() {
rootCmd.PersistentFlags().StringP("from", "f", "", "the format to convert from")
rootCmd.PersistentFlags().StringP("to", "t", "commonmeta", "the format to convert to")

rootCmd.PersistentFlags().StringP("number", "n", "10", "number of samples")
rootCmd.PersistentFlags().StringP("number", "n", "10", "number of results")
rootCmd.PersistentFlags().StringP("member", "m", "", "Crossref member ID")
rootCmd.PersistentFlags().StringP("type", "", "journal-article", "work type")
rootCmd.PersistentFlags().BoolP("has-orcid", "", false, "has one or more ORCID IDs")
Expand Down
32 changes: 18 additions & 14 deletions cmd/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import (
"encoding/json"
"fmt"

"github.com/front-matter/commonmeta-go/commonmeta"
"github.com/front-matter/commonmeta-go/crossref"
"github.com/front-matter/commonmeta-go/datacite"
"github.com/front-matter/commonmeta-go/types"
"commonmeta-go/commonmeta"

"commonmeta-go/crossref"

"commonmeta-go/datacite"

"commonmeta-go/types"

"github.com/spf13/cobra"
)
Expand All @@ -21,13 +24,15 @@ var sampleCmd = &cobra.Command{
Use: "sample",
Short: "A random sample of works",
Long: `A random sample of works. Currently only available for
the Crossref API. Options include numnber of samples, Crossref
member id and work type. For example:
the Crossref and DataCite provider. Options include numnber of samples,
work type, and Crossref member id or DataCite client id. For example:
commonmeta sample --number 10 --member 78 --type journal-article,
commonmeta sample --number 10 --member cern.zenodo --type dataset`,

commonmeta sample --number 10 --member 78 --type journal-article`,
Run: func(cmd *cobra.Command, args []string) {
number, _ := cmd.Flags().GetInt("number")
provider, _ := cmd.Flags().GetString("provider")
from, _ := cmd.Flags().GetString("from")

member, _ := cmd.Flags().GetString("member")
type_, _ := cmd.Flags().GetString("type")
Expand All @@ -42,10 +47,11 @@ var sampleCmd = &cobra.Command{

var data []types.Data
var err error
if provider == "crossref" {
data, err = crossref.FetchCrossrefSample(number, member, type_, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive)
} else if provider == "datacite" {
data, err = datacite.FetchDataciteSample(number)
sample := true
if from == "crossref" {
data, err = crossref.FetchCrossrefList(number, member, type_, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive)
} else if from == "datacite" {
data, err = datacite.FetchDataciteList(number, sample)
}
if err != nil {
fmt.Println(err)
Expand All @@ -63,6 +69,4 @@ var sampleCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(sampleCmd)

sampleCmd.PersistentFlags().StringP("provider", "", "crossref", "the provider to get a sample from")
}
2 changes: 1 addition & 1 deletion codemeta/codemeta.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package codemeta

import "github.com/front-matter/commonmeta-go/types"
import "commonmeta-go/types"

type Content struct {
ID string `json:"id"`
Expand Down
4 changes: 2 additions & 2 deletions commonmeta/commonmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"fmt"

"github.com/front-matter/commonmeta-go/schemautils"
"github.com/front-matter/commonmeta-go/types"
"commonmeta-go/schemautils"
"commonmeta-go/types"

"github.com/xeipuuv/gojsonschema"
)
Expand Down
26 changes: 15 additions & 11 deletions crossref/crossref.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"strings"
"time"

"github.com/front-matter/commonmeta-go/dateutils"
"github.com/front-matter/commonmeta-go/doiutils"
"github.com/front-matter/commonmeta-go/types"
"github.com/front-matter/commonmeta-go/utils"
"commonmeta-go/dateutils"
"commonmeta-go/doiutils"
"commonmeta-go/types"
"commonmeta-go/utils"
)

type Content struct {
Expand Down Expand Up @@ -247,10 +247,10 @@ func FetchCrossref(str string) (types.Data, error) {
return data, nil
}

func FetchCrossrefSample(number int, member string, _type string, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]types.Data, error) {
func FetchCrossrefList(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]types.Data, error) {

var data []types.Data
content, err := GetCrossrefSample(number, member, _type, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive)
content, err := GetCrossrefList(number, member, _type, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive)
if err != nil {
return data, err
}
Expand Down Expand Up @@ -566,7 +566,7 @@ func ReadCrossref(content Content) (types.Data, error) {
return data, nil
}

func GetCrossrefSample(number int, member string, _type string, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]Content, error) {
func GetCrossrefList(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) ([]Content, error) {
// the envelope for the JSON response from the Crossref API
type Response struct {
Status string `json:"status"`
Expand All @@ -581,11 +581,11 @@ func GetCrossrefSample(number int, member string, _type string, hasORCID bool, h
if number > 100 {
number = 100
}
url := CrossrefApiSampleUrl(number, member, _type, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive)
url := CrossrefQueryUrl(number, member, _type, sample, hasORCID, hasROR, hasReferences, hasRelation, hasAbstract, hasAward, hasLicense, hasArchive)
req, err := http.NewRequest("GET", url, nil)
v := "0.1"
u := "[email protected]"
userAgent := fmt.Sprintf("commonmeta-go/%s (https://commonmeta.org/commonmeta-go/; mailto: %s)", v, u)
userAgent := fmt.Sprintf("commonmeta-go/%s (https://commonmeta.org; mailto: %s)", v, u)
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Cache-Control", "private")
if err != nil {
Expand Down Expand Up @@ -613,7 +613,7 @@ func GetCrossrefSample(number int, member string, _type string, hasORCID bool, h
return response.Message.Items, nil
}

func CrossrefApiSampleUrl(number int, member string, _type string, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) string {
func CrossrefQueryUrl(number int, member string, _type string, sample bool, hasORCID bool, hasROR bool, hasReferences bool, hasRelation bool, hasAbstract bool, hasAward bool, hasLicense bool, hasArchive bool) string {
types := []string{
"book-section",
"monograph",
Expand Down Expand Up @@ -660,7 +660,11 @@ func CrossrefApiSampleUrl(number int, member string, _type string, hasORCID bool
}
u, _ := url.Parse("https://api.crossref.org/works")
values := u.Query()
values.Add("sample", strconv.Itoa(number))
if sample {
values.Add("sample", strconv.Itoa(number))
} else {
values.Add("rows", strconv.Itoa(number))
}
var filters []string
if member != "" {
filters = append(filters, "member:"+member)
Expand Down
34 changes: 18 additions & 16 deletions crossref/crossref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"strings"
"testing"

"github.com/front-matter/commonmeta-go/crossref"
"github.com/front-matter/commonmeta-go/doiutils"
"github.com/front-matter/commonmeta-go/types"
"commonmeta-go/crossref"
"commonmeta-go/doiutils"
"commonmeta-go/types"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -96,51 +96,53 @@ func TestFetchCrossref(t *testing.T) {
}
}

func TestCrossrefApiSampleUrl(t *testing.T) {
func TestCrossrefQueryUrl(t *testing.T) {
t.Parallel()

type testCase struct {
number int
member string
_type string
sample bool
want string
}

testCases := []testCase{
{number: 10, member: "340", _type: "journal-article", want: "https://api.crossref.org/works?filter=member%3A340%2Ctype%3Ajournal-article&sample=10"},
{number: 1, member: "", _type: "posted-content", want: "https://api.crossref.org/works?filter=type%3Aposted-content&sample=1"},
{number: 20, member: "78", _type: "", want: "https://api.crossref.org/works?filter=member%3A78&sample=20"},
{number: 120, member: "", _type: "", want: "https://api.crossref.org/works?sample=120"},
{number: 10, member: "340", _type: "journal-article", sample: false, want: "https://api.crossref.org/works?filter=member%3A340%2Ctype%3Ajournal-article&sample=10"},
{number: 1, member: "", _type: "posted-content", sample: true, want: "https://api.crossref.org/works?filter=type%3Aposted-content&sample=1"},
{number: 20, member: "78", _type: "", sample: true, want: "https://api.crossref.org/works?filter=member%3A78&sample=20"},
{number: 120, member: "", _type: "", sample: true, want: "https://api.crossref.org/works?sample=120"},
}
for _, tc := range testCases {
got := crossref.CrossrefApiSampleUrl(tc.number, tc.member, tc._type, false, false, false, false, false, false, false, false)
got := crossref.CrossrefQueryUrl(tc.number, tc.member, tc._type, tc.sample, false, false, false, false, false, false, false, false)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("CrossrefApiSampleUrl mismatch (-want +got):\n%s", diff)
t.Errorf("CrossrefQueryUrl mismatch (-want +got):\n%s", diff)
}
}
}

func TestGetCrossrefSample(t *testing.T) {
func TestGetCrossrefList(t *testing.T) {
t.Parallel()

type testCase struct {
number int
member string
_type string
sample bool
}

testCases := []testCase{
{number: 3, member: "340", _type: "journal-article"},
{number: 1, member: "", _type: "posted-content"},
{number: 2, member: "", _type: ""},
{number: 3, member: "340", _type: "journal-article", sample: false},
{number: 1, member: "", _type: "posted-content", sample: true},
{number: 2, member: "", _type: "", sample: true},
}
for _, tc := range testCases {
got, err := crossref.GetCrossrefSample(tc.number, tc.member, tc._type, false, false, false, false, false, false, false, false)
got, err := crossref.GetCrossrefList(tc.number, tc.member, tc._type, true, false, false, false, false, false, false, false, false)
if err != nil {
t.Errorf("GetCrossrefSample(%v): error %v", tc.number, err)
}
if diff := cmp.Diff(tc.number, len(got)); diff != "" {
t.Errorf("GetCrossrefSample mismatch (-want +got):\n%s", diff)
t.Errorf("GetCrossrefList mismatch (-want +got):\n%s", diff)
}
}
}
2 changes: 1 addition & 1 deletion crossrefxml/crossrefxml.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package crossrefxml

import (
"github.com/front-matter/commonmeta-go/types"
"commonmeta-go/types"
)

type Content struct {
Expand Down
2 changes: 1 addition & 1 deletion csl/csl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package csl

import "github.com/front-matter/commonmeta-go/types"
import "commonmeta-go/types"

type Content struct {
ID string `json:"id"`
Expand Down
Loading

0 comments on commit 3a00660

Please sign in to comment.