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

fix: Preserve new lines when buffering #44

Closed

Conversation

pmoncadaisla
Copy link

Instead of writing a println() which prints to stdout, write the new line character tot he io.Writer, this way it is preserved, otherwise, the character is lost.

This way, the code of tf-summarize can be used as a library inside your Go code rather than a executable binary and the expected output is the same.

Simple test code:

package main

import (
	"bytes"
	"os"

	"github.com/dineshba/tf-summarize/parser"
	"github.com/dineshba/tf-summarize/reader"
	"github.com/dineshba/tf-summarize/writer"
)

func main() {
	planOutputFile := "/tmp/plan"
	newReader, err := reader.CreateReader(os.Stdin, []string{planOutputFile})
	if err != nil {
		panic(err)
	}
	input, err := newReader.Read()
	newParser, err := parser.CreateParser(input, newReader.Name())
	terraformState, err := newParser.Parse()
	if err != nil {
		panic(err)
	}
	terraformState.FilterNoOpResources()
	newWriter := writer.CreateWriter(false, false, false, true, false, terraformState)

	var b bytes.Buffer
	newWriter.Write(&b)

	println(b.String())

}

Instead of writing a `println()` which prints to stdout, write the new line character tot he `io.Writer`, this way it is preserved, otherwise, the character is lost.

This way, the code of `tf-summarize` can be used as a library inside your Go code rather than a executable binary and the expected output is the same.

Simple test code:
```
package main

import (
	"bytes"
	"os"

	"github.com/dineshba/tf-summarize/parser"
	"github.com/dineshba/tf-summarize/reader"
	"github.com/dineshba/tf-summarize/writer"
)

func main() {
	planOutputFile := "/tmp/plan"
	newReader, err := reader.CreateReader(os.Stdin, []string{planOutputFile})
	if err != nil {
		panic(err)
	}
	input, err := newReader.Read()
	newParser, err := parser.CreateParser(input, newReader.Name())
	terraformState, err := newParser.Parse()
	if err != nil {
		panic(err)
	}
	terraformState.FilterNoOpResources()
	newWriter := writer.CreateWriter(false, false, false, true, false, terraformState)

	var b bytes.Buffer
	newWriter.Write(&b)

	println(b.String())

}
```
Copy link
Owner

@dineshba dineshba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR

@dineshba
Copy link
Owner

@mdb
Copy link
Contributor

mdb commented Mar 3, 2024

I believe this issue has been addressed by #62.

@dineshba
Copy link
Owner

dineshba commented Mar 4, 2024

Yes, can we close this @pmoncadaisla

@dineshba dineshba closed this Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants