Skip to content

Commit

Permalink
Refactor fuzzyfinder
Browse files Browse the repository at this point in the history
  • Loading branch information
mpon committed Jun 26, 2020
1 parent 75d071d commit 9dcad19
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 50 deletions.
21 changes: 4 additions & 17 deletions internal/command/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package command
import (
"fmt"

"github.com/ktr0731/go-fuzzyfinder"
"github.com/mpon/ecswalk/internal/pkg/awsapi"
"github.com/mpon/ecswalk/internal/pkg/fuzzyfinder"
"github.com/spf13/cobra"
)

Expand All @@ -28,26 +28,13 @@ func NewCmdServices() *cobra.Command {
return nil
}

idx, err := fuzzyfinder.Find(output.Clusters,
func(i int) string {
return fmt.Sprintf("%s", *output.Clusters[i].ClusterName)
},
fuzzyfinder.WithPromptString("Select Cluster:"),
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
cluster := output.Clusters[i]
return fmt.Sprintf("%s\n\nServices: %d\nRunning Tasks: %d\nPending Tasks: %d",
*cluster.ClusterName,
*cluster.ActiveServicesCount,
*cluster.RunningTasksCount,
*cluster.PendingTasksCount)
}),
)

cluster, err := fuzzyfinder.FindCluster(output.Clusters)
if err != nil {
// Abort fuzzyfinder
return nil
}

getServicesCmdRun(*output.Clusters[idx].ClusterName)
getServicesCmdRun(*cluster.ClusterName)
return nil
},
}
Expand Down
45 changes: 12 additions & 33 deletions internal/command/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/mpon/ecswalk/internal/pkg/awsapi"
"github.com/mpon/ecswalk/internal/pkg/fuzzyfinder"
"github.com/spf13/cobra"
)

Expand All @@ -29,22 +29,12 @@ func NewCmdTasks() *cobra.Command {
return nil
}

idx, _ := fuzzyfinder.Find(output.Clusters,
func(i int) string {
return fmt.Sprintf("%s", *output.Clusters[i].ClusterName)
},
fuzzyfinder.WithPromptString("Select Cluster:"),
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
cluster := output.Clusters[i]
return fmt.Sprintf("%s\n\nServices: %d\nRunning Tasks: %d\nPending Tasks: %d",
*cluster.ClusterName,
*cluster.ActiveServicesCount,
*cluster.RunningTasksCount,
*cluster.PendingTasksCount)
}),
)
cluster, err := fuzzyfinder.FindCluster(output.Clusters)
if err != nil {
// Abort fuzzyfinder
return nil
}

cluster := output.Clusters[idx]
describeServicesOutputs, err := client.DescribeAllECSServices(*cluster.ClusterName)
if err != nil {
return err
Expand All @@ -62,24 +52,13 @@ func NewCmdTasks() *cobra.Command {
return nil
}

idx2, _ := fuzzyfinder.Find(services,
func(i int) string {
s := services[i]
return fmt.Sprintf("%s", *s.ServiceName)
},
fuzzyfinder.WithPromptString("Select Service:"),
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
s := services[i]
return fmt.Sprintf("%s\n\nTask Definition: %s\nDesired tasks: %d\nRunning tasks: %d",
*s.ServiceName,
awsapi.ShortArn(*s.TaskDefinition),
*s.DesiredCount,
*s.RunningCount,
)
}),
)
service, err := fuzzyfinder.FindService(services)
if err != nil {
// Abort fuzzyfinder
return nil
}

getTasksCmdRun(*cluster.ClusterName, *services[idx2].ServiceName)
getTasksCmdRun(*cluster.ClusterName, *service.ServiceName)
return nil
},
}
Expand Down
58 changes: 58 additions & 0 deletions internal/pkg/fuzzyfinder/fuzzyfinder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package fuzzyfinder

import (
"fmt"

"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/mpon/ecswalk/internal/pkg/awsapi"
)

// FindCluster find fuzzily ecs.Cluster
func FindCluster(clusters []ecs.Cluster) (*ecs.Cluster, error) {
idx, err := fuzzyfinder.Find(clusters,
func(i int) string {
return fmt.Sprintf("%s", *clusters[i].ClusterName)
},
fuzzyfinder.WithPromptString("Select Cluster:"),
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
cluster := clusters[i]
return fmt.Sprintf("%s\n\nServices: %d\nRunning Tasks: %d\nPending Tasks: %d",
*cluster.ClusterName,
*cluster.ActiveServicesCount,
*cluster.RunningTasksCount,
*cluster.PendingTasksCount)
}),
)

if err != nil {
return nil, err
}
return &clusters[idx], nil
}

// FindService find fuzzily ecs.Service
func FindService(services []ecs.Service) (*ecs.Service, error) {
idx, err := fuzzyfinder.Find(services,
func(i int) string {
s := services[i]
return fmt.Sprintf("%s", *s.ServiceName)
},
fuzzyfinder.WithPromptString("Select Service:"),
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
s := services[i]
return fmt.Sprintf("%s\n\nTask Definition: %s\nDesired tasks: %d\nRunning tasks: %d",
*s.ServiceName,
awsapi.ShortArn(*s.TaskDefinition),
*s.DesiredCount,
*s.RunningCount,
)
}),
)

if err != nil {
return nil, err
}

return &services[idx], nil
}

0 comments on commit 9dcad19

Please sign in to comment.