Skip to content
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
32 changes: 5 additions & 27 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"regexp"
"syscall"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/sestrella/iecs/client"
"github.com/sestrella/iecs/selector"
Expand Down Expand Up @@ -58,18 +57,9 @@ var execCmd = &cobra.Command{
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}

awsClient := client.NewClient(cfg)

selection, err := execSelector(
context.TODO(),
selector.NewSelectors(awsClient, *theme),
rootClusterRegex,
rootServiceRegex,
rootSelectors,
execTaskRegex,
execContainerRegex,
)
Expand All @@ -79,7 +69,7 @@ var execCmd = &cobra.Command{

err = runExec(
context.TODO(),
awsClient,
rootClient,
*selection,
execCommand,
execInteractive,
Expand All @@ -95,22 +85,10 @@ var execCmd = &cobra.Command{
func execSelector(
ctx context.Context,
selectors selector.Selectors,
clusterRegex *regexp.Regexp,
serviceRegex *regexp.Regexp,
taskRegex *regexp.Regexp,
containerRegex *regexp.Regexp,
) (*ExecSelection, error) {
cluster, err := selectors.Cluster(ctx, clusterRegex)
if err != nil {
return nil, err
}

service, err := selectors.Service(ctx, cluster, serviceRegex)
if err != nil {
return nil, err
}

task, err := selectors.Task(ctx, service, taskRegex)
task, err := selectors.Task(ctx, rootService, taskRegex)
if err != nil {
return nil, err
}
Expand All @@ -121,8 +99,8 @@ func execSelector(
}

return &ExecSelection{
cluster: cluster,
service: service,
cluster: rootCluster,
service: rootService,
task: task,
container: container,
}, nil
Expand Down
30 changes: 6 additions & 24 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sync"
"time"

"github.com/aws/aws-sdk-go-v2/config"
logsTypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/fatih/color"
Expand Down Expand Up @@ -46,22 +45,15 @@ var logsCmd = &cobra.Command{
return err
}

cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}

client := client.NewClient(cfg)

selection, err := logsSelector(context.TODO(), selector.NewSelectors(client, *theme))
selection, err := logsSelector(context.TODO(), rootSelectors)
if err != nil {
return err
}

err = runLogs(
context.TODO(),
noColors,
client,
rootClient,
*selection,
)
if err != nil {
Expand Down Expand Up @@ -174,29 +166,19 @@ func logsSelector(
ctx context.Context,
selectors selector.Selectors,
) (*LogsSelection, error) {
cluster, err := selectors.Cluster(ctx, rootClusterRegex)
if err != nil {
return nil, err
}

service, err := selectors.Service(ctx, cluster, rootServiceRegex)
if err != nil {
return nil, err
}

tasks, err := selectors.Tasks(ctx, service)
tasks, err := selectors.Tasks(ctx, rootService)
if err != nil {
return nil, err
}

containers, err := selectors.ContainerDefinitions(ctx, *service.TaskDefinition)
containers, err := selectors.ContainerDefinitions(ctx, *rootService.TaskDefinition)
if err != nil {
return nil, err
}

return &LogsSelection{
cluster: cluster,
service: service,
cluster: rootCluster,
service: rootService,
tasks: tasks,
containers: containers,
}, nil
Expand Down
55 changes: 41 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package cmd

import (
"context"
_ "embed"
"fmt"
"regexp"
"strings"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/charmbracelet/huh"
"github.com/sestrella/iecs/client"
"github.com/sestrella/iecs/selector"
"github.com/spf13/cobra"
)

var (
availableThemes string
themeStr string
theme *huh.Theme
rootCluster string
rootClusterRegex *regexp.Regexp
rootService string
rootServiceRegex *regexp.Regexp
availableThemes string
themeStr string
theme *huh.Theme
rootClient client.Client
rootSelectors selector.Selectors
rootClusterStr string
rootCluster *types.Cluster
rootServiceStr string
rootService *types.Service
)

var themes = map[string]*huh.Theme{
Expand All @@ -33,26 +40,46 @@ var rootCmd = &cobra.Command{
Short: "An interactive CLI for ECS",
Long: "Performs commons tasks on ECS, such as getting remote access or viewing logs",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}

rootClient = client.NewClient(cfg)

if selectedTheme, ok := themes[themeStr]; ok {
theme = selectedTheme
} else {
return fmt.Errorf("unsupported theme \"%s\" expecting one of: %s", themeStr, availableThemes)
}

var err error
rootSelectors = selector.NewSelectors(rootClient, *theme)

if rootCluster != "" {
rootClusterRegex, err = regexp.Compile(rootCluster)
var clusterRegex *regexp.Regexp
if rootClusterStr != "" {
clusterRegex, err = regexp.Compile(rootClusterStr)
if err != nil {
return err
}
}

if rootService != "" {
rootServiceRegex, err = regexp.Compile(rootService)
rootCluster, err = rootSelectors.Cluster(context.TODO(), clusterRegex)
if err != nil {
return err
}

var serviceRegex *regexp.Regexp
if rootServiceStr != "" {
serviceRegex, err = regexp.Compile(rootServiceStr)
if err != nil {
return err
}

}

rootService, err = rootSelectors.Service(context.TODO(), rootCluster, serviceRegex)
if err != nil {
return err
}

return nil
Expand Down Expand Up @@ -88,7 +115,7 @@ func init() {
),
)
rootCmd.PersistentFlags().
StringVar(&rootCluster, "cluster", "", "A regex pattern for filtering clusters")
StringVar(&rootClusterStr, "cluster", "", "A regex pattern for filtering clusters")
rootCmd.PersistentFlags().
StringVar(&rootService, "service", "", "A regex pattern for filtering services")
StringVar(&rootServiceStr, "service", "", "A regex pattern for filtering services")
}
29 changes: 5 additions & 24 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"time"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/sestrella/iecs/client"
"github.com/sestrella/iecs/selector"
Expand All @@ -23,23 +22,15 @@ var updateCmd = &cobra.Command{
Use: "update",
Short: "Updates a serice configuration",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return err
}

client := client.NewClient(cfg)
selectors := selector.NewSelectors(client, *theme)

selection, err := updateSelector(
context.Background(),
selectors,
rootSelectors,
)
if err != nil {
return err
}

err = runUpdate(context.Background(), *selection, client, waitTimeoutFlag)
err = runUpdate(context.Background(), *selection, rootClient, waitTimeoutFlag)
if err != nil {
return err
}
Expand All @@ -52,24 +43,14 @@ func updateSelector(
ctx context.Context,
selectors selector.Selectors,
) (*UpdateSelection, error) {
cluster, err := selectors.Cluster(ctx, rootClusterRegex)
if err != nil {
return nil, err
}

service, err := selectors.Service(ctx, cluster, rootServiceRegex)
if err != nil {
return nil, err
}

serviceConfig, err := selectors.ServiceConfig(ctx, service)
serviceConfig, err := selectors.ServiceConfig(ctx, rootService)
if err != nil {
return nil, err
}

return &UpdateSelection{
cluster: *cluster,
service: *service,
cluster: *rootCluster,
service: *rootService,
serviceConfig: *serviceConfig,
}, nil
}
Expand Down