From 6780df9f1e25b3fcec1bf25791ae53e07ed6baaa Mon Sep 17 00:00:00 2001 From: fujiwara Date: Sat, 5 Aug 2023 00:55:29 +0900 Subject: [PATCH 1/3] add --sort option to init command To be stable a created task definition. --- diff.go | 2 +- init.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/diff.go b/diff.go index a27ebd98..dde46b11 100644 --- a/diff.go +++ b/diff.go @@ -277,7 +277,7 @@ func sortTaskDefinitionForDiff(td *TaskDefinitionInput) { }) // containerDefinitions are sorted by name sort.SliceStable(td.ContainerDefinitions, func(i, j int) bool { - return aws.ToString(td.ContainerDefinitions[i].Name) > aws.ToString(td.ContainerDefinitions[j].Name) + return aws.ToString(td.ContainerDefinitions[i].Name) < aws.ToString(td.ContainerDefinitions[j].Name) }) if td.Cpu != nil { diff --git a/init.go b/init.go index 3a011e1c..08a8149a 100644 --- a/init.go +++ b/init.go @@ -24,6 +24,7 @@ type InitOption struct { TaskDefinition string `help:"ECS task definition name:revision" required:"" xor:"FROM"` TaskDefinitionPath string `help:"path to output task definition file" default:"ecs-task-def.json"` ServiceDefinitionPath string `help:"path to output service definition file" default:"ecs-service-def.json"` + Sort bool `help:"sort elements in task definition" default:"false" negatable:""` ConfigFilePath string ForceOverwrite bool `help:"overwrite existing files" default:"false"` Jsonnet bool `help:"output files as jsonnet format" default:"false"` @@ -188,6 +189,9 @@ func (d *App) initTaskDefinition(ctx context.Context, opt InitOption, tdArn stri if err != nil { return nil, err } + if opt.Sort { + sortTaskDefinitionForDiff(td) + } if b, err := MarshalJSONForAPI(td); err != nil { return nil, fmt.Errorf("unable to marshal task definition to JSON: %w", err) } else { From 50d328d933edf3890d852e4fc906b2e91bc3fe4b Mon Sep 17 00:00:00 2001 From: fujiwara Date: Sat, 5 Aug 2023 01:01:03 +0900 Subject: [PATCH 2/3] rename sortTaskDefinitionForDiff -> sortTaskDefinition It is not used only for "diff" command. --- diff.go | 6 +++--- export_test.go | 34 +++++++++++++++++----------------- init.go | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/diff.go b/diff.go index dde46b11..ccf0f882 100644 --- a/diff.go +++ b/diff.go @@ -110,8 +110,8 @@ func diffServices(local, remote *Service, remoteArn string, localPath string, un } func diffTaskDefs(local, remote *TaskDefinitionInput, remoteArn string, localPath string, unified bool) (string, error) { - sortTaskDefinitionForDiff(local) - sortTaskDefinitionForDiff(remote) + sortTaskDefinition(local) + sortTaskDefinition(remote) newTdBytes, err := MarshalJSONForAPI(local) if err != nil { @@ -235,7 +235,7 @@ func ServiceDefinitionForDiff(sv *Service) *ServiceForDiff { } } -func sortTaskDefinitionForDiff(td *TaskDefinitionInput) { +func sortTaskDefinition(td *TaskDefinitionInput) { for i, cd := range td.ContainerDefinitions { sort.SliceStable(cd.Environment, func(i, j int) bool { return aws.ToString(cd.Environment[i].Name) < aws.ToString(cd.Environment[j].Name) diff --git a/export_test.go b/export_test.go index b936b6d2..128c1d04 100644 --- a/export_test.go +++ b/export_test.go @@ -8,23 +8,23 @@ import ( ) var ( - SortTaskDefinitionForDiff = sortTaskDefinitionForDiff - ToNumberCPU = toNumberCPU - ToNumberMemory = toNumberMemory - CalcDesiredCount = calcDesiredCount - ParseTags = parseTags - ExtractRoleName = extractRoleName - IsLongArnFormat = isLongArnFormat - ECRImageURLRegex = ecrImageURLRegex - NewLogger = newLogger - NewLogFilter = newLogFilter - NewConfigLoader = newConfigLoader - NewVerifier = newVerifier - ArnToName = arnToName - InitVerifyState = initVerifyState - VerifyResource = verifyResource - Map2str = map2str - DiffServices = diffServices + SortTaskDefinition = sortTaskDefinition + ToNumberCPU = toNumberCPU + ToNumberMemory = toNumberMemory + CalcDesiredCount = calcDesiredCount + ParseTags = parseTags + ExtractRoleName = extractRoleName + IsLongArnFormat = isLongArnFormat + ECRImageURLRegex = ecrImageURLRegex + NewLogger = newLogger + NewLogFilter = newLogFilter + NewConfigLoader = newConfigLoader + NewVerifier = newVerifier + ArnToName = arnToName + InitVerifyState = initVerifyState + VerifyResource = verifyResource + Map2str = map2str + DiffServices = diffServices ) type ModifyAutoScalingParams = modifyAutoScalingParams diff --git a/init.go b/init.go index 08a8149a..cba44b97 100644 --- a/init.go +++ b/init.go @@ -190,7 +190,7 @@ func (d *App) initTaskDefinition(ctx context.Context, opt InitOption, tdArn stri return nil, err } if opt.Sort { - sortTaskDefinitionForDiff(td) + sortTaskDefinition(td) } if b, err := MarshalJSONForAPI(td); err != nil { return nil, fmt.Errorf("unable to marshal task definition to JSON: %w", err) From e32fed96f12e6bc052130cd895ed660de416f7e4 Mon Sep 17 00:00:00 2001 From: fujiwara Date: Sat, 5 Aug 2023 01:07:56 +0900 Subject: [PATCH 3/3] fix tests --- diff_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diff_test.go b/diff_test.go index 93e21f15..9f8c9d12 100644 --- a/diff_test.go +++ b/diff_test.go @@ -137,8 +137,8 @@ var testTaskDefinition2 = &ecspresso.TaskDefinitionInput{ } func TestTaskDefinitionDiffer(t *testing.T) { - ecspresso.SortTaskDefinitionForDiff(testTaskDefinition1) - ecspresso.SortTaskDefinitionForDiff(testTaskDefinition2) + ecspresso.SortTaskDefinition(testTaskDefinition1) + ecspresso.SortTaskDefinition(testTaskDefinition2) td1, _ := ecspresso.MarshalJSONForAPI(testTaskDefinition1) td2, _ := ecspresso.MarshalJSONForAPI(testTaskDefinition2) if diff := cmp.Diff(td1, td2); diff != "" { @@ -228,7 +228,7 @@ var testServiceDefinitionHasDesiredCount = &ecspresso.Service{ } func TestDiffServices(t *testing.T) { - t.Run("when local.DesiredCount is nil, ignore diff of DesiredCount", func (t *testing.T) { + t.Run("when local.DesiredCount is nil, ignore diff of DesiredCount", func(t *testing.T) { diff, err := ecspresso.DiffServices( testServiceDefinitionNoDesiredCount, testServiceDefinitionHasDesiredCount,