Skip to content
Open
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
28 changes: 28 additions & 0 deletions commands/create/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package create

import (
"mzm/core"

"github.com/spf13/cobra"
)

var inputFormat core.InputFormatEnum = core.FORMAT.CRUD.YAML
var Command = &cobra.Command{
Use: "create",
Short: "Create new mezmo resource",
Long: "Create new mezmo resource",
Example: core.NewExampleRenderer().
Example(
"Create a new view",
"mzm create view",
).
Render(),
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

func init() {
Command.PersistentFlags().VarP(&inputFormat, "output", "o", "The data format used to interact (edit | create) with remote resources [yaml, json]")
Command.AddCommand(viewCommand)
}
58 changes: 58 additions & 0 deletions commands/create/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package create

import (
"fmt"
"github.com/spf13/cobra"
"mzm/core"
"mzm/core/resource"
resourceView "mzm/core/resource/v1/view"
)

var viewCommand = &cobra.Command{
Use: "view",
Short: "Create new mezmo view",
Long: `
The view subcommand allows you to create a single view resource from a template.
It will open the resource in a text editor as specified by the EDITOR
Environment variable, or fallback to vi on unix platform and notepad on windows.
The default format is yaml. To edit in JSON, specifiy "-o json"
`,
Example: core.NewExampleRenderer().Render(),
RunE: func(cmd *cobra.Command, args []string) error {
resourceInterface, err := resource.Registry.GetResource("v1", "view")
if err != nil {
return fmt.Errorf("failed to get view resource: %w", err)
}

api, ok := resourceInterface.(resource.IResource[resourceView.View, resourceView.View])
if !ok {
return fmt.Errorf("unexpected resource type: %T", resourceInterface)
}

templateContent := api.GetTemplate()

// Open in editor and get the edited content
content, err := resource.FromString(
templateContent,
inputFormat,
"view",
)

if err != nil {
return err
}

template, err := resource.ParseAndValidate[resourceView.View](content)
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}

result, err := api.Create(*template)
if err != nil {
return fmt.Errorf("failed to create view: %w", err)
}

fmt.Printf("Successfully created view: %v\n", result)
return nil
},
}
21 changes: 21 additions & 0 deletions commands/delete/mod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package delete

import (
"github.com/spf13/cobra"
"mzm/core"
)

var Command = &cobra.Command{
Use: "delete",
Short: "Delete resources from a file or stdin.",
Long: "Delete resources from a file or stdin.",
Example: core.NewExampleRenderer().
Render(),
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

func init() {
Command.AddCommand(deleteViewCommand)
}
31 changes: 31 additions & 0 deletions commands/delete/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package delete

import (
"github.com/spf13/cobra"
"mzm/core"
"mzm/core/resource"
"mzm/core/resource/v1/view"
)

var deleteViewCommand = &cobra.Command{
Use: "view [flags] [view-id]",
Short: "Delete resources from a file or stdin.",
Long: "Delete resources from a file or stdin.",
Args: cobra.RangeArgs(1, 1),
ArgAliases: []string{"viewid"},
Example: core.NewExampleRenderer().Render(),
RunE: func(cmd *cobra.Command, args []string) error {

resource, err := resource.Registry.GetResource("v1", "view")
if err != nil {
return err
}

err = resource.(*view.ViewResource).Remove(args[0])

if err != nil {
return err
}
return nil
},
}
36 changes: 36 additions & 0 deletions commands/get/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package get

import (
"github.com/spf13/cobra"
"mzm/core"
)

var outputFormat core.OutputFormatEnum = core.FORMAT.OUTPUT.TABLE

var Command = &cobra.Command{
Use: "get",
Short: "Introspect various resources.",
Long: "Prints a table of the most important information about the specified resources.",
Example: core.NewExampleRenderer().
Example(
"Get all views",
"mzm get view",
).
Example(
"Get a specific view by ID",
"mzm get view <view-id>",
).
Example(
"Get account information",
"mzm get account",
).
Render(),
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

func init() {
Command.AddCommand(getViewCommand)
Command.PersistentFlags().VarP(&outputFormat, "output", "o", `output logs in specific format [json, pretty]`)
}
161 changes: 161 additions & 0 deletions commands/get/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package get

import (
"cmp"
"fmt"
"mzm/core"
"mzm/core/logging"
coreResource "mzm/core/resource"
api "mzm/core/resource/v1/view"
"os"
"slices"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
"github.com/spf13/cobra"
)

var defaultViewParams = make(map[string]string)
var getViewCommand = &cobra.Command{
Use: "view [flags] [view-id]",
Short: "Display Information about view",
Long: "Displays The most infomration about view, which are predefined sets of search filters",
Args: cobra.RangeArgs(0, 1),
ArgAliases: []string{"viewid"},
Example: core.NewExampleRenderer().
Example(
`list all views in json format`,
`mzm get view -o json`,
).
Example(
`Get a specific view by id`,
`mzm get view 3f4bca174`,
).
Example(
`Get a specific view by name`,
`mzm get view "my first view"`,
).
Render(),
RunE: func(cmd *cobra.Command, args []string) error {
var views []api.View
var viewid string = ""
var err error = nil

log, ok := cmd.Context().Value("log").(logging.Logger)

if ok {
log = log.Child("get.view")
}

if len(args) > 0 {
viewid = args[0]
}

viewRes := api.NewViewResource()

if viewid == "" {
views, err = viewRes.List(defaultViewParams)
if err != nil {
return fmt.Errorf("Unable to get views: %s", err)
}
} else {
view, err := viewRes.Get(viewid, nil)
if err != nil {
return err
}

if view == nil {
return nil
}
views = []api.View{*view}
}

switch outputFormat.String() {
case "json", "yaml":
format := core.InputFormatEnum(outputFormat.String())
if viewid != "" {
// Single view - encode just the view object
content, err := coreResource.Stringify(views[0], format)
if err != nil {
return err
}
os.Stdout.Write(content)
} else {
// Multiple views - encode the entire slice
content, err := coreResource.Stringify(views, format)
if err != nil {
return err
}
os.Stdout.Write(content)
}
case "table":
table := tablewriter.NewTable(
os.Stdout,
tablewriter.WithRenderer(
renderer.NewBlueprint(
tw.Rendition{
Borders: tw.BorderNone,
Settings: tw.Settings{
Separators: tw.Separators{
ShowHeader: tw.Off,
ShowFooter: tw.Off,
BetweenRows: tw.Off,
BetweenColumns: tw.Off,
},
Lines: tw.Lines{
ShowTop: tw.Off,
ShowBottom: tw.Off,
ShowHeaderLine: tw.Off,
ShowFooterLine: tw.Off,
},
},
},
),
),
tablewriter.WithConfig(
tablewriter.Config{
Header: tw.CellConfig{
Alignment: tw.CellAlignment{Global: tw.AlignLeft},
},
Row: tw.CellConfig{
Merging: tw.CellMerging{Mode: tw.MergeHierarchical},
},
},
),
)

table.Header("CATEGORY", "ID", "NAME", "APPS", "HOSTS", "QUERY")

slices.SortFunc(views, func(a, b api.View) int {
if a.GetCategory() == "Uncategorized" {
return -1
}
if b.GetCategory() == "Uncategorized" {
return 1
}
return cmp.Compare(a.GetCategory(), b.GetCategory())
})
for _, view := range views {
categories := "Uncategorized" // Default if it doesn't have any

if len(view.Category) > 0 {
categories = view.Category[0]
}

table.Append(
categories,
view.PK(),
view.Name,
strings.Join(view.Apps, ", "),
strings.Join(view.Hosts, ", "),
view.Query,
)
}

table.Render()
}
return err
},
}
27 changes: 27 additions & 0 deletions commands/log/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package log

import (
"github.com/spf13/cobra"
)

var format formatEnum = pretty
var Command = &cobra.Command{
Use: "log",
Short: "Stream and Search log data",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
}

func init() {
Command.PersistentFlags().VarP(&format, "output", "o", `output logs in specific format [json, pretty]`)
Command.PersistentFlags().StringArrayP("host", "H", []string{}, "Host names to filter the log stream")
Command.PersistentFlags().StringArrayP("tag", "t", []string{}, "tags to filter by")
Command.PersistentFlags().StringArrayP("app", "a", []string{}, "app names to filter by")
Command.PersistentFlags().StringArrayP("level", "l", []string{}, "log levels to filter by")
Command.AddCommand(tailCmd)
Command.AddCommand(searchCmd)
}
Loading