github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/extractor/nodecom/nodecom.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 nodecom 10 11 import ( 12 "encoding/json" 13 "errors" 14 "github.com/vchain-us/vcn/pkg/bom/node" 15 "io/ioutil" 16 "strings" 17 18 "github.com/vchain-us/vcn/pkg/api" 19 "github.com/vchain-us/vcn/pkg/extractor" 20 "github.com/vchain-us/vcn/pkg/uri" 21 ) 22 23 // Scheme for java component 24 const Scheme = "nodecom" 25 26 // Artifact returns a file *api.Artifact from a given u 27 func Artifact(u *uri.URI, options ...extractor.Option) ([]*api.Artifact, error) { 28 29 if u.Scheme != Scheme { 30 return nil, nil 31 } 32 33 componentFolder := strings.TrimPrefix(u.Opaque, "//") 34 35 pJSONP, err := node.GetPackageJsonPath(componentFolder) 36 if err != nil { 37 return nil, err 38 } 39 o1, err := ioutil.ReadFile(pJSONP) 40 if err != nil { 41 return nil, err 42 } 43 44 var plJson map[string]interface{} 45 err = json.Unmarshal(o1, &plJson) 46 if err != nil { 47 return nil, err 48 } 49 50 name, ok := plJson["name"].(string) 51 if !ok { 52 return nil, errors.New("malformed package json") 53 } 54 version, ok := plJson["version"].(string) 55 if !ok { 56 return nil, errors.New("malformed package json") 57 } 58 59 digest, err := node.GetNodeComDigest(componentFolder) 60 if err != nil { 61 return nil, err 62 } 63 64 m := api.Metadata{} 65 m["version"] = version 66 m["name"] = name 67 68 return []*api.Artifact{{ 69 Kind: "nodecom", 70 Name: name + "-" + version, 71 Hash: digest.Encoded(), 72 Metadata: m, 73 }}, nil 74 }