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

Commit fe3ee65

Browse files
author
Matthieu Nottale
committed
split,merge: Factor common code.
Signed-off-by: Matthieu Nottale <matthieu.nottale@docker.com>
1 parent 6268e99 commit fe3ee65

2 files changed

Lines changed: 23 additions & 29 deletions

File tree

cmd/docker-app/merge.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
4362
func 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
},

cmd/docker-app/split.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package main
22

33
import (
4-
"os"
5-
6-
"github.com/docker/app/internal"
74
"github.com/docker/app/internal/packager"
8-
"github.com/docker/app/types"
95
"github.com/docker/cli/cli"
10-
"github.com/pkg/errors"
116
"github.com/spf13/cobra"
127
)
138

@@ -26,23 +21,13 @@ func splitCmd() *cobra.Command {
2621
defer extractedApp.Cleanup()
2722
inPlace := false
2823
if splitOutputDir == "" {
29-
if extractedApp.Source == types.AppSourceURL || extractedApp.Source == types.AppSourceImage {
30-
splitOutputDir = internal.DirNameFromAppName(extractedApp.Name)
31-
} else {
32-
inPlace = true
33-
splitOutputDir = extractedApp.Path + ".tmp"
34-
}
24+
splitOutputDir, inPlace = handleInPlace(extractedApp)
3525
}
3626
if err := packager.Split(extractedApp, splitOutputDir); err != nil {
3727
return err
3828
}
3929
if inPlace {
40-
if err := os.RemoveAll(extractedApp.Path); err != nil {
41-
return errors.Wrap(err, "failed to erase previous application directory")
42-
}
43-
if err := os.Rename(splitOutputDir, extractedApp.Path); err != nil {
44-
return errors.Wrap(err, "failed to rename new application directory")
45-
}
30+
return removeAndRename(splitOutputDir, extractedApp.Path)
4631
}
4732
return nil
4833
},

0 commit comments

Comments
 (0)