Remove plugin manager

This commit is contained in:
Zachary Yedidia 2019-03-17 19:03:37 -04:00
parent a2b9acd153
commit 15dff722b0
6 changed files with 9 additions and 400 deletions

View file

@ -12,7 +12,6 @@ import (
"github.com/zyedidia/micro/internal/action"
"github.com/zyedidia/micro/internal/buffer"
"github.com/zyedidia/micro/internal/config"
"github.com/zyedidia/micro/internal/manager"
"github.com/zyedidia/micro/internal/screen"
"github.com/zyedidia/micro/internal/util"
"github.com/zyedidia/tcell"
@ -28,7 +27,6 @@ var (
flagStartPos = flag.String("startpos", "", "LINE,COL to start the cursor at when opening a buffer.")
flagConfigDir = flag.String("config-dir", "", "Specify a custom location for the configuration directory")
flagOptions = flag.Bool("options", false, "Show all option help")
flagPlugin = flag.String("plugin", "", "Run a plugin manager action")
)
func InitFlags() {
@ -40,9 +38,6 @@ func InitFlags() {
fmt.Println("+LINE:COL")
fmt.Println(" \tSpecify a line and column to start the cursor at when opening a buffer")
fmt.Println(" \tThis can also be done by opening file:LINE:COL")
fmt.Println("-plugin ACTION")
fmt.Println(" \tRun a plugin manager action")
fmt.Println(" \tActions include: list, add, remove")
fmt.Println("-options")
fmt.Println(" \tShow all option help")
fmt.Println("-version")
@ -63,11 +58,6 @@ func InitFlags() {
flag.Parse()
err := config.InitConfigDir(*flagConfigDir)
if err != nil {
screen.TermMessage(err)
}
if *flagVersion {
// If -version was passed
fmt.Println("Version:", util.Version)
@ -76,22 +66,6 @@ func InitFlags() {
os.Exit(0)
}
if len(*flagPlugin) != 0 {
action := *flagPlugin
// args := flag.Args()
if action == "list" {
plugins, err := manager.ListInstalledPlugins()
if err != nil {
fmt.Println(err)
} else {
for _, p := range plugins {
fmt.Println(p.Info.Name, p.Version)
}
}
}
os.Exit(0)
}
if *flagOptions {
// If -options was passed
for k, v := range config.DefaultGlobalSettings() {
@ -184,9 +158,13 @@ func main() {
}
config.InitGlobalSettings()
// InitConfigDir happens in InitFlags
InitFlags()
err = config.InitConfigDir(*flagConfigDir)
if err != nil {
screen.TermMessage(err)
}
action.InitBindings()
action.InitCommands()

5
go.mod
View file

@ -2,12 +2,14 @@ module github.com/zyedidia/micro
require (
github.com/blang/semver v3.5.1+incompatible
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0
github.com/flynn/json5 v0.0.0-20160717195620-7620272ed633
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-errors/errors v1.0.1
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 // indirect
github.com/lucasb-eyer/go-colorful v0.0.0-20181028223441-12d3b2882a08 // indirect
github.com/mattn/go-isatty v0.0.4
@ -26,10 +28,11 @@ require (
github.com/zyedidia/pty v1.1.2-0.20180126010845-30364665a244 // indirect
github.com/zyedidia/tcell v0.0.0-20190204041104-518c15c24302
github.com/zyedidia/terminal v0.0.0-20180726154117-533c623e2415
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 // indirect
golang.org/x/text v0.3.0
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
gopkg.in/src-d/go-git.v4 v4.9.1
gopkg.in/yaml.v2 v2.2.2
layeh.com/gopher-luar v1.0.4
)

39
go.sum
View file

@ -1,7 +1,3 @@
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
@ -10,29 +6,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/emirpasic/gods v1.9.0 h1:rUF4PuzEjMChMiNsVjdI+SyLu7rEqpQ5reNFnhC7oFo=
github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/flynn/json5 v0.0.0-20160717195620-7620272ed633 h1:xJMmr4GMYIbALX5edyoDIOQpc2bOQTeJiWMeCl9lX/8=
github.com/flynn/json5 v0.0.0-20160717195620-7620272ed633/go.mod h1:NJDK3/o7abx6PP54EOe0G0n0RLmhCo9xv61gUYpI0EY=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg=
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8=
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@ -46,15 +29,10 @@ github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/npat-efault/poller v2.0.0+incompatible h1:jtTdXWKgN5kDK41ts8hoY1rvTEi0K08MTB8/bRO9MqE=
github.com/npat-efault/poller v2.0.0+incompatible/go.mod h1:lni01B89P8PtVpwlAhdhK1niN5rPkDGGpGGgBJzpSgo=
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d h1:1VUlQbCfkoSGv7qP7Y+ro3ap1P1pPZxgdGVqiTVy5C4=
@ -65,14 +43,9 @@ github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PX
github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c h1:Ho+uVpkel/udgjbwB5Lktg9BtvJSh2DT0Hi6LPSyI2w=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c/go.mod h1:XDJAKZRPZ1CvBcN2aX5YOUTYGHki24fSF0Iv48Ibg0s=
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro=
github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8=
github.com/yuin/gopher-lua v0.0.0-20190125051437-7b9317363aa9 h1:Wy3fAQLBPP0JSWdq3kBnmbFgXDHcyhtPpd+8kENV7mU=
github.com/yuin/gopher-lua v0.0.0-20190125051437-7b9317363aa9/go.mod h1:fFiAh+CowNFr0NK5VASokuwKwkbacRmHsVA7Yb1Tqac=
github.com/zyedidia/clipboard v0.0.0-20180718195219-bd31d747117d h1:Lhqt2eo+rgM8aswvM7nTtAMVm8ARPWzkE9n6eZDOccY=
@ -87,10 +60,6 @@ github.com/zyedidia/tcell v0.0.0-20190204041104-518c15c24302 h1:ruNSURcO81y+J+Xn
github.com/zyedidia/tcell v0.0.0-20190204041104-518c15c24302/go.mod h1:yXgdp23+aW8OMENYVBvpKoeiBtjaVWJ9HhpPDu6LBfM=
github.com/zyedidia/terminal v0.0.0-20180726154117-533c623e2415 h1:752dTQ5OatJ9M5ULK2+9lor+nzyZz+LYDo3WGngg3Rc=
github.com/zyedidia/terminal v0.0.0-20180726154117-533c623e2415/go.mod h1:8leT8G0Cm8NoJHdrrKHyR9MirWoF4YW7pZh06B6H+1E=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9 h1:lkiLiLBHGoH3XnqSLUIaBsilGMUjI+Uy2Xu2JLUtTas=
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
@ -102,14 +71,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
gopkg.in/src-d/go-billy.v4 v4.2.1 h1:omN5CrMrMcQ+4I8bJ0wEhOBPanIRWzFC953IiXKdYzo=
gopkg.in/src-d/go-billy.v4 v4.2.1/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1 h1:XWW/s5W18RaJpmo1l0IYGqXKuJITWRFuA45iOf1dKJs=
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
gopkg.in/src-d/go-git.v4 v4.9.1 h1:0oKHJZY8tM7B71378cfTg2c5jmWyNlXvestTT6WfY+4=
gopkg.in/src-d/go-git.v4 v4.9.1/go.mod h1:Vtut8izDyrM8BUVQnzJ+YvmNcem2J89EmfZYCkLokZk=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
layeh.com/gopher-luar v1.0.4 h1:BFgt94J/CXh4HkDcE2b7A7pBaVeQKEVfHEBRKL/K/Tc=

View file

@ -1,85 +0,0 @@
package manager
import (
"fmt"
"io/ioutil"
"net/http"
"path"
"github.com/blang/semver"
"github.com/zyedidia/micro/internal/config"
"github.com/zyedidia/micro/internal/util"
git "gopkg.in/src-d/go-git.v4"
)
// NewPluginInfoFromUrl creates a new PluginInfo from a URL by fetching
// the data at that URL and parsing the JSON (running a GET request at
// the URL should return the JSON for a plugin info)
func NewPluginInfoFromUrl(url string) (*PluginInfo, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
dat, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return NewPluginInfo(dat)
}
// FetchRepo downloads this plugin's git repository
func (i *PluginInfo) FetchRepo() error {
dir := path.Join(config.ConfigDir, "plugin", i.Name)
r, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: i.Repo,
Progress: nil,
})
if err != nil {
return err
}
p := &Plugin{
Info: i,
Dir: dir,
Repo: r,
}
err = p.ResolveVersion()
if err != nil {
return err
}
err = p.WriteVersion()
return err
}
func (p *Plugin) ResolveVersion() error {
i := p.Info
vs := i.Versions
for _, v := range vs {
microrange, err := semver.ParseRange(v.Require["micro"])
if err != nil {
return err
}
if microrange(util.SemVersion) {
p.Version = v.Vers
fmt.Println("resolve version to ", v.Vstr)
return nil
}
}
return ErrRequireUnsat
}
func (p *Plugin) WriteVersion() error {
return ioutil.WriteFile(path.Join(p.Dir, versionfile), []byte(p.Version.String()), 0644)
}
func (p *Plugin) PostInstallHooks() error {
return nil
}

View file

@ -1,75 +0,0 @@
package manager
import (
"testing"
"github.com/blang/semver"
"github.com/zyedidia/micro/internal/config"
"github.com/zyedidia/micro/internal/util"
)
func init() {
config.InitConfigDir("./")
util.Version = "1.3.1"
util.SemVersion, _ = semver.Make(util.Version)
}
var sampleJson = []byte(`{
"name": "comment",
"description": "Plugin to auto comment or uncomment lines",
"website": "https://github.com/micro-editor/comment-plugin",
"repository": "https://github.com/micro-editor/comment-plugin",
"versions": [
{
"version": "1.0.6",
"tag": "v1.0.6",
"require": {
"micro": ">=1.1.0"
}
},
{
"version": "1.0.5",
"tag": "v1.0.5",
"require": {
"micro": ">=1.0.0"
}
},
{
"version": "1.0.6-dev",
"tag": "nightly",
"require": {
"micro": ">=1.3.1"
}
}
]
}`)
func TestParse(t *testing.T) {
_, err := NewPluginInfo(sampleJson)
if err != nil {
t.Error(err)
}
}
func TestFetch(t *testing.T) {
i, err := NewPluginInfoFromUrl("http://zbyedidia.webfactional.com/micro/test.json")
if err != nil {
t.Error(err)
}
err = i.FetchRepo()
if err != nil {
t.Error(err)
}
}
// func TestList(t *testing.T) {
// is, err := ListInstalledPlugins()
// if err != nil {
// t.Error(err)
// }
//
// for _, i := range is {
// fmt.Println(i.dir)
// }
// }

View file

@ -1,173 +0,0 @@
package manager
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"path"
"github.com/blang/semver"
"github.com/zyedidia/micro/internal/config"
git "gopkg.in/src-d/go-git.v4"
)
var (
ErrMissingName = errors.New("Missing or empty name field")
ErrMissingDesc = errors.New("Missing or empty description field")
ErrMissingSite = errors.New("Missing or empty website field")
ErrMissingRepo = errors.New("Missing or empty repository field")
ErrMissingVersions = errors.New("Missing or empty versions field")
ErrMissingTag = errors.New("Missing or empty tag field")
ErrMissingRequire = errors.New("Missing or empty require field")
ErrRequireUnsat = errors.New("Version require could not be satisfied")
)
const (
infojson = "plugin.json"
versionfile = "version.lock"
)
type Plugin struct {
Info *PluginInfo
Dir string
Repo *git.Repository
Version semver.Version // currently installed version
}
func (p *Plugin) GetRequires() *PluginVersion {
for _, v := range p.Info.Versions {
if p.Version.Equals(v.Vers) {
return &v
}
}
return nil
}
// PluginVersion describes a version for a plugin as well as any dependencies that
// version might have
// This marks a tag that corresponds to the version in the git repo
type PluginVersion struct {
Vers semver.Version
Vstr string `json:"version"`
Tag string `json:"tag"`
Require map[string]string `json:"require"`
}
// PluginInfo contains all the needed info about a plugin
type PluginInfo struct {
Name string `json:"name"`
Desc string `json:"description"`
Site string `json:"website"`
Repo string `json:"repository"`
Versions []PluginVersion `json:"versions"`
}
// NewPluginInfo parses a JSON input into a valid PluginInfo struct
// Returns an error if there are any missing fields or any invalid fields
// There are no optional fields in a plugin info json file
func NewPluginInfo(data []byte) (*PluginInfo, error) {
var info PluginInfo
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields() // Force errors
if err := dec.Decode(&info); err != nil {
return nil, err
}
if len(info.Name) == 0 {
return nil, ErrMissingName
} else if len(info.Desc) == 0 {
return nil, ErrMissingDesc
} else if len(info.Site) == 0 {
return nil, ErrMissingSite
} else if len(info.Repo) == 0 {
return nil, ErrMissingRepo
} else if err := info.makeVersions(); err != nil {
return nil, err
}
return &info, nil
}
func (i *PluginInfo) makeVersions() error {
if len(i.Versions) == 0 {
return ErrMissingVersions
}
for j, v := range i.Versions {
sv, err := semver.Make(v.Vstr)
if err != nil {
return err
}
i.Versions[j].Vers = sv
if len(v.Tag) == 0 {
return ErrMissingTag
} else if v.Require == nil {
return ErrMissingRequire
}
}
return nil
}
// ListInstalledPlugins searches the config directory for all installed plugins
// and returns the list of plugin infos corresponding to them
func ListInstalledPlugins() ([]*Plugin, error) {
pdir := path.Join(config.ConfigDir, "plugin")
files, err := ioutil.ReadDir(pdir)
if err != nil {
return nil, err
}
var plugins []*Plugin
for _, dir := range files {
if dir.IsDir() {
files, err := ioutil.ReadDir(path.Join(pdir, dir.Name()))
if err != nil {
return nil, err
}
for _, f := range files {
if f.Name() == infojson {
dat, err := ioutil.ReadFile(path.Join(pdir, dir.Name(), infojson))
if err != nil {
return nil, err
}
info, err := NewPluginInfo(dat)
if err != nil {
return nil, err
}
versiondat, err := ioutil.ReadFile(path.Join(pdir, dir.Name(), versionfile))
if err != nil {
return nil, err
}
sv, err := semver.Make(string(bytes.TrimSpace(versiondat)))
if err != nil {
return nil, err
}
dirname := path.Join(pdir, dir.Name())
r, err := git.PlainOpen(dirname)
if err != nil {
return nil, err
}
p := &Plugin{
Info: info,
Dir: dirname,
Repo: r,
Version: sv,
}
plugins = append(plugins, p)
}
}
}
}
return plugins, nil
}