How to pipe output of cdk diff to file (without colors)?
See original GitHub issueI found cdk diff
and @aws-cdk/cloudformation-diff.formatDifferences
as fabulous tools when rewriting some CFN stacks to CDK. It is really great!
Yesterday I attempted to check differences between two templates with cdk diff
but there was too many lines that has changed and it was not really tiresome to use terminal to check all of this changes, so I decided to pipe output to some text file cdk diff > out.txt
. I got an empty file, so I tried again with stderr - cdk diff 2> out.txt
and I noticed there are also saved some extra sequences for text and background colors, extract:
[37m[4m[1mResources[22m[24m[39m
[37m[33m[~][37m [36mAWS::IAM::Policy[37m Pipeline/Role/DefaultPolicy [90mPipelineRoleDefaultPolicyC7A05455[37m [39m
[37m └─ [33m[~][37m PolicyDocument[39m
[37m └─ [33m[~][37m [34m.Statement[37m:[39m
[37m └─ [35m@@ -65,54 +65,6 @@[37m[39m
[37m [90m[ ][37m ][39m
[37m [90m[ ][37m },[39m
[37m [90m[ ][37m {[39m
[37m [1m[31m[-][37m [31m "Action": [[37m[22m[39m
[37m [1m[31m[-][37m [31m "cloudformation:CreateChangeSet",[37m[22m[39m
[37m [1m[31m[-][37m [31m "cloudformation:DeleteChangeSet",[37m[22m[39m
[37m [1m[31m[-][37m [31m "cloudformation:DescribeChangeSet",[37m[22m[39m
[37m [1m[31m[-][37m [31m "cloudformation:DescribeStacks"[37m[22m[39m
[37m [1m[31m[-][37m [31m ],[37m[22m[39m
[37m [1m[31m[-][37m [31m "Condition": {[37m[22m[39m
[37m [1m[31m[-][37m [31m "StringEqualsIfExists": {[37m[22m[39m
[37m [1m[31m[-][37m [31m "cloudformation:ChangeSetName": "Deploy"[37m[22m[39m
[37m [1m[31m[-][37m [31m }[37m[22m[39m
[37m [1m[31m[-][37m [31m },[37m[22m[39m
[37m [1m[31m[-][37m [31m "Effect": "Allow",[37m[22m[39m
[37m [1m[31m[-][37m [31m "Resource": [[37m[22m[39m
[37m [1m[31m[-][37m [31m {[37m[22m[39m
[37m [1m[31m[-][37m [31m "Fn::Join": [[37m[22m[39m
[37m [1m[31m[-][37m [31m "",[37m[22m[39m
[37m [1m[31m[-][37m [31m [[37m[22m[39m
I am not a person who gives up so easily to troubleshoot that problem, so I wrote simple JS script which utilizes the @aws-cdk/cloudformation-diff
package to check it is related to formatDifferences
function or not. Answer is “yes, it is”.
const cfndiff = require('@aws-cdk/cloudformation-diff')
const fs = require('fs')
const templatesDir = 'some/outputdir'
const tplA = JSON.parse(fs.readFileSync(`${templatesDir}/${process.argv[2]}`).toString())
const tplB = JSON.parse(fs.readFileSync(`${templatesDir}/${process.argv[3]}`).toString())
cfndiff.formatDifferences(process.stdout, cfndiff.diffTemplate(tplB, tplA))
Of course, I went through I wrote some quick post-processing script to ignore those sequences but personally, I would be more satisfied if this function will allow to specify if I want or not an output in a plain-text or not.
Generally speaking, it is a good practice to check if output is piped or not, and then (unless forced with some flag, like --color
/--no-color
) do not add those colouring sequences, otherwise use them.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
putting the answer here because this thread still comes up in many searches:
cdk diff --no-color &> diff.txt
We should update the CLI to detect if a terminal is attached, like
awslint
does: https://github.com/awslabs/aws-cdk/blob/master/tools/awslint/bin/awslint.ts#L41I’ve also hacked around it by piping to perl:
cdk deploy | perl -pe 's/\x1b\[[^m]+m//g;')"