Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"archive/zip"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/tursom/mc-gateway/internal/pluginmanager"
|
|
)
|
|
|
|
func runPluginCLI(args []string) (bool, int) {
|
|
if len(args) < 2 || args[0] != "plugin" {
|
|
return false, 0
|
|
}
|
|
if len(args) < 3 {
|
|
fmt.Fprintln(os.Stderr, "usage: gateway plugin inspect|validate|compat <artifact.mcgp>")
|
|
return true, 2
|
|
}
|
|
|
|
command, packagePath := args[1], args[2]
|
|
switch command {
|
|
case "inspect":
|
|
manifest, err := readPackageManifest(packagePath)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return true, 1
|
|
}
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
encoder.SetIndent("", " ")
|
|
if err := encoder.Encode(manifest); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return true, 1
|
|
}
|
|
return true, 0
|
|
case "validate", "compat":
|
|
tmpRoot, err := os.MkdirTemp("", "mcgp-cli-*")
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return true, 1
|
|
}
|
|
defer os.RemoveAll(tmpRoot)
|
|
store := pluginmanager.NewArtifactStore(tmpRoot)
|
|
artifact, err := store.ValidateAndStore(pluginmanager.ArtifactUpload{
|
|
SourcePath: packagePath,
|
|
FileName: filepath.Base(packagePath),
|
|
Actor: "cli",
|
|
})
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return true, 1
|
|
}
|
|
fmt.Fprintf(os.Stdout, "ok plugin=%s version=%s sha256=%s api=%s go=%s %s/%s\n",
|
|
artifact.PluginID, artifact.Version, artifact.SHA256, artifact.APIVersion, artifact.GoVersion, artifact.GOOS, artifact.GOARCH)
|
|
return true, 0
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "unknown plugin command %q\n", command)
|
|
return true, 2
|
|
}
|
|
}
|
|
|
|
func readPackageManifest(packagePath string) (pluginmanager.Manifest, error) {
|
|
reader, err := zip.OpenReader(packagePath)
|
|
if err != nil {
|
|
return pluginmanager.Manifest{}, err
|
|
}
|
|
defer reader.Close()
|
|
|
|
for _, file := range reader.File {
|
|
if file.Name != "manifest.json" {
|
|
continue
|
|
}
|
|
rc, err := file.Open()
|
|
if err != nil {
|
|
return pluginmanager.Manifest{}, err
|
|
}
|
|
defer rc.Close()
|
|
data, err := io.ReadAll(io.LimitReader(rc, pluginmanager.DefaultManifestMaxBytes+1))
|
|
if err != nil {
|
|
return pluginmanager.Manifest{}, err
|
|
}
|
|
if len(data) > pluginmanager.DefaultManifestMaxBytes {
|
|
return pluginmanager.Manifest{}, fmt.Errorf("manifest.json exceeds %d bytes", pluginmanager.DefaultManifestMaxBytes)
|
|
}
|
|
var manifest pluginmanager.Manifest
|
|
if err := json.Unmarshal(data, &manifest); err != nil {
|
|
return pluginmanager.Manifest{}, err
|
|
}
|
|
return manifest, nil
|
|
}
|
|
return pluginmanager.Manifest{}, fmt.Errorf("manifest.json is required")
|
|
}
|