Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding cmd to create new application #3

Merged
merged 10 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api

import (
"meltred/meltcd/internal/core"
"meltred/meltcd/internal/core/application"
"net/http"

"github.com/gofiber/fiber/v2"
)

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

if err := c.BodyParser(&app); err != nil {
return &fiber.Error{
Code: fiber.StatusBadRequest,
Message: err.Error(),
}
}

if err := core.Register(&app); err != nil {
return err
}

return c.SendStatus(http.StatusAccepted)
}
Binary file added assets/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
94 changes: 94 additions & 0 deletions cmd/meltcd/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2023 - PRESENT Meltred

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package meltcd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"meltred/meltcd/internal/core/application"
"net/http"

"github.com/spf13/cobra"
)

func createNewApplication(cmd *cobra.Command, args []string) error {
var spec application.Spec

if len(args) == 0 {
info("Creating application with Specification file")
// Creating application without application name
// means using a file
file, err := cmd.Flags().GetString("file")
if err != nil {
return err
}
spec, err = application.ParseSpecFromFile(file)
if err != nil {
return err
}
} else {
// creating application with application name
// means using arguments
info("Creating application using arguments")
name := args[0]

repo, err := cmd.Flags().GetString("repo")
if err != nil {
return err
}

path, err := cmd.Flags().GetString("path")
if err != nil {
return err
}

refresh, _ := cmd.Flags().GetString("refresh")
revision, _ := cmd.Flags().GetString("revision")

spec, err = application.ParseSpecFromValue(name, repo, revision, path, refresh)
if err != nil {
return err
}
}

app := application.New(spec)
payload, err := json.Marshal(app)
if err != nil {
return err
}

res, err := http.Post(fmt.Sprintf("%s/api/application/register", getHost()), "application/json", bytes.NewReader(payload))
if err != nil {
return err
}
defer res.Body.Close()

var responseBody struct {
Code int `json:"code"`
Message string `json:"message"`
}

if res.StatusCode != 202 {
errorMsg("server not respond with 202: error %s", responseBody.Message)
return errors.New("something went wrong")
}

info("New Application created")
return nil
}
24 changes: 23 additions & 1 deletion cmd/meltcd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// NewApplication creates a new cli app
// This cli app can be used to start the api server
// as well as a client
func NewApplication() *cobra.Command {
func NewCLI() *cobra.Command {
// Log time, date, and file name
log.SetFlags(log.Ldate | log.Lshortfile)

Expand Down Expand Up @@ -54,7 +54,29 @@ func NewApplication() *cobra.Command {

serveCmd.Flags().Bool("verbose", false, "verbose is used to get extra logs/info about process")

// Application
appCmd := &cobra.Command{
Use: "app",
Short: "Work with Applications",
}

appCreateCmd := &cobra.Command{
Use: "create",
Short: "Create a new application",
Args: cobra.RangeArgs(0, 1),
RunE: createNewApplication,
}

appCreateCmd.Flags().String("repo", "", "The git repository where the service file is hosted")
appCreateCmd.Flags().String("revision", "HEAD", "The git repository revision")
appCreateCmd.Flags().String("path", "", "The path to service file")
appCreateCmd.Flags().String("refresh", "3m0s", "The refresh time for sync")
appCreateCmd.Flags().String("file", "", "Application schema file")

appCmd.AddCommand(appCreateCmd)

rootCmd.AddCommand(serveCmd)
rootCmd.AddCommand(appCmd)

return rootCmd
}
24 changes: 24 additions & 0 deletions cmd/meltcd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package meltcd

import (
"fmt"
"os"
"strings"
)

func getHost() string {
server := "http://127.0.0.1:11771"
if os.Getenv("MELTCD_SERVER") != "" {
server, _ = strings.CutSuffix(os.Getenv("MELTCD_SERVER"), "/")
}

return server
}

func info(text string, args ...any) {
fmt.Printf(text+"\n", args...)
}

func errorMsg(text string, args ...any) {
fmt.Printf(text+"\n", args...)
}
38 changes: 36 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@ go 1.21.0

require github.com/spf13/cobra v1.8.0

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.10.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.9.1 // indirect
github.com/charmbracelet/log v0.3.0
github.com/charmbracelet/log v0.3.0
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/gofiber/fiber/v2 v2.51.0
github.com/gofiber/fiber/v2 v2.51.0
github.com/google/uuid v1.4.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
Expand Down
Loading