github.com/AiRISTAFlowInc/fs-cli@v0.2.6/util/contrib.go (about) 1 package util 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "strings" 10 ) 11 12 const ( 13 fileDescriptorJson string = "descriptor.json" 14 ) 15 16 var contribDescriptors = []string{"descriptor.json", "activity.json", "trigger.json", "action.json"} 17 18 // FlogoAppDescriptor is the descriptor for a Flogo application 19 type FlogoContribDescriptor struct { 20 Name string `json:"name"` 21 Type string `json:"type"` 22 Version string `json:"version"` 23 Description string `json:"description"` 24 Homepage string `json:"homepage"` 25 Shim string `json:"shim"` 26 Ref string `json:"ref"` //legacy 27 28 IsLegacy bool `json:"-"` 29 } 30 31 type FlogoContribBundleDescriptor struct { 32 Name string `json:"name"` 33 Description string `json:"description"` 34 Contribs []string `json:"contributions"` 35 } 36 37 func (d *FlogoContribDescriptor) GetContribType() string { 38 parts := strings.Split(d.Type, ":") 39 if len(parts) > 1 { 40 return parts[1] 41 } 42 return "" 43 } 44 45 46 func GetContribDescriptorFromImport(depManager DepManager, contribImport Import) (*FlogoContribDescriptor, error) { 47 48 contribPath, err := depManager.GetPath(contribImport) 49 if err != nil { 50 return nil, err 51 } 52 53 return GetContribDescriptor(contribPath) 54 } 55 56 func GetContribDescriptor(contribPath string) (*FlogoContribDescriptor, error) { 57 58 var descriptorPath string 59 oldDescriptor := false 60 61 for _, descriptorName := range contribDescriptors { 62 dPath := filepath.Join(contribPath, descriptorName) 63 if _, err := os.Stat(dPath); err == nil { 64 if descriptorName != "descriptor.json" { 65 oldDescriptor = true 66 } 67 descriptorPath = dPath 68 } 69 } 70 71 if descriptorPath == "" { 72 //descriptor not found 73 return nil, nil 74 } 75 76 desc, err := ReadContribDescriptor(descriptorPath) 77 if err != nil { 78 return nil, err 79 } 80 81 if desc.Ref != "" && oldDescriptor { 82 desc.IsLegacy = true 83 } 84 85 return desc, nil 86 } 87 88 func ReadContribDescriptor(descriptorFile string) (*FlogoContribDescriptor, error) { 89 90 descriptorJson, err := os.Open(descriptorFile) 91 if err != nil { 92 return nil, err 93 } 94 95 bytes, err := ioutil.ReadAll(descriptorJson) 96 if err != nil { 97 return nil, err 98 } 99 100 descriptor := &FlogoContribDescriptor{} 101 102 err = json.Unmarshal(bytes, descriptor) 103 if err != nil { 104 return nil, fmt.Errorf("failed to parse descriptor '%s': %s", descriptorFile, err.Error()) 105 } 106 107 return descriptor, nil 108 } 109 110 func GetContribType(depManager DepManager, ref string) (string, error) { 111 112 refAsFlogoImport, err := NewFlogoImportFromPath(ref) 113 if err != nil { 114 return "", err 115 } 116 117 impPath, err := depManager.GetPath(refAsFlogoImport)//(refAsFlogoImport) 118 if err != nil { 119 return "", err 120 } 121 var descriptorPath string 122 123 if _, err := os.Stat(filepath.Join(impPath, fileDescriptorJson)); err == nil { 124 descriptorPath = filepath.Join(impPath, fileDescriptorJson) 125 126 } else if _, err := os.Stat(filepath.Join(impPath, "activity.json")); err == nil { 127 descriptorPath = filepath.Join(impPath, "activity.json") 128 } else if _, err := os.Stat(filepath.Join(impPath, "trigger.json")); err == nil { 129 descriptorPath = filepath.Join(impPath, "trigger.json") 130 } else if _, err := os.Stat(filepath.Join(impPath, "action.json")); err == nil { 131 descriptorPath = filepath.Join(impPath, "action.json") 132 } 133 134 if _, err := os.Stat(descriptorPath); descriptorPath != "" && err == nil { 135 136 desc, err := ReadContribDescriptor(descriptorPath) 137 if err != nil { 138 return "", err 139 } 140 141 return desc.Type, nil 142 } 143 144 return "", nil 145 }