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

Recreate #67

Merged
merged 3 commits into from
Dec 28, 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: run
run: frontend
run:
swag init --output ./docs/swagger
go run main.go serve --verbose

Expand Down
8 changes: 7 additions & 1 deletion cli_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ meltcd app refresh <app-name>
meltcd app sync <app-name>
```

7. Remove an application [DONE]
7. Recreate application [TODO]

```bash
meltcd app recreate <app-name>
```

8. Remove an application [DONE]

```bash
meltcd app remove <app-name>
Expand Down
261 changes: 0 additions & 261 deletions cmd/meltcd/app.go

This file was deleted.

112 changes: 112 additions & 0 deletions cmd/meltcd/app/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
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 app

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/meltred/meltcd/internal/core/application"
api "github.com/meltred/meltcd/server/api/app"
"github.com/meltred/meltcd/util"
"github.com/spf13/cobra"
)

func CreateNewApplication(cmd *cobra.Command, args []string) error {
spec, err := getSpecFromData(cmd, args)
if err != nil {
return err
}

app := application.New(spec)

buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(app); err != nil {
return err
}

res, err := http.Post(fmt.Sprintf("%s/api/apps", util.GetServer()), "application/json", buf)
if err != nil {
return err
}
defer res.Body.Close()

var resPayload api.GlobalResponse
if err := json.NewDecoder(res.Body).Decode(&resPayload); err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return errors.New(resPayload.Message)
}

util.Info(resPayload.Message)
return nil
}

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

if len(args) == 0 {
util.Info("Application with Specification file")
// Creating application without application name
// means using a file

// if user has specified --repo then he/she must have forgotten the app name
// and come here
repo, err := cmd.Flags().GetString("repo")
if repo != "" && err == nil {
return application.Spec{}, errors.New("missing application name")
}

file, err := cmd.Flags().GetString("file")
if err != nil {
return application.Spec{}, err
}
spec, err = application.ParseSpecFromFile(file)
if err != nil {
return application.Spec{}, err
}
} else {
// creating application with application name
// menu using arguments
util.Info("Creating application using arguments")
name := args[0]

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

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

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

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

return spec, nil
}
Loading