-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add beginnings of plugin install command
- Loading branch information
1 parent
293e010
commit a0ab402
Showing
9 changed files
with
277 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mc | ||
server.yml | ||
*.exe | ||
*.*~ | ||
*.swp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/the-obsidian/mc/plugin" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Config struct { | ||
Plugins []*plugin.Plugin | ||
|
||
unknownKeys []string | ||
} | ||
|
||
func NewConfigFromString(data string) (*Config, error) { | ||
c := Config{} | ||
|
||
err := yaml.Unmarshal([]byte(data), &c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, plugin := range c.Plugins { | ||
err = plugin.Init() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return &c, nil | ||
} | ||
|
||
func NewConfigFromFile(path string) (*Config, error) { | ||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewConfigFromString(string(data)) | ||
} | ||
|
||
func (c *Config) InstallPlugins() error { | ||
err := os.MkdirAll("plugins", 0755) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, plugin := range c.Plugins { | ||
err = plugin.Install() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import "os" | ||
|
||
var cmdInstall = &Command{ | ||
NeedsServer: true, | ||
Usage: "install", | ||
Category: "app", | ||
Short: "install dependencies", | ||
Long: ` | ||
Install installs any required dependencies. | ||
Examples: | ||
$ mc install | ||
`, | ||
} | ||
|
||
func init() { | ||
cmdInstall.Run = runInstall | ||
} | ||
|
||
func runInstall(cmd *Command, args []string) { | ||
if len(args) != 0 { | ||
printUsageTo(os.Stderr) | ||
os.Exit(2) | ||
} | ||
|
||
config, err := NewConfigFromFile("server.yml") | ||
if err != nil { | ||
printFatal("Error reading config file: %v", err) | ||
} | ||
|
||
err = config.InstallPlugins() | ||
if err != nil { | ||
printFatal("failed to install plugins: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package plugin | ||
|
||
import ( | ||
"crypto/sha1" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/hashicorp/go-getter" | ||
"github.com/the-obsidian/mc/util" | ||
) | ||
|
||
type Plugin struct { | ||
Name string | ||
URI string | ||
Sha string | ||
Processors []Processor `yaml:"-"` | ||
|
||
originalProcessors []map[string]interface{} `yaml:"processors"` | ||
} | ||
|
||
func (p *Plugin) Init() error { | ||
for _, pr := range p.originalProcessors { | ||
processor, err := NewProcessorFromConfig(p, pr) | ||
if err != nil { | ||
return err | ||
} | ||
p.Processors = append(p.Processors, processor) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (p *Plugin) Install() error { | ||
dest := path.Join(".", "plugins", p.Name+".jar") | ||
tmpDir := path.Join(".", ".tmp", "plugins", p.Name) | ||
tmpDownload := path.Join(tmpDir, "download") | ||
hash := sha1.New() | ||
|
||
checksumValue, err := decodeChecksum(p.Sha) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if exists, err := util.FileExists(dest); err != nil { | ||
return err | ||
} else if exists { | ||
err = checksum(dest, hash, checksumValue) | ||
if err == nil { | ||
return nil | ||
} | ||
} | ||
|
||
err = os.MkdirAll(tmpDir, 0755) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = getter.GetFile(tmpDownload, p.URI) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = checksum(tmpDownload, hash, checksumValue) | ||
if err != nil { | ||
return fmt.Errorf("failed to checksum download: %v", err) | ||
} | ||
|
||
err = os.Rename(tmpDownload, dest) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type Processor interface { | ||
Process() | ||
} | ||
|
||
func NewProcessorFromConfig(p *Plugin, config map[string]interface{}) (Processor, error) { | ||
switch config["type"] { | ||
case "unzip": | ||
pr := &UnzipProcessor{ | ||
Type: "unzip", | ||
Files: config["files"].([]string), | ||
} | ||
return pr, nil | ||
default: | ||
return nil, fmt.Errorf("invalid processor type: %v", config["type"]) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
type UnzipProcessor struct { | ||
Type string | ||
Files []string | ||
} | ||
|
||
func (pr *UnzipProcessor) Process() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package plugin | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
"hash" | ||
"io" | ||
"os" | ||
) | ||
|
||
func decodeChecksum(data string) ([]byte, error) { | ||
b, err := hex.DecodeString(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid checksum: %s", err) | ||
} | ||
return b, nil | ||
} | ||
|
||
func checksum(source string, h hash.Hash, v []byte) error { | ||
f, err := os.Open(source) | ||
if err != nil { | ||
return fmt.Errorf("Failed to open file for checksum: %s", err) | ||
} | ||
defer f.Close() | ||
|
||
if _, err := io.Copy(h, f); err != nil { | ||
return fmt.Errorf("Failed to hash: %s", err) | ||
} | ||
|
||
if actual := h.Sum(nil); !bytes.Equal(actual, v) { | ||
return fmt.Errorf( | ||
"Checksums did not match.\nExpected: %s\nGot: %s", | ||
hex.EncodeToString(v), | ||
hex.EncodeToString(actual), | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package util | ||
|
||
import "os" | ||
|
||
func FileExists(path string) (bool, error) { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
} |