Skip to content

Commit 9603938

Browse files
committed
Refactor exec command and improve code quality
- Refactor ExecFargate for KISS/DRY compliance: * Extract helper functions (trySSMParent, tryDirectExecution, etc.) * Replace boolean flags with execResult struct * Reduce ExecFargate from ~180 lines to ~20 lines * Improve error handling and readability - Fix container name handling in exec command: * Add validation when container_name flag is not provided * Ensure command is not empty after container name extraction * Provide clear error messages for invalid usage - Remove debug print statement from cmd/run.go - Fix redundant string wrapping in lib/runFargate.go: * Remove unnecessary aws.String()/aws.StringValue() wrapping * Use strings directly in log fields - Add documentation comment about public IP usage in Fargate * Note that IPv6 implementation will change this behavior - Fix linter warnings and improve code consistency
1 parent dce0f5e commit 9603938

File tree

5 files changed

+368
-350
lines changed

5 files changed

+368
-350
lines changed

cmd/exec.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,27 @@ var execCmd = &cobra.Command{
1818
Args: cobra.MinimumNArgs(1),
1919
Run: func(cmd *cobra.Command, args []string) {
2020
viper.SetDefault("run.launch_type", "FARGATE")
21-
//var containerName string
21+
var containerName string
2222
var commandArgs []string
2323
if name := viper.GetString("container_name"); name == "" {
24+
// If container_name not provided via flag, use first arg as container name
25+
if len(args) < 2 {
26+
log.Error("When --container_name is not provided, at least 2 arguments are required: <container_name> <command>")
27+
os.Exit(1)
28+
}
29+
containerName = args[0]
2430
commandArgs = args[1:]
2531
} else {
32+
containerName = name
2633
commandArgs = args
2734
}
2835

36+
// Validate that we have a command to execute
37+
if len(commandArgs) == 0 {
38+
log.Error("No command provided to execute")
39+
os.Exit(1)
40+
}
41+
2942
// Join the commandArgs to form a single command string
3043
commandString := strings.Join(commandArgs, " ")
3144

@@ -35,7 +48,7 @@ var execCmd = &cobra.Command{
3548
Command: commandString,
3649
TaskID: viper.GetString("task_id"),
3750
TaskDefinitionName: viper.GetString("task_definition"),
38-
ContainerName: viper.GetString("container_name"),
51+
ContainerName: containerName,
3952
})
4053
if err != nil {
4154
log.WithError(err).Error("Can't execute command in Fargate mode")

cmd/run.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/spf13/cobra"
88
"github.com/spf13/viper"
99
"github.com/springload/ecs-tool/lib"
10-
"fmt"
1110
)
1211

1312
var runCmd = &cobra.Command{
@@ -57,5 +56,4 @@ func init() {
5756
viper.BindPFlag("log_group", runCmd.PersistentFlags().Lookup("log_group"))
5857
viper.BindPFlag("container_name", runCmd.PersistentFlags().Lookup("container_name"))
5958
//viper.BindPFlag("task_definition", runCmd.PersistentFlags().Lookup("task_definition"))
60-
fmt.Println("Default launch_type set to:", viper.GetString("run.launch_type"))
6159
}

0 commit comments

Comments
 (0)