90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
|
package actions
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"regexp"
|
||
|
|
||
|
"gitea.benny.dog/torjus/ezshare/pb"
|
||
|
"github.com/urfave/cli/v2"
|
||
|
"google.golang.org/grpc"
|
||
|
)
|
||
|
|
||
|
func ParseBinary(path string) (*pb.Binary, error) {
|
||
|
baseName := filepath.Base(path)
|
||
|
|
||
|
pattern := `ezshare-(\d+\.\d+\.\d+).([a-z]+)-([a-z1-9]+)(\.exe)*`
|
||
|
re := regexp.MustCompile(pattern)
|
||
|
|
||
|
match := re.FindStringSubmatch(baseName)
|
||
|
if len(match) < 4 {
|
||
|
return nil, fmt.Errorf("invalid filename")
|
||
|
}
|
||
|
version := fmt.Sprintf("v%s", match[1])
|
||
|
operatingSystem := match[2]
|
||
|
arch := match[3]
|
||
|
|
||
|
binary := &pb.Binary{
|
||
|
Arch: arch,
|
||
|
Os: operatingSystem,
|
||
|
Version: version,
|
||
|
}
|
||
|
|
||
|
// TODO: Verify that os and arch are valid
|
||
|
|
||
|
f, err := os.Open(path)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
binary.Data, err = io.ReadAll(f)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return binary, nil
|
||
|
}
|
||
|
|
||
|
func ActionAdminUploadBinary(c *cli.Context) error {
|
||
|
if c.Args().Len() < 1 {
|
||
|
return cli.Exit("need at least 1 argument", 1)
|
||
|
}
|
||
|
cfg, err := getConfig(c)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
addr := cfg.Client.DefaultServer
|
||
|
if c.IsSet("addr") {
|
||
|
addr = c.String("addr")
|
||
|
}
|
||
|
|
||
|
clientCreds, err := cfg.Client.Creds()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
conn, err := grpc.DialContext(c.Context, addr, grpc.WithTransportCredentials(clientCreds), grpc.WithMaxMsgSize(1024*1024*100))
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer conn.Close()
|
||
|
|
||
|
client := pb.NewBinaryServiceClient(conn)
|
||
|
|
||
|
var binaries []*pb.Binary
|
||
|
for _, arg := range c.Args().Slice() {
|
||
|
binary, err := ParseBinary(arg)
|
||
|
if err != nil {
|
||
|
return cli.Exit(fmt.Sprintf("error reading binary: %s", err), 1)
|
||
|
}
|
||
|
binaries = append(binaries, binary)
|
||
|
}
|
||
|
if _, err := client.UploadBinaries(c.Context, &pb.UploadBinariesRequest{Binaries: binaries}); err != nil {
|
||
|
return cli.Exit(fmt.Sprintf("error upload binaries: %s", err), 1)
|
||
|
}
|
||
|
return nil
|
||
|
}
|