RoadSign/pkg/cmd/rdc/deploy/commands.go

99 lines
2.9 KiB
Go
Raw Normal View History

2023-12-10 06:52:00 +00:00
package deploy
import (
"fmt"
2023-12-10 10:55:13 +00:00
"io"
2023-12-10 06:52:00 +00:00
"os"
2023-12-10 09:56:17 +00:00
"strings"
2023-12-10 06:52:00 +00:00
2024-01-17 06:34:08 +00:00
jsoniter "github.com/json-iterator/go"
2024-01-25 16:00:07 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/cmd/rdc/conn"
2023-12-10 06:52:00 +00:00
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var DeployCommands = []*cli.Command{
{
Name: "deploy",
Aliases: []string{"dp"},
ArgsUsage: "<server> <site> <upstream> [path]",
Action: func(ctx *cli.Context) error {
2023-12-10 12:52:42 +00:00
if ctx.Args().Len() < 4 {
return fmt.Errorf("must have four arguments: <server> <site> <upstream> <path>")
}
if !strings.HasSuffix(ctx.Args().Get(3), ".zip") {
return fmt.Errorf("input file must be a zip file and ends with .zip")
2023-12-10 06:52:00 +00:00
}
server, ok := conn.GetConnection(ctx.Args().Get(0))
if !ok {
return fmt.Errorf("server was not found, use \"rds connect\" add one first")
} else if err := server.CheckConnectivity(); err != nil {
return fmt.Errorf("couldn't connect server: %s", err.Error())
2023-12-10 06:52:00 +00:00
}
// Send request
log.Info().Msg("Now publishing to remote server...")
url := fmt.Sprintf("/webhooks/publish/%s/%s?mimetype=%s", ctx.Args().Get(1), ctx.Args().Get(2), "application/zip")
client := fiber.Put(server.Url+url).
2023-12-10 12:52:42 +00:00
SendFile(ctx.Args().Get(3), "attachments").
2023-12-10 06:52:00 +00:00
MultipartForm(nil).
BasicAuth("RoadSign CLI", server.Credential)
2023-12-10 10:26:04 +00:00
if status, data, err := client.Bytes(); len(err) > 0 {
2023-12-10 12:52:42 +00:00
return fmt.Errorf("failed to publish to remote: %q", err)
2023-12-10 06:52:00 +00:00
} else if status != 200 {
2023-12-10 12:52:42 +00:00
return fmt.Errorf("server rejected request, status code %d, response %s", status, string(data))
2023-12-10 06:52:00 +00:00
}
log.Info().Msg("Well done! Your site is successfully published! 🎉")
2023-12-10 10:55:13 +00:00
return nil
},
},
{
Name: "sync",
ArgsUsage: "<server> <site> <configuration path>",
Action: func(ctx *cli.Context) error {
if ctx.Args().Len() < 3 {
return fmt.Errorf("must have three arguments: <server> <site> <configuration path>")
}
server, ok := conn.GetConnection(ctx.Args().Get(0))
if !ok {
return fmt.Errorf("server was not found, use \"rds connect\" add one first")
} else if err := server.CheckConnectivity(); err != nil {
return fmt.Errorf("couldn't connect server: %s", err.Error())
2023-12-10 10:55:13 +00:00
}
2024-01-25 16:00:07 +00:00
var raw []byte
2023-12-10 10:55:13 +00:00
if file, err := os.Open(ctx.Args().Get(2)); err != nil {
return err
} else {
2024-01-25 16:00:07 +00:00
raw, _ = io.ReadAll(file)
2023-12-10 10:55:13 +00:00
}
url := fmt.Sprintf("/webhooks/sync/%s", ctx.Args().Get(1))
client := fiber.Put(server.Url+url).
2023-12-16 14:57:32 +00:00
JSONEncoder(jsoniter.ConfigCompatibleWithStandardLibrary.Marshal).
JSONDecoder(jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal).
2024-01-25 16:00:07 +00:00
Body(raw).
2023-12-10 10:55:13 +00:00
BasicAuth("RoadSign CLI", server.Credential)
if status, data, err := client.Bytes(); len(err) > 0 {
return fmt.Errorf("failed to sync to remote: %q", err)
} else if status != 200 {
return fmt.Errorf("server rejected request, status code %d, response %s", status, string(data))
}
log.Info().Msg("Well done! Your site configuration is up-to-date! 🎉")
2023-12-10 06:52:00 +00:00
return nil
},
},
}