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

Generate OT data from a Stated template using Meltini REST APIs #183

Merged
merged 5 commits into from
Aug 11, 2023
Merged
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
57 changes: 57 additions & 0 deletions cmd/melt/meltini.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package melt

import (
"bytes"
"fmt"
"io"
"net/http"
"os"

"github.com/spf13/cobra"
)

var meltMelitiniCmd = &cobra.Command{
Use: "meltini",
Short: "uses meltini APIs to generate metrics from a template",
Long: `Uses meltini APIs to generate OT data from provided stated template file.`,
TraverseChildren: true,
Hidden: true,
Run: meltMeltini,
}

func init() {
meltMelitiniCmd.Flags().String("template-file", "", "path to the stated template file")
meltMelitiniCmd.Flags().String("meltini-url", "http://localhost:3000/", "meltini REST APIs URL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we think about maybe importing MELTini as a dependency (I will have to make it available on npm)? Hitting that endpoint means we'll be starting a node server on local.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can definitely optionally install/start it once available on npm

_ = meltMelitiniCmd.MarkFlagRequired("template-file")
meltCmd.AddCommand(meltMelitiniCmd) // Assuming meltCmd is defined elsewhere
}

func meltMeltini(cmd *cobra.Command, args []string) {
templateFile, _ := cmd.Flags().GetString("template-file")
meltiniURL, _ := cmd.Flags().GetString("meltini-url")

// Read the JSON file
jsonData, err := os.ReadFile(templateFile)
if err != nil {
fmt.Println("Error reading template file:", err)
return
}

// Send a POST request with the JSON data
response, err := http.Post(meltiniURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error sending request to meltini:", err)
return
}
defer response.Body.Close()

// Read the response
respData, err := io.ReadAll(response.Body)
if err != nil {
fmt.Println("Error reading response from meltini:", err)
return
}

// Print the response
fmt.Println("Response from meltini:", string(respData))
}