fmt

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Example (Fmt_help)
package main

import (
	"os"

	"github.com/aws-cloudformation/rain/internal/cmd/fmt"
)

func main() {
	os.Args = []string{
		os.Args[0],
		"../../../test/templates/success.template",
	}

	fmt.Cmd.Execute()
}
Output:

Description: This template succeeds

Parameters:
  BucketName:
    Type: String

Resources:
  Bucket1:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName

Index

Examples

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:                   "fmt <filename>",
	Aliases:               []string{"format"},
	Short:                 "Format CloudFormation templates",
	Long:                  "Reads a CloudFormation template from <filename> (or stdin if no filename is supplied) and formats it",
	Args:                  cobra.MaximumNArgs(1),
	Annotations:           cmd.TemplateAnnotation,
	DisableFlagsInUseLine: true,
	Run: func(cmd *cobra.Command, args []string) {
		var r io.Reader
		var err error

		fn := "<stdin>"

		if len(args) == 0 {
			r = os.Stdin

			stat, err := os.Stdin.Stat()
			if err != nil {
				panic(ui.Errorf(err, "unable to open stdin"))
			}

			if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 {
				fmt.Println("CAKES")
				panic(cmd.Help())
			}

			writeFlag = false
		} else {
			fn = args[0]
			r, err = os.Open(args[0])
			if err != nil {
				panic(ui.Errorf(err, "unable to read '%s'", fn))
			}
		}

		input, err := ioutil.ReadAll(r)
		if err != nil {
			panic(ui.Errorf(err, "unable to read input"))
		}

		source, err := parse.String(string(input))
		if err != nil {
			panic(ui.Errorf(err, "unable to parse input"))
		}

		output := format.String(source, format.Options{
			JSON:     jsonFlag,
			Unsorted: unsortedFlag,
		})

		if verifyFlag {
			fmt.Fprint(os.Stderr, fn+": ")

			if strings.TrimSpace(string(input)) == strings.TrimSpace(output) {
				fmt.Println("formatted OK")
				return
			}

			panic("would reformat")
		}

		if err = parse.Verify(source, output); err != nil {
			panic(err)
		}

		if writeFlag {
			ioutil.WriteFile(fn, []byte(output), 0644)
		} else {
			fmt.Println(output)
		}
	},
}

Cmd is the fmt command's entrypoint

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL