@@ -40,6 +40,25 @@ func extraFiles(appname string) ([]string, error) {
4040 return res , nil
4141}
4242
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+
4362func mergeCmd (dockerCli command.Cli ) * cobra.Command {
4463 cmd := & cobra.Command {
4564 Use : "merge [<app-name>] [-o output_file]" ,
@@ -53,12 +72,7 @@ func mergeCmd(dockerCli command.Cli) *cobra.Command {
5372 defer extractedApp .Cleanup ()
5473 inPlace := false
5574 if mergeOutputFile == "" {
56- if extractedApp .Source == types .AppSourceURL || extractedApp .Source == types .AppSourceImage {
57- mergeOutputFile = internal .DirNameFromAppName (extractedApp .Name )
58- } else {
59- inPlace = true
60- mergeOutputFile = extractedApp .Path + ".tmp"
61- }
75+ mergeOutputFile , inPlace = handleInPlace (extractedApp )
6276 }
6377 if inPlace {
6478 extra , err := extraFiles (extractedApp .Path )
@@ -86,12 +100,7 @@ func mergeCmd(dockerCli command.Cli) *cobra.Command {
86100 target .(io.WriteCloser ).Close ()
87101 }
88102 if inPlace {
89- if err := os .RemoveAll (extractedApp .Path ); err != nil {
90- return errors .Wrap (err , "failed to erase previous application" )
91- }
92- if err := os .Rename (mergeOutputFile , extractedApp .Path ); err != nil {
93- return errors .Wrap (err , "failed to rename new application" )
94- }
103+ return removeAndRename (mergeOutputFile , extractedApp .Path )
95104 }
96105 return nil
97106 },
0 commit comments