Skip to content

Commit

Permalink
cleanup common.go
Browse files Browse the repository at this point in the history
  • Loading branch information
miku committed Jan 4, 2022
1 parent e181af7 commit 4a079c5
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cmd/span-compare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
"text/template"

"github.com/miku/clam"
"github.com/miku/span"
"github.com/miku/span/solrutil"
"github.com/miku/span/xio"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -176,7 +176,7 @@ var SourceNames = map[string]string{
}

// TODO: move to XDG
var defaultConfigPath = path.Join(span.UserHomeDir(), ".config/span/span.json")
var defaultConfigPath = path.Join(xio.UserHomeDir(), ".config/span/span.json")

var (
amslLiveServer = flag.String("amsl", "", "url to live amsl api for ad-hoc source names, e.g. https://example.technology")
Expand Down
6 changes: 4 additions & 2 deletions cmd/span-review/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package main

import (
"bytes"
"github.com/segmentio/encoding/json"
"flag"
"fmt"
"io"
Expand All @@ -22,10 +21,13 @@ import (
"strings"
"text/tabwriter"

"github.com/segmentio/encoding/json"

"github.com/fatih/color"
"github.com/miku/span"
"github.com/miku/span/reviewutil"
"github.com/miku/span/solrutil"
"github.com/miku/span/xio"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -163,7 +165,7 @@ var (
textile = flag.Bool("t", false, "emit a textile table to stdout")
ascii = flag.Bool("a", false, "emit ascii table to stdout")
reviewFile = flag.String("c", "", "path to review.yaml file containing test cases, e.g. https://git.io/fh5Zh")
spanConfigFile = flag.String("span-config", path.Join(span.UserHomeDir(), ".config/span/span.json"), "gitlab, redmine tokens, whatislive location")
spanConfigFile = flag.String("span-config", path.Join(xio.UserHomeDir(), ".config/span/span.json"), "gitlab, redmine tokens, whatislive location")
ticket = flag.String("ticket", "", "post result to redmine, overrides review.yaml, requires redmine.baseurl and redmine.apitoken configured in span-config")
noCollapse = flag.Bool("C", false, "do not collapse details")
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/span-webhookd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/miku/span/configutil"
"github.com/miku/span/gitlab"
"github.com/miku/span/reviewutil"
"github.com/miku/span/xio"
log "github.com/sirupsen/logrus"
)

Expand All @@ -61,7 +62,7 @@ var (
token = flag.String("token", "", "gitlab auth token, if empty will use span-config")
repoDir = flag.String("repo-dir", path.Join(os.TempDir(), "span-webhookd/span"), "local repo clone path")
logfile = flag.String("logfile", "", "log to file")
spanConfigFile = flag.String("span-config", path.Join(span.UserHomeDir(), ".config/span/span.json"), "gitlab, redmine tokens, whatislive location")
spanConfigFile = flag.String("span-config", path.Join(xio.UserHomeDir(), ".config/span/span.json"), "gitlab, redmine tokens, whatislive location")
triggerPath = flag.String("trigger-path", "trigger", "path trigger, {host}:{port}/{trigger-path}")
banner = fmt.Sprintf(`[<>] webhookd %s`, span.AppVersion)

Expand Down
20 changes: 1 addition & 19 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ package span

import (
"fmt"
"os"
"regexp"
"runtime"
)

const (
Expand All @@ -39,27 +36,12 @@ const (
KeyLengthLimit = 250
)

// ISSNPattern is a regular expression matching standard ISSN.
var ISSNPattern = regexp.MustCompile(`[0-9]{4,4}-[0-9]{3,3}[0-9X]`)

// Skip marks records to skip.
type Skip struct {
Reason string
}

// Error returns the reason for skipping.
func (s Skip) Error() string {
return fmt.Sprintf("SKIP %s", s.Reason)
}

// UserHomeDir returns the home directory of the user.
func UserHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
return fmt.Sprintf("[skip] %s", s.Reason)
}
7 changes: 4 additions & 3 deletions filter/issn.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package filter

import (
"github.com/segmentio/encoding/json"
"strings"

"github.com/segmentio/encoding/json"

log "github.com/sirupsen/logrus"

"github.com/miku/span"
"github.com/miku/span/container"
"github.com/miku/span/formats/finc"
"github.com/miku/span/strutil"
"github.com/miku/span/xio"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func (f *ISSNFilter) UnmarshalJSON(p []byte) error {
line = strings.ToUpper(line)
// Sniff ISSNs.
issns := container.NewStringSet()
for _, s := range span.ISSNPattern.FindAllString(line, -1) {
for _, s := range strutil.ISSNPattern.FindAllString(line, -1) {
issns.Add(s)
}
if issns.Size() == 0 {
Expand Down
2 changes: 1 addition & 1 deletion formats/genios/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (doc Document) Authors() (authors []finc.Author) {
// ISSNList returns a list of ISSN.
func (doc Document) ISSNList() []string {
issns := container.NewStringSet()
for _, s := range span.ISSNPattern.FindAllString(doc.ISSN, -1) {
for _, s := range strutil.ISSNPattern.FindAllString(doc.ISSN, -1) {
issns.Add(s)
}
return issns.Values()
Expand Down
6 changes: 4 additions & 2 deletions quality/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package quality

import (
"github.com/segmentio/encoding/json"
"errors"
"fmt"
"net/url"
Expand All @@ -12,8 +11,11 @@ import (
"strings"
"time"

"github.com/segmentio/encoding/json"

"github.com/miku/span"
"github.com/miku/span/formats/finc"
"github.com/miku/span/strutil"
)

var (
Expand Down Expand Up @@ -336,7 +338,7 @@ func TestHasURL(is finc.IntermediateSchema) error {
// TestCanonicalISSN checks for the canonical ISSN format 1234-567X.
func TestCanonicalISSN(is finc.IntermediateSchema) error {
for _, issn := range append(is.ISSN, is.EISSN...) {
if !span.ISSNPattern.MatchString(issn) {
if !strutil.ISSNPattern.MatchString(issn) {
return Issue{Err: ErrNonCanonicalISSN, Record: is}
}
}
Expand Down
4 changes: 4 additions & 0 deletions strutil/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package strutil

import (
"html"
"regexp"
"strings"
)

// ISSNPattern is a regular expression matching standard ISSN.
var ISSNPattern = regexp.MustCompile(`[0-9]{4,4}-[0-9]{3,3}[0-9X]`)

// Truncate truncates a string.
func Truncate(s string, length int) string {
if len(s) < length || length < 0 {
Expand Down
13 changes: 13 additions & 0 deletions xio/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"os"
"runtime"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -355,3 +356,15 @@ func LoadSet(r io.Reader, m map[string]struct{}) error {
}
return nil
}

// UserHomeDir returns the home directory of the user.
func UserHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}

0 comments on commit 4a079c5

Please sign in to comment.