Skip to content

Commit

Permalink
feat: enhance support nested source: target
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 14, 2021
1 parent 181e56a commit 01ca716
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions probe/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,43 @@ func (es invalidURIs) Error() string {
return "Invalid URI: " + strings.Join(ss, ", ")
}

type ignoreSet []string

func (is ignoreSet) Has(s string) bool {
for _, i := range is {
if i == s {
return true
}
}
return false
}

func normalizeSourceURL(u *url.URL) *url.URL {
if u.Opaque == "" {
return &url.URL{Scheme: "source", Opaque: u.Path}
} else {
return &url.URL{Scheme: "source", Opaque: u.Opaque}
}
}

type SourceProbe struct {
target *url.URL
}

func NewSourceProbe(u *url.URL) (SourceProbe, error) {
s := SourceProbe{}
if u.Opaque == "" {
s.target = &url.URL{Scheme: "source", Opaque: u.Path}
} else {
s.target = &url.URL{Scheme: "source", Opaque: u.Opaque}
s := SourceProbe{
target: normalizeSourceURL(u),
}
_, err := s.load()
_, err := s.load(s.target.Opaque, nil)
return s, err
}

func (p SourceProbe) Target() *url.URL {
return p.target
}

func (p SourceProbe) load() ([]Probe, error) {
f, err := os.Open(p.target.Opaque)
func (p SourceProbe) load(path string, ignores ignoreSet) ([]Probe, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
Expand All @@ -58,6 +74,25 @@ func (p SourceProbe) load() ([]Probe, error) {
continue
}

if strings.HasPrefix(target, "source:") {
u, err := url.Parse(target)
if err != nil {
invalids = append(invalids, target)
continue
}
u = normalizeSourceURL(u)
if ignores.Has(u.Opaque) {
continue
}
ps, err := p.load(u.Opaque, append(ignores, path))
if err == nil {
probes = append(probes, ps...)
} else if es, ok := err.(invalidURIs); ok {
invalids = append(invalids, es...)
}
continue
}

probe, err := Get(target)
if err != nil {
invalids = append(invalids, target)
Expand All @@ -76,7 +111,7 @@ func (p SourceProbe) load() ([]Probe, error) {
func (p SourceProbe) Check() []store.Record {
stime := time.Now()

probes, err := p.load()
probes, err := p.load(p.target.Opaque, nil)
if err != nil {
d := time.Now().Sub(stime)
return []store.Record{{
Expand Down

0 comments on commit 01ca716

Please sign in to comment.