Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pipe output of cdk diff to file (without colors)? #1638

Closed
piotrkubisa opened this issue Jan 30, 2019 · 7 comments · Fixed by #1641
Closed

How to pipe output of cdk diff to file (without colors)? #1638

piotrkubisa opened this issue Jan 30, 2019 · 7 comments · Fixed by #1641
Assignees
Labels
effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 package/tools Related to AWS CDK Tools or CLI

Comments

@piotrkubisa
Copy link

piotrkubisa commented Jan 30, 2019

I 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.

@piotrkubisa piotrkubisa changed the title How to pipe output of cdk diff to file without colors How to pipe output of cdk diff to file (without colors)? Jan 30, 2019
@sam-goodwin
Copy link
Contributor

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#L41

I've also hacked around it by piping to perl: cdk deploy | perl -pe 's/\x1b\[[^m]+m//g;')"

@debora-ito debora-ito added feature-request A feature should be added or improved. package/tools Related to AWS CDK Tools or CLI labels Jan 30, 2019
@bgdnlp bgdnlp mentioned this issue Jun 21, 2019
5 tasks
@matteo-prosperi
Copy link

I tried this today with version 0.36 of the CDK. When redirecting to a file some, but not all, the color codes are removed.
Color codes around the [+], [ ], [-] and [~] symbols are not removed.

@costleya costleya reopened this Jul 1, 2019
@costleya
Copy link
Contributor

costleya commented Jul 1, 2019

Reopened due to incomplete issue resolution.

@shivlaks shivlaks added the effort/medium Medium work item – several days of effort label Feb 6, 2020
@yjw113080
Copy link

Hi, I can't pipe the result of cdk diff irrelevantly with --no-color feature.
Any idea on this? I'm trying to pipe cdk diff result for an input for approval request in deployment pipeline.

@shivlaks shivlaks added the p2 label Aug 7, 2020
@NGL321 NGL321 assigned rix0rrr and unassigned shivlaks Jan 25, 2021
@erict-dev
Copy link

putting the answer here because this thread still comes up in many searches:

cdk diff --no-color &> diff.txt

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@alper
Copy link

alper commented Jul 4, 2022

Can we get a --porcelain option for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants