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

Commit 70cde29

Browse files
authored
Merge pull request #51 from mnottale/detect-app-cwd
Extract: Detect app in cwd and subdirs.
2 parents 1f319f4 + 936c326 commit 70cde29

File tree

9 files changed

+88
-16
lines changed

9 files changed

+88
-16
lines changed

cmd/helm.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
var helmCmd = &cobra.Command{
1414
Use: "helm <app-name> [-c <compose-files>...] [-e key=value...] [-f settings-file...]",
1515
Short: "Render the Compose file for this app as an Helm package",
16-
Args: cobra.ExactArgs(1),
16+
Args: cobra.MaximumNArgs(1),
1717
Run: func(cmd *cobra.Command, args []string) {
1818
d := make(map[string]string)
1919
for _, v := range helmEnv {
@@ -28,7 +28,11 @@ var helmCmd = &cobra.Command{
2828
}
2929
d[kv[0]] = kv[1]
3030
}
31-
err := renderer.Helm(args[0], helmComposeFiles, helmSettingsFile, d)
31+
app := ""
32+
if len(args) > 0 {
33+
app = args[0]
34+
}
35+
err := renderer.Helm(app, helmComposeFiles, helmSettingsFile, d)
3236
if err != nil {
3337
fmt.Printf("%v\n", err)
3438
os.Exit(1)

cmd/inspect.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import (
1212
var inspectCmd = &cobra.Command{
1313
Use: "inspect <app-name>",
1414
Short: "Retrieve metadata for a given app package",
15-
Args: cobra.ExactArgs(1),
15+
Args: cobra.MaximumNArgs(1),
1616
Run: func(cmd *cobra.Command, args []string) {
17-
err := packager.Inspect(args[0])
17+
app := ""
18+
if len(args) > 0 {
19+
app = args[0]
20+
}
21+
err := packager.Inspect(app)
1822
if err != nil {
1923
fmt.Printf("%v\n", err)
2024
os.Exit(1)

cmd/load.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import (
1212
var loadCmd = &cobra.Command{
1313
Use: "load <repotag>",
1414
Short: "Load an app from docker",
15-
Args: cobra.ExactArgs(1),
15+
Args: cobra.MaximumNArgs(1),
1616
Run: func(cmd *cobra.Command, args []string) {
17-
err := packager.Load(args[0])
17+
app := ""
18+
if len(args) > 0 {
19+
app = args[0]
20+
}
21+
err := packager.Load(app)
1822
if err != nil {
1923
fmt.Printf("%v\n", err)
2024
os.Exit(1)

cmd/pack.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import (
1212
var packCmd = &cobra.Command{
1313
Use: "pack <app-name> [-o output_file]",
1414
Short: "Pack this app as a single file",
15-
Args: cobra.ExactArgs(1),
15+
Args: cobra.MaximumNArgs(1),
1616
Run: func(cmd *cobra.Command, args []string) {
17-
err := packager.Pack(args[0], packOutputFile)
17+
app := ""
18+
if len(args) > 0 {
19+
app = args[0]
20+
}
21+
err := packager.Pack(app, packOutputFile)
1822
if err != nil {
1923
fmt.Printf("%v\n", err)
2024
os.Exit(1)

cmd/push.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import (
1212
var pushCmd = &cobra.Command{
1313
Use: "push <app-name>",
1414
Short: "Push the application to a registry",
15-
Args: cobra.ExactArgs(1),
15+
Args: cobra.MaximumNArgs(1),
1616
Run: func(cmd *cobra.Command, args []string) {
1717
if pushTag == "" {
1818
pushTag = "latest"
1919
}
20-
err := packager.Push(args[0], pushPrefix, pushTag)
20+
app := ""
21+
if len(args) > 0 {
22+
app = args[0]
23+
}
24+
err := packager.Push(app, pushPrefix, pushTag)
2125
if err != nil {
2226
fmt.Printf("%v\n", err)
2327
os.Exit(1)

cmd/render.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Override is provided in different ways:
2121
- Individual settings values can be passed directly on the command line with the
2222
-s flag. These value takes precedence over all settings files.
2323
`,
24-
Args: cobra.ExactArgs(1),
24+
Args: cobra.MaximumNArgs(1),
2525
Run: func(cmd *cobra.Command, args []string) {
2626
d := make(map[string]string)
2727
for _, v := range renderEnv {
@@ -36,7 +36,11 @@ Override is provided in different ways:
3636
}
3737
d[kv[0]] = kv[1]
3838
}
39-
rendered, err := renderer.Render(args[0], renderComposeFiles, renderSettingsFile, d)
39+
app := ""
40+
if len(args) > 0 {
41+
app = args[0]
42+
}
43+
rendered, err := renderer.Render(app, renderComposeFiles, renderSettingsFile, d)
4044
if err != nil {
4145
fmt.Printf("%v\n", err)
4246
os.Exit(1)

cmd/save.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import (
1212
var saveCmd = &cobra.Command{
1313
Use: "save <app-name>",
1414
Short: "Save the application to docker (in preparation for push)",
15-
Args: cobra.ExactArgs(1),
15+
Args: cobra.MaximumNArgs(1),
1616
Run: func(cmd *cobra.Command, args []string) {
1717
if saveTag == "" {
1818
saveTag = "latest"
1919
}
20-
err := packager.Save(args[0], savePrefix, saveTag)
20+
app := ""
21+
if len(args) > 0 {
22+
app = args[0]
23+
}
24+
err := packager.Save(app, savePrefix, saveTag)
2125
if err != nil {
2226
fmt.Printf("%v\n", err)
2327
os.Exit(1)

cmd/unpack.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import (
1212
var unpackCmd = &cobra.Command{
1313
Use: "unpack <app-name> [-o output_dir]",
1414
Short: "Unpack the app to expose the content",
15-
Args: cobra.ExactArgs(1),
15+
Args: cobra.MaximumNArgs(1),
1616
Run: func(cmd *cobra.Command, args []string) {
17-
err := packager.Unpack(args[0], unpackOutputDir)
17+
app := ""
18+
if len(args) > 0 {
19+
app = args[0]
20+
}
21+
err := packager.Unpack(app, unpackOutputDir)
1822
if err != nil {
1923
fmt.Printf("%v\n", err)
2024
os.Exit(1)

packager/extract.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package packager
22

33
import (
44
"archive/tar"
5+
"fmt"
56
"io"
67
"io/ioutil"
78
"os"
9+
"path"
10+
"strings"
811

12+
"github.com/docker/lunchbox/constants"
913
"github.com/docker/lunchbox/utils"
1014
"github.com/pkg/errors"
1115
)
@@ -14,9 +18,45 @@ var (
1418
noop = func() {}
1519
)
1620

21+
// findApp looks for an app in CWD or subdirs
22+
func findApp() (string, error) {
23+
cwd, err := os.Getwd()
24+
if err != nil {
25+
return "", errors.Wrap(err, "cannot resolve current working directory")
26+
}
27+
if strings.HasSuffix(cwd, constants.AppExtension) {
28+
return cwd, nil
29+
}
30+
content, err := ioutil.ReadDir(cwd)
31+
if err != nil {
32+
return "", errors.Wrap(err, "failed to read current working directory")
33+
}
34+
hit := ""
35+
for _, c := range content {
36+
if strings.HasSuffix(c.Name(), constants.AppExtension) {
37+
if hit != "" {
38+
return "", fmt.Errorf("multiple apps found in current directory, specify the app on the command line")
39+
}
40+
hit = c.Name()
41+
}
42+
}
43+
if hit == "" {
44+
return "", fmt.Errorf("no app found in current directory")
45+
}
46+
return path.Join(cwd, hit), nil
47+
}
48+
1749
// Extract extracts the app content if argument is an archive, or does nothing if a dir.
1850
// It returns effective app name, and cleanup function
51+
// If appname is empty, it looks into cwd, and all subdirs for a single matching .dockerapp
1952
func Extract(appname string) (string, func(), error) {
53+
if appname == "" {
54+
var err error
55+
appname, err = findApp()
56+
if err != nil {
57+
return "", nil, err
58+
}
59+
}
2060
// try verbatim first
2161
s, err := os.Stat(appname)
2262
if err != nil {

0 commit comments

Comments
 (0)