github.com/ijc/docker-app@v0.6.1-0.20181012090447-c7ca8bc483fb/cmd/docker-app/render.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/docker/app/internal"
     8  	"github.com/docker/app/internal/formatter"
     9  	"github.com/docker/app/internal/packager"
    10  	"github.com/docker/app/render"
    11  	"github.com/docker/app/types"
    12  	"github.com/docker/cli/cli"
    13  	"github.com/docker/cli/cli/command"
    14  	cliopts "github.com/docker/cli/opts"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var (
    19  	formatDriver       string
    20  	renderComposeFiles []string
    21  	renderSettingsFile []string
    22  	renderEnv          []string
    23  	renderOutput       string
    24  )
    25  
    26  func renderCmd(dockerCli command.Cli) *cobra.Command {
    27  	cmd := &cobra.Command{
    28  		Use:   "render <app-name> [-s key=value...] [-f settings-file...]",
    29  		Short: "Render the Compose file for the application",
    30  		Long:  `Render the Compose file for the application.`,
    31  		Args:  cli.RequiresMaxArgs(1),
    32  		RunE: func(cmd *cobra.Command, args []string) error {
    33  			app, err := packager.Extract(firstOrEmpty(args),
    34  				types.WithSettingsFiles(renderSettingsFile...),
    35  				types.WithComposeFiles(renderComposeFiles...),
    36  			)
    37  			if err != nil {
    38  				return err
    39  			}
    40  			defer app.Cleanup()
    41  			d := cliopts.ConvertKVStringsToMap(renderEnv)
    42  			rendered, err := render.Render(app, d)
    43  			if err != nil {
    44  				return err
    45  			}
    46  			res, err := formatter.Format(rendered, formatDriver)
    47  			if err != nil {
    48  				return err
    49  			}
    50  			if renderOutput == "-" {
    51  				fmt.Fprint(dockerCli.Out(), res)
    52  			} else {
    53  				f, err := os.Create(renderOutput)
    54  				if err != nil {
    55  					return err
    56  				}
    57  				fmt.Fprint(f, res)
    58  			}
    59  			return nil
    60  		},
    61  	}
    62  	if internal.Experimental == "on" {
    63  		cmd.Use += " [-c <compose-files>...]"
    64  		cmd.Long += `- External Compose files or template Compose files can be specified with the -c flag.
    65    (Repeat the flag for multiple files). These files will be merged in order with
    66    the app's own Compose file.`
    67  		cmd.Flags().StringArrayVarP(&renderComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
    68  	}
    69  	cmd.Flags().StringArrayVarP(&renderSettingsFile, "settings-files", "f", []string{}, "Override settings files")
    70  	cmd.Flags().StringArrayVarP(&renderEnv, "set", "s", []string{}, "Override settings values")
    71  	cmd.Flags().StringVarP(&renderOutput, "output", "o", "-", "Output file")
    72  	cmd.Flags().StringVar(&formatDriver, "formatter", "yaml", "Configure the output format (yaml|json)")
    73  	return cmd
    74  }