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

Add instance subcommand to CLI #10

Merged
merged 4 commits into from
Jan 16, 2025
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
99 changes: 99 additions & 0 deletions cli/instance_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cli

import (
"fmt"
"os"
"time"

"github.com/choria-io/fisk"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/synadia-io/connect/cli/render"
"github.com/synadia-io/connect/client"
)

func init() {
registerCommand("instance", 3, configureInstanceCommand)
}

type instanceCommand struct {
connectorId string
deploymentId string
instanceId string
instanceStatus []string
}

func configureInstanceCommand(parentCmd commandHost) {
c := &instanceCommand{}

instanceCmd := parentCmd.Command("instance", "Interact with instances").Alias("i")

listCmd := instanceCmd.Command("list", "List instances for a connector").Alias("ls").Action(c.listInstances)
listCmd.Arg("connector", "The connector to list instances for").StringVar(&c.connectorId)
listCmd.Arg("deployment", "The deployment to list instances for").StringVar(&c.deploymentId)

infoCmd := instanceCmd.Command("get", "Show details about a instance").Alias("info").Action(c.describeInstance)
infoCmd.Arg("connector", "The connector to show instance details for").Required().StringVar(&c.connectorId)
infoCmd.Arg("deployment", "The deployment to show instance details for").Required().StringVar(&c.deploymentId)
infoCmd.Arg("id", "The instance id to show details for").Required().StringVar(&c.instanceId)
}

func (c *instanceCommand) listInstances(pc *fisk.ParseContext) error {
filter := client.InstanceFilter{
ConnectorId: c.connectorId,
DeploymentId: c.deploymentId,
}

w := table.NewWriter()
w.AppendHeader(table.Row{"Id", "Deployment", "Connector", "Status", "Uptime"})

w.SetStyle(table.StyleLight)
err := controlClient().ListInstances(filter, func(instance *client.InstanceInfo, hasMore bool) error {
if instance == nil {
return nil
}

w.AppendRow(table.Row{instance.InstanceId, instance.DeploymentId, instance.ConnectorId, instance.Status, instance.Uptime})
return nil
})
if err != nil {
color.Red("Could not list instances: %s", err)
os.Exit(1)
}

fmt.Println(w.Render())
return nil
}

func (c *instanceCommand) describeInstance(pc *fisk.ParseContext) error {
instance, err := controlClient().GetInstance(c.connectorId, c.deploymentId, c.instanceId)
if err != nil {
return err
}

if instance == nil {
color.Red("Instance %s not found for deployment %s and connector %s", c.instanceId, c.deploymentId, c.connectorId)
os.Exit(1)
}

now := time.Now()
loc := now.Location()

cols := render.New("")
cols.AddRow("Id", instance.InstanceId)
cols.AddRow("DeploymentId", instance.DeploymentId)
cols.AddRow("ConnectorId", instance.ConnectorId)

cols.AddSectionTitle("Timeline")
if instance.ScheduledAt != nil {
cols.AddRow("Scheduled At", instance.ScheduledAt.In(loc))
}
if instance.StartedAt != nil {
cols.AddRow("Started At", instance.StartedAt.In(loc))
}
if instance.FinishedAt != nil {
cols.AddRow("Finished At", instance.FinishedAt.In(loc))
}

return cols.Frender(os.Stdout)
}
Loading