|
1 | 1 | package commands |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
| 4 | + "io" |
5 | 5 | "os" |
6 | 6 |
|
| 7 | + "github.com/deislabs/duffle/pkg/action" |
| 8 | + "github.com/deislabs/duffle/pkg/claim" |
7 | 9 | "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 | 10 | "github.com/docker/cli/cli" |
13 | 11 | "github.com/docker/cli/cli/command" |
14 | | - cliopts "github.com/docker/cli/opts" |
| 12 | + "github.com/pkg/errors" |
15 | 13 | "github.com/spf13/cobra" |
16 | 14 | ) |
17 | 15 |
|
18 | | -var ( |
19 | | - formatDriver string |
20 | | - renderComposeFiles []string |
21 | | - renderParametersFile []string |
22 | | - renderEnv []string |
23 | | - renderOutput string |
24 | | -) |
| 16 | +type renderOptions struct { |
| 17 | + parametersOptions |
| 18 | + registryOptions |
| 19 | + pullOptions |
| 20 | + |
| 21 | + formatDriver string |
| 22 | + renderOutput string |
| 23 | +} |
25 | 24 |
|
26 | 25 | func renderCmd(dockerCli command.Cli) *cobra.Command { |
| 26 | + var opts renderOptions |
27 | 27 | cmd := &cobra.Command{ |
28 | 28 | Use: "render <app-name> [-s key=value...] [-f parameters-file...]", |
29 | 29 | Short: "Render the Compose file for the application", |
30 | 30 | Long: `Render the Compose file for the application.`, |
31 | 31 | Args: cli.RequiresMaxArgs(1), |
32 | 32 | RunE: func(cmd *cobra.Command, args []string) error { |
33 | | - app, err := packager.Extract(firstOrEmpty(args), |
34 | | - types.WithParametersFiles(renderParametersFile...), |
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, nil) |
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 |
| 33 | + return runRender(dockerCli, firstOrEmpty(args), opts) |
60 | 34 | }, |
61 | 35 | } |
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 file") |
68 | | - } |
69 | | - cmd.Flags().StringArrayVarP(&renderParametersFile, "parameters-files", "f", []string{}, "Override with parameters from files") |
70 | | - cmd.Flags().StringArrayVarP(&renderEnv, "set", "s", []string{}, "Override parameters values") |
71 | | - cmd.Flags().StringVarP(&renderOutput, "output", "o", "-", "Output file") |
72 | | - cmd.Flags().StringVar(&formatDriver, "formatter", "yaml", "Configure the output format (yaml|json)") |
| 36 | + opts.parametersOptions.addFlags(cmd.Flags()) |
| 37 | + opts.registryOptions.addFlags(cmd.Flags()) |
| 38 | + opts.pullOptions.addFlags(cmd.Flags()) |
| 39 | + cmd.Flags().StringVarP(&opts.renderOutput, "output", "o", "-", "Output file") |
| 40 | + cmd.Flags().StringVar(&opts.formatDriver, "formatter", "yaml", "Configure the output format (yaml|json)") |
| 41 | + |
73 | 42 | return cmd |
74 | 43 | } |
| 44 | + |
| 45 | +func runRender(dockerCli command.Cli, appname string, opts renderOptions) error { |
| 46 | + defer muteDockerCli(dockerCli)() |
| 47 | + |
| 48 | + c, err := claim.New("render") |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + driverImpl, err := prepareDriver(dockerCli, bindMount{}) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + bundle, err := resolveBundle(dockerCli, appname, opts.pull, opts.insecureRegistries) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + c.Bundle = bundle |
| 61 | + |
| 62 | + parameters, err := mergeBundleParameters(c.Bundle, |
| 63 | + withFileParameters(opts.parametersFiles), |
| 64 | + withCommandLineParameters(opts.overrides), |
| 65 | + ) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + c.Parameters = parameters |
| 70 | + c.Parameters[internal.Namespace+"render-format"] = opts.formatDriver |
| 71 | + |
| 72 | + a := &action.RunCustom{ |
| 73 | + Action: internal.Namespace + "render", |
| 74 | + Driver: driverImpl, |
| 75 | + } |
| 76 | + |
| 77 | + var writer io.Writer = dockerCli.Out() |
| 78 | + if opts.renderOutput != "-" { |
| 79 | + f, err := os.Create(opts.renderOutput) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + defer f.Close() |
| 84 | + writer = f |
| 85 | + } |
| 86 | + err = a.Run(c, nil, writer) |
| 87 | + return errors.Wrap(err, "Render failed") |
| 88 | +} |
0 commit comments