github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/extractor/extractor.go (about) 1 /* 2 * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved. 3 * This software is released under GPL3. 4 * The full license information can be found under: 5 * https://www.gnu.org/licenses/gpl-3.0.en.html 6 * 7 */ 8 9 package extractor 10 11 import ( 12 "fmt" 13 "github.com/vchain-us/vcn/pkg/api" 14 "github.com/vchain-us/vcn/pkg/uri" 15 ) 16 17 var extractors = map[string]Extractor{} 18 19 // Extractor extract an api.Artifact referenced by the given uri.URI. 20 type Extractor func(*uri.URI, ...Option) ([]*api.Artifact, error) 21 22 // Register the Extractor e for the given scheme 23 func Register(scheme string, e Extractor) { 24 extractors[scheme] = e 25 } 26 27 // Schemes returns the list of registered schemes. 28 func Schemes() []string { 29 schemes := make([]string, len(extractors)) 30 i := 0 31 for scheme := range extractors { 32 schemes[i] = scheme 33 i++ 34 } 35 return schemes 36 } 37 38 // Extract returns an []*api.Artifact for the given rawURIs. 39 func Extract(rawURIs []string, options ...Option) ([]*api.Artifact, error) { 40 artifacts := make([]*api.Artifact, 0) 41 for _, ru := range rawURIs { 42 u, err := uri.Parse(ru) 43 if err != nil { 44 return nil, err 45 } 46 if e, ok := extractors[u.Scheme]; ok { 47 ars, err := e(u, options...) 48 if err != nil { 49 return nil, err 50 } 51 artifacts = append(artifacts, ars...) 52 } else { 53 return nil, fmt.Errorf("%s scheme not yet supported", u.Scheme) 54 } 55 } 56 return artifacts, nil 57 }