Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 3e25c6e

Browse files
Factorize simple custom action commands
Signed-off-by: Silvin Lubecki <[email protected]>
1 parent 86ac367 commit 3e25c6e

File tree

3 files changed

+38
-59
lines changed

3 files changed

+38
-59
lines changed

internal/commands/cnab.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"strings"
99

10+
"github.com/deislabs/duffle/pkg/action"
1011
"github.com/deislabs/duffle/pkg/bundle"
1112
"github.com/deislabs/duffle/pkg/claim"
1213
"github.com/deislabs/duffle/pkg/credentials"
@@ -241,3 +242,37 @@ func socketPath(host string) string {
241242
func isDockerHostLocal(host string) bool {
242243
return host == "" || strings.HasPrefix(host, "unix://") || strings.HasPrefix(host, "npipe://")
243244
}
245+
246+
func prepareCustomAction(actionName string, dockerCli command.Cli, appname string,
247+
registryOpts registryOptions, pullOpts pullOptions, paramsOpts parametersOptions) (*action.RunCustom, *claim.Claim, error) {
248+
defer muteDockerCli(dockerCli)()
249+
250+
c, err := claim.New(actionName)
251+
if err != nil {
252+
return nil, nil, err
253+
}
254+
driverImpl, err := prepareDriver(dockerCli, bindMount{})
255+
if err != nil {
256+
return nil, nil, err
257+
}
258+
bundle, err := resolveBundle(dockerCli, appname, pullOpts.pull, registryOpts.insecureRegistries)
259+
if err != nil {
260+
return nil, nil, err
261+
}
262+
c.Bundle = bundle
263+
264+
parameters, err := mergeBundleParameters(c.Bundle,
265+
withFileParameters(paramsOpts.parametersFiles),
266+
withCommandLineParameters(paramsOpts.overrides),
267+
)
268+
if err != nil {
269+
return nil, nil, err
270+
}
271+
c.Parameters = parameters
272+
273+
a := &action.RunCustom{
274+
Action: internal.Namespace + actionName,
275+
Driver: driverImpl,
276+
}
277+
return a, c, nil
278+
}

internal/commands/inspect.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package commands
22

33
import (
4-
"github.com/deislabs/duffle/pkg/action"
5-
"github.com/deislabs/duffle/pkg/claim"
6-
"github.com/docker/app/internal"
74
"github.com/docker/cli/cli"
85
"github.com/docker/cli/cli/command"
96
"github.com/pkg/errors"
@@ -33,35 +30,9 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
3330
}
3431

3532
func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) error {
36-
defer muteDockerCli(dockerCli)()
37-
38-
c, err := claim.New("inspect")
39-
if err != nil {
40-
return err
41-
}
42-
driverImpl, err := prepareDriver(dockerCli, bindMount{})
43-
if err != nil {
44-
return err
45-
}
46-
bundle, err := resolveBundle(dockerCli, appname, opts.pull, opts.insecureRegistries)
33+
a, c, err := prepareCustomAction("inspect", dockerCli, appname, opts.registryOptions, opts.pullOptions, opts.parametersOptions)
4734
if err != nil {
4835
return err
4936
}
50-
c.Bundle = bundle
51-
52-
parameters, err := mergeBundleParameters(c.Bundle,
53-
withFileParameters(opts.parametersFiles),
54-
withCommandLineParameters(opts.overrides),
55-
)
56-
if err != nil {
57-
return err
58-
}
59-
c.Parameters = parameters
60-
61-
a := &action.RunCustom{
62-
Action: internal.Namespace + "inspect",
63-
Driver: driverImpl,
64-
}
65-
err = a.Run(c, nil, dockerCli.Out())
66-
return errors.Wrap(err, "Inspect failed")
37+
return errors.Wrap(a.Run(c, nil, dockerCli.Out()), "Inspect failed")
6738
}

internal/commands/render.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"io"
55
"os"
66

7-
"github.com/deislabs/duffle/pkg/action"
8-
"github.com/deislabs/duffle/pkg/claim"
97
"github.com/docker/app/internal"
108
"github.com/docker/cli/cli"
119
"github.com/docker/cli/cli/command"
@@ -43,37 +41,12 @@ func renderCmd(dockerCli command.Cli) *cobra.Command {
4341
}
4442

4543
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)
44+
a, c, err := prepareCustomAction("render", dockerCli, appname, opts.registryOptions, opts.pullOptions, opts.parametersOptions)
5745
if err != nil {
5846
return err
5947
}
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
7048
c.Parameters[internal.Namespace+"render-format"] = opts.formatDriver
7149

72-
a := &action.RunCustom{
73-
Action: internal.Namespace + "render",
74-
Driver: driverImpl,
75-
}
76-
7750
var writer io.Writer = dockerCli.Out()
7851
if opts.renderOutput != "-" {
7952
f, err := os.Create(opts.renderOutput)

0 commit comments

Comments
 (0)