Skip to content

Commit a438c94

Browse files
committed
feat(cli): auto-discover .mpr file when -p is omitted
When no --project flag is given, scan the current directory for .mpr files. If exactly one is found, use it as the default and print a hint to stderr: "Using project: <file>". Zero or multiple .mpr files leave behavior unchanged (commands that require -p still error as before).
1 parent 3fc58b2 commit a438c94

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

cmd/mxcli/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"os"
10+
"strings"
1011

1112
"github.com/mendixlabs/mxcli/mdl/diaglog"
1213
"github.com/mendixlabs/mxcli/mdl/executor"
@@ -53,6 +54,25 @@ func shouldSuppressWarning() bool {
5354
return false
5455
}
5556

57+
// discoverProjectPath looks for a single .mpr file in the current directory.
58+
// Returns the filename if exactly one is found, otherwise returns "".
59+
func discoverProjectPath() string {
60+
entries, err := os.ReadDir(".")
61+
if err != nil {
62+
return ""
63+
}
64+
var mprFiles []string
65+
for _, e := range entries {
66+
if !e.IsDir() && strings.HasSuffix(e.Name(), ".mpr") {
67+
mprFiles = append(mprFiles, e.Name())
68+
}
69+
}
70+
if len(mprFiles) == 1 {
71+
return mprFiles[0]
72+
}
73+
return ""
74+
}
75+
5676
var rootCmd = &cobra.Command{
5777
Use: "mxcli",
5878
Short: "Mendix CLI - Work with Mendix projects using MDL syntax",
@@ -78,6 +98,15 @@ Examples:
7898
mxcli -p app.mpr -c "SHOW ENTITIES"
7999
`,
80100
Version: version,
101+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
102+
projectPath, _ := cmd.Flags().GetString("project")
103+
if projectPath == "" {
104+
if discovered := discoverProjectPath(); discovered != "" {
105+
_ = cmd.Flags().Set("project", discovered)
106+
fmt.Fprintf(os.Stderr, "Using project: %s\n", discovered)
107+
}
108+
}
109+
},
81110
Run: func(cmd *cobra.Command, args []string) {
82111
// Get flags
83112
commands, _ := cmd.Flags().GetString("command")

0 commit comments

Comments
 (0)