-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [ch4364] feat: add `terradoc fmt` * on main_test, build binary in tmp dir
- Loading branch information
Nathan Thiesen
authored
Jan 28, 2022
1 parent
aefce0d
commit ca83ee0
Showing
11 changed files
with
294 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package cli | ||
|
||
var Cli struct { | ||
Generate GenerateCmd `cmd:"" help:"Generate a markdown file from .tfdoc.hcl input."` | ||
Format FormatCmd `name:"fmt" cmd:"" help:"Format .tfdoc.hcl file."` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
) | ||
|
||
type FormatCmd struct { | ||
InputFile string `arg:"" help:"Input file." type:"existingfile"` | ||
Write bool `name:"write" short:"w" help:"Overwrite file with formatted version."` | ||
} | ||
|
||
func (f FormatCmd) Run() error { | ||
inSrc, err := ioutil.ReadFile(f.InputFile) | ||
if err != nil { | ||
return fmt.Errorf("reading input: %s", err) | ||
} | ||
|
||
outSrc := hclwrite.Format(inSrc) | ||
|
||
if f.Write { | ||
err = ioutil.WriteFile(f.InputFile, outSrc, 0644) | ||
} else { | ||
_, err = os.Stdout.Write(outSrc) | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("writing result: %s", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/mineiros-io/terradoc/internal/parser/hclparser" | ||
"github.com/mineiros-io/terradoc/internal/renderers/markdown" | ||
) | ||
|
||
type GenerateCmd struct { | ||
InputFile string `arg:"" required:"" help:"Input file." type:"existingfile"` | ||
OutputFile string `name:"output" short:"o" optional:"" default:"-" help:"Output file to write resulting markdown to" type:"path"` | ||
} | ||
|
||
func (g GenerateCmd) Run() error { | ||
r, rCloser, err := openInput(g.InputFile) | ||
if err != nil { | ||
return fmt.Errorf("opening input: %s\n", err) | ||
} | ||
defer rCloser() | ||
|
||
w, wCloser, err := getOutputWriter(g.OutputFile) | ||
if err != nil { | ||
return fmt.Errorf("creating writer: %s\n", err) | ||
} | ||
defer wCloser() | ||
|
||
def, err := hclparser.Parse(r, r.Name()) | ||
if err != nil { | ||
return fmt.Errorf("parsing input: %v", err) | ||
} | ||
|
||
err = markdown.Render(w, def) | ||
if err != nil { | ||
return fmt.Errorf("rendering document: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func openInput(path string) (*os.File, func(), error) { | ||
if path == "-" { | ||
return os.Stdin, noopClose, nil | ||
} | ||
|
||
f, err := os.Open(path) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
closer := func() { | ||
if err := f.Close(); err != nil { | ||
fmt.Fprintf(os.Stderr, "closing input stream: %v", err) | ||
} | ||
} | ||
|
||
return f, closer, nil | ||
} | ||
|
||
func getOutputWriter(filename string) (io.Writer, func(), error) { | ||
if filename == "-" { | ||
return os.Stdout, noopClose, nil | ||
} | ||
|
||
f, err := os.Create(filename) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
closer := func() { | ||
if err := f.Close(); err != nil { | ||
fmt.Fprintf(os.Stderr, "closing output stream: %v", err) | ||
} | ||
} | ||
|
||
return f, closer, nil | ||
} | ||
|
||
func noopClose() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/mineiros-io/terradoc/internal/parser/hclparser" | ||
"github.com/mineiros-io/terradoc/internal/renderers/markdown" | ||
"github.com/alecthomas/kong" | ||
"github.com/mineiros-io/terradoc/cmd/terradoc/cli" | ||
) | ||
|
||
func main() { | ||
outputFile := flag.String("o", "", "Output file name") | ||
|
||
flag.Parse() | ||
|
||
r, rCloser, err := openInput(flag.Args()...) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "reading input: %s\n", err) | ||
|
||
os.Exit(1) | ||
} | ||
defer rCloser() | ||
|
||
def, err := hclparser.Parse(r, r.Name()) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "parsing document: %v", err) | ||
|
||
os.Exit(1) | ||
} | ||
|
||
w, wCloser, err := getOutputWriter(*outputFile) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "creating writer: %s\n", err) | ||
|
||
os.Exit(1) | ||
} | ||
defer wCloser() | ||
|
||
err = markdown.Render(w, def) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "rendering document: %v", err) | ||
|
||
os.Exit(1) | ||
} | ||
ctx := kong.Parse(&cli.Cli) | ||
err := ctx.Run() | ||
ctx.FatalIfErrorf(err) | ||
} | ||
|
||
func getOutputWriter(filename string) (io.Writer, func(), error) { | ||
if filename == "" { | ||
return os.Stdout, noopClose, nil | ||
} | ||
|
||
f, err := os.Create(filename) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
closer := func() { | ||
if err := f.Close(); err != nil { | ||
fmt.Fprintf(os.Stderr, "closing output stream: %v", err) | ||
} | ||
} | ||
|
||
return f, closer, nil | ||
} | ||
|
||
func openInput(args ...string) (*os.File, func(), error) { | ||
switch len(args) { | ||
case 0: | ||
return os.Stdin, noopClose, nil | ||
case 1: | ||
f, err := os.Open(args[0]) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
closer := func() { | ||
if err := f.Close(); err != nil { | ||
fmt.Fprintf(os.Stderr, "closing input stream: %v", err) | ||
} | ||
} | ||
|
||
return f, closer, nil | ||
default: | ||
return nil, nil, fmt.Errorf("expects none or one input file but given %d", len(args)) | ||
} | ||
} | ||
|
||
func noopClose() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.