Skip to content

Commit

Permalink
feat(api,cli): remove application
Browse files Browse the repository at this point in the history
  • Loading branch information
KunalSin9h committed Nov 26, 2023
1 parent c26ac1f commit 45023e8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: Release

on:
push:
tags:
- "*"
workflow_dispatch:

permissions:
contents: write
Expand Down
19 changes: 19 additions & 0 deletions cmd/meltcd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,22 @@ func refreshApplication(_ *cobra.Command, args []string) error {
}
return nil
}

func removeApplication(_ *cobra.Command, args []string) error {
appName := args[0]

res, err := http.Post(fmt.Sprintf("%s/api/application/remove/%s", getServer(), appName), "", nil)
if err != nil {
return err
}
defer res.Body.Close()

if res.StatusCode != fiber.StatusOK {
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
return errors.New(string(data))
}
return nil
}
9 changes: 9 additions & 0 deletions cmd/meltcd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,20 @@ func NewCLI() *cobra.Command {
RunE: refreshApplication,
}

appRemoveCmd := &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Short: "Remove Application",
Args: cobra.ExactArgs(1),
RunE: removeApplication,
}

appCmd.AddCommand(appCreateCmd)
appCmd.AddCommand(appUpdateCmd)
appCmd.AddCommand(appGetCmd)
appCmd.AddCommand(appListCmd)
appCmd.AddCommand(appRefreshCmd)
appCmd.AddCommand(appRemoveCmd)

rootCmd.AddCommand(serveCmd)
rootCmd.AddCommand(appCmd)
Expand Down
40 changes: 40 additions & 0 deletions internal/core/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ limitations under the License.
package core

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/meltred/meltcd/internal/core/application"

"github.com/charmbracelet/log"
Expand Down Expand Up @@ -132,3 +136,39 @@ func loadRegistryData(d *[]byte) error {

return nil
}

func RemoveApplication(appName string) error {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
}

runningService, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
if err != nil {
return err
}

for _, svc := range runningService {
if strings.HasPrefix(svc.Spec.Name, appName) {
if err := cli.ServiceRemove(context.Background(), svc.ID); err != nil {
return err
}
}
}

removeSvcFromApps(appName)

return nil
}

func removeSvcFromApps(appName string) {
tmp := make([]*application.Application, 0)

for _, app := range Applications {
if app.Name != appName {
tmp = append(tmp, app)
}
}

Applications = tmp
}
20 changes: 15 additions & 5 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/gofiber/fiber/v2"
)

func Register(c *fiber.Ctx) error {
func register(c *fiber.Ctx) error {
var app application.Application

if err := c.BodyParser(&app); err != nil {
Expand All @@ -40,7 +40,7 @@ func Register(c *fiber.Ctx) error {
return c.SendStatus(http.StatusAccepted)
}

func Update(c *fiber.Ctx) error {
func update(c *fiber.Ctx) error {
var app application.Application

if err := c.BodyParser(&app); err != nil {
Expand All @@ -54,7 +54,7 @@ func Update(c *fiber.Ctx) error {
return c.SendStatus(http.StatusAccepted)
}

func Details(c *fiber.Ctx) error {
func details(c *fiber.Ctx) error {
appName := c.Params("app_name")
if appName == "" {
return errors.New("application name (app_name) missing in querystring")
Expand All @@ -77,7 +77,7 @@ type AppStatus struct {
Health string `json:"health"`
}

func AllApplications(c *fiber.Ctx) error {
func allApplications(c *fiber.Ctx) error {
status := core.List()

var res AppList
Expand All @@ -92,7 +92,7 @@ func AllApplications(c *fiber.Ctx) error {
return c.Status(200).JSON(res)
}

func Refresh(c *fiber.Ctx) error {
func refresh(c *fiber.Ctx) error {
appName := c.Params("app_name")

if err := core.Refresh(appName); err != nil {
Expand All @@ -101,3 +101,13 @@ func Refresh(c *fiber.Ctx) error {

return c.SendStatus(200)
}

func remove(c *fiber.Ctx) error {
appName := c.Params("app_name")

if err := core.RemoveApplication(appName); err != nil {
return err
}

return c.SendStatus(200)
}
11 changes: 6 additions & 5 deletions server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ func Serve(ln net.Listener, origins string, verboseOutput bool) error {
})

application := api.Group("application")
application.Post("/create", Register)
application.Post("/update", Update)
application.Post("/refresh/:app_name", Refresh)
application.Get("/get", AllApplications)
application.Get("/get/:app_name", Details)
application.Post("/create", register)
application.Post("/update", update)
application.Post("/refresh/:app_name", refresh)
application.Post("/remove/:app_name", remove)
application.Get("/get", allApplications)
application.Get("/get/:app_name", details)

err := core.Setup()
if err != nil {
Expand Down

0 comments on commit 45023e8

Please sign in to comment.