99
1010 "github.com/docker/app/internal"
1111 "github.com/docker/app/internal/packager"
12+ "github.com/docker/app/types"
1213 "github.com/docker/cli/cli"
1314 "github.com/docker/cli/cli/command"
1415 "github.com/pkg/errors"
@@ -39,6 +40,25 @@ func extraFiles(appname string) ([]string, error) {
3940 return res , nil
4041}
4142
43+ //handleInPlace returns the operation target path and if it's in-place
44+ func handleInPlace (app * types.App ) (string , bool ) {
45+ if app .Source == types .AppSourceURL || app .Source == types .AppSourceImage {
46+ return internal .DirNameFromAppName (app .Name ), false
47+ }
48+ return app .Path + ".tmp" , true
49+ }
50+
51+ // removeAndRename removes target and rename source into target
52+ func removeAndRename (source , target string ) error {
53+ if err := os .RemoveAll (target ); err != nil {
54+ return errors .Wrap (err , "failed to erase previous application" )
55+ }
56+ if err := os .Rename (source , target ); err != nil {
57+ return errors .Wrap (err , "failed to rename new application" )
58+ }
59+ return nil
60+ }
61+
4262func mergeCmd (dockerCli command.Cli ) * cobra.Command {
4363 cmd := & cobra.Command {
4464 Use : "merge [<app-name>] [-o output_file]" ,
@@ -50,7 +70,10 @@ func mergeCmd(dockerCli command.Cli) *cobra.Command {
5070 return err
5171 }
5272 defer extractedApp .Cleanup ()
53- inPlace := mergeOutputFile == ""
73+ inPlace := false
74+ if mergeOutputFile == "" {
75+ mergeOutputFile , inPlace = handleInPlace (extractedApp )
76+ }
5477 if inPlace {
5578 extra , err := extraFiles (extractedApp .Path )
5679 if err != nil {
@@ -59,7 +82,6 @@ func mergeCmd(dockerCli command.Cli) *cobra.Command {
5982 if len (extra ) != 0 {
6083 return fmt .Errorf ("refusing to overwrite %s: extra files would be deleted: %s" , extractedApp .Path , strings .Join (extra , "," ))
6184 }
62- mergeOutputFile = extractedApp .Path + ".tmp"
6385 }
6486 var target io.Writer
6587 if mergeOutputFile == "-" {
@@ -78,12 +100,7 @@ func mergeCmd(dockerCli command.Cli) *cobra.Command {
78100 target .(io.WriteCloser ).Close ()
79101 }
80102 if inPlace {
81- if err := os .RemoveAll (extractedApp .Path ); err != nil {
82- return errors .Wrap (err , "failed to erase previous application" )
83- }
84- if err := os .Rename (mergeOutputFile , extractedApp .Path ); err != nil {
85- return errors .Wrap (err , "failed to rename new application" )
86- }
103+ return removeAndRename (mergeOutputFile , extractedApp .Path )
87104 }
88105 return nil
89106 },
0 commit comments