|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "regexp" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/forj-oss/forjj-modules/trace" |
| 13 | + "gopkg.in/yaml.v2" |
| 14 | +) |
| 15 | + |
| 16 | +func (a *jPluginsApp) doListInstalled() { |
| 17 | + if !a.readFromJenkins() { |
| 18 | + return |
| 19 | + } |
| 20 | + a.printOutVersion(a.installedPlugins) |
| 21 | +} |
| 22 | + |
| 23 | +// readFromJenkins read manifest of each plugins and store information in a.installedPlugins |
| 24 | +func (a *jPluginsApp) readFromJenkins() (_ bool) { |
| 25 | + pluginsPath := path.Join(*a.jenkinsHomePath, "plugins") |
| 26 | + |
| 27 | + a.installedPlugins = make(plugins) |
| 28 | + |
| 29 | + fEntries, err := ioutil.ReadDir(pluginsPath) |
| 30 | + |
| 31 | + if err != nil { |
| 32 | + gotrace.Error("Invalid Jenkins home '%s'. %s", pluginsPath, err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + var fileRE, manifestRE *regexp.Regexp |
| 37 | + fileREDefine := `^(.*)\.[jh]pi*$` |
| 38 | + manifestREDefine := `([\w-]*: )(.*)\n` |
| 39 | + |
| 40 | + if re, err := regexp.Compile(fileREDefine); err != nil { |
| 41 | + gotrace.Error("Internal error. Regex '%s': %s", fileREDefine, err) |
| 42 | + return |
| 43 | + } else { |
| 44 | + fileRE = re |
| 45 | + |
| 46 | + } |
| 47 | + if re, err := regexp.Compile(manifestREDefine); err != nil { |
| 48 | + gotrace.Error("Internal error. Regex '%s': %s", fileREDefine, err) |
| 49 | + return |
| 50 | + } else { |
| 51 | + manifestRE = re |
| 52 | + } |
| 53 | + |
| 54 | + for _, fEntry := range fEntries { |
| 55 | + if fEntry.IsDir() { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + if fileMatch := fileRE.FindAllStringSubmatch(fEntry.Name(), -1); fileMatch != nil { |
| 60 | + pluginFileName := fileMatch[0][0] |
| 61 | + pluginName := fileMatch[0][1] |
| 62 | + |
| 63 | + if pluginFileName != "" && pluginName == "" { |
| 64 | + gotrace.Error("Invalid file '%s'. Ignored.", pluginFileName) |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + pluginMetafile := path.Join(pluginsPath, pluginName, "META-INF", "MANIFEST.MF") |
| 69 | + |
| 70 | + if _, err := os.Stat(pluginMetafile); err != nil && os.IsNotExist(err) { |
| 71 | + // TODO: Ignored for now. but may need to extract the plugin file to get the version |
| 72 | + gotrace.Warning("Plugin '%s' found but not expanded. Ignored. (fix in next jplugins version)", pluginMetafile) |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + var manifest *pluginManifest |
| 77 | + |
| 78 | + if d, err := ioutil.ReadFile(pluginMetafile); err != nil { |
| 79 | + gotrace.Error("Unable to read file '%s'. %s. Ignored", pluginMetafile, err) |
| 80 | + continue |
| 81 | + } else { |
| 82 | + // Remove DOS format if exist |
| 83 | + data := strings.Replace(string(d), "\r", "", -1) |
| 84 | + // and remove new lines ('\n ') |
| 85 | + data = strings.Replace(data, "\n ", "", -1) |
| 86 | + // and escape " |
| 87 | + data = strings.Replace(data, "\"", "\\\"", -1) |
| 88 | + // and embrace value part with " |
| 89 | + data = manifestRE.ReplaceAllString(data, `$1"$2"`+"\n") |
| 90 | + |
| 91 | + manifest = new(pluginManifest) |
| 92 | + if err := yaml.Unmarshal([]byte(data), &manifest); err != nil { |
| 93 | + gotrace.Error("Unable to read file '%s' as yaml. %s. Ignored", pluginMetafile, err) |
| 94 | + fmt.Print(data) |
| 95 | + continue |
| 96 | + } |
| 97 | + } |
| 98 | + a.installedPlugins[manifest.Name] = manifest |
| 99 | + } |
| 100 | + } |
| 101 | + return true |
| 102 | +} |
| 103 | + |
| 104 | +func (a *jPluginsApp) printOutVersion(plugins plugins) (_ bool) { |
| 105 | + if a.installedPlugins == nil { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + pluginsList := make([]string, len(plugins)) |
| 110 | + |
| 111 | + |
| 112 | + iCount := 0 |
| 113 | + for name := range plugins { |
| 114 | + pluginsList[iCount] = name |
| 115 | + iCount++ |
| 116 | + } |
| 117 | + |
| 118 | + sort.Strings(pluginsList) |
| 119 | + |
| 120 | + for _, name := range pluginsList { |
| 121 | + fmt.Printf("%s: %s\n", name, plugins[name].Version) |
| 122 | + } |
| 123 | + fmt.Println(iCount, "plugin(s)") |
| 124 | + return true |
| 125 | +} |
0 commit comments