cmd

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusStopped  = "stopped"
	StatusUnloaded = "unloaded"
	StatusRunning  = "running"
)

Variables

View Source
var ComposeCmd = &cobra.Command{
	Use:   "compose",
	Short: "Manage multiple functions with compose files",
	Long: `Compose allows you to define and run multi-function applications.
A compose file lets you configure multiple functions, their dependencies,
and their runtime configurations in a single YAML file.`,
}

ComposeCmd represents the compose command.

View Source
var Container = di.NewContainer()

Container holds the dependency injection container.

View Source
var LocalRegistryPath string
View Source
var PsCmd = &cobra.Command{
	Use:   "ps",
	Short: "List running functions",
	Long: `List all running functions currently loaded in the Ignition engine.

This command connects to the running engine and displays details about all
currently loaded and running WebAssembly functions, including:
* Namespace
* Function name
* Running status

The command requires that the Ignition engine is already running. If the engine
is not running, it will display a warning and show no functions.`,
	Example: `  # List all running functions
  ignition ps

  # List in plain format (useful for scripting)
  ignition ps --plain`,
	RunE: func(c *cobra.Command, _ []string) error {

		plainFormat, _ := c.Flags().GetBool("plain")

		client, err := Container.Get("engineClient")
		if err != nil {
			if !plainFormat {
				ui.PrintError("Error getting engine client")
			}
			return fmt.Errorf("error getting engine client: %w", err)
		}
		engineClient, ok := client.(*services.EngineClient)
		if !ok {
			if !plainFormat {
				ui.PrintError("Invalid engine client type")
			}
			return errors.New("invalid engine client type")
		}

		ctx := context.Background()
		engineRunning := true
		if err := engineClient.Status(ctx); err != nil {
			engineRunning = false
			if !plainFormat {
				ui.PrintWarning("Engine is not running. No functions will be shown.")
			}
		}

		// Get all loaded functions if engine is running
		var runningFunctions []types.LoadedFunction
		if engineRunning {
			loadedFunctions, err := engineClient.ListFunctions(ctx)
			if err != nil {
				if !plainFormat {
					ui.PrintError(fmt.Sprintf("Failed to list functions: %v", err))
				}
				return fmt.Errorf("failed to list functions: %w", err)
			}
			runningFunctions = loadedFunctions
		}

		if plainFormat {
			// Define format strings with exact field widths
			const headerFormat = "%-20s\t%-20s\t%-15s\n"
			const dataFormat = "%-20s\t%-20s\t%-15s\n"

			fmt.Printf(headerFormat, "NAMESPACE", "NAME", "STATUS")

			if engineRunning && len(runningFunctions) > 0 {
				for _, fn := range runningFunctions {
					status := fn.Status
					if status == "" {
						status = StatusRunning
					} else if status == StatusStopped {
						status = StatusStopped
					} else if status == StatusUnloaded {
						status = StatusUnloaded
					}
					fmt.Printf(dataFormat, fn.Namespace, fn.Name, status)
				}
			} else {
				fmt.Println("No functions found")
			}
			return nil
		}

		table := ui.NewTable([]string{"NAMESPACE", "NAME", "STATUS"})

		if engineRunning && len(runningFunctions) > 0 {
			unloadedFunctionsExist := false
			stoppedFunctionsExist := false

			for _, fn := range runningFunctions {
				var statusStyle string

				if fn.Status == StatusUnloaded {
					statusStyle = ui.StyleStatusValue(StatusUnloaded)
					unloadedFunctionsExist = true
				} else if fn.Status == StatusStopped {
					statusStyle = ui.StyleStatusValue(StatusStopped)
					stoppedFunctionsExist = true
				} else {
					statusStyle = ui.StyleStatusValue(StatusRunning)
				}

				table.AddRow(fn.Namespace, fn.Name, statusStyle)
			}

			fmt.Println(ui.RenderTable(table))

			if unloadedFunctionsExist || stoppedFunctionsExist {
				fmt.Println()

				if unloadedFunctionsExist {
					ui.PrintInfo("Note", "Functions with '"+StatusUnloaded+"' status are available but not currently loaded in memory")
				}

				if stoppedFunctionsExist {
					ui.PrintInfo("Note", "Functions with '"+StatusStopped+"' status will not be automatically reloaded when called")
				}
			}
		} else {
			ui.PrintInfo("Status", "No functions found")
		}

		return nil
	},
}

PsCmd creates a new cobra command for listing running functions.

Functions

func Execute

func Execute()

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL