Skip to content

Commit 5a02f35

Browse files
committed
fix: project command should recognize the project from .ee
1 parent c617bb3 commit 5a02f35

File tree

2 files changed

+139
-25
lines changed

2 files changed

+139
-25
lines changed

cmd/ee/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ It supports schema validation, multiple environments, and inheritance.`,
8080
// Global Commands - basic project operations
8181
command.NewInitCommand("global"), // Project initialization
8282
command.NewApplyCommand("global"), // Apply environment variables
83-
command.NewUICommand("global"), // Terminal user interface
8483

8584
// Entity Management - local entity operations
8685
command.NewSchemaCommand("entities"), // Schema management
8786
command.NewSheetCommand("entities"), // Config sheet management
8887
command.NewProjectCommand("entities"), // Project management
8988

9089
// Remote Operations - require authentication
90+
command.NewUICommand("authenticated"), // Terminal user interface
9191
command.NewPushCommand("authenticated"), // Push to remote
9292
command.NewPullCommand("authenticated"), // Pull from remote
9393
command.NewRemoteCommand("authenticated"), // Remote configuration

internal/command/project.go

Lines changed: 138 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,51 @@ type ProjectCommand struct {
1919
reader *bufio.Reader
2020
}
2121

22+
// resolveProjectName resolves the project name from args or .ee file
23+
// If args has at least one element, uses args[0] as project name
24+
// Otherwise, tries to get project name from .ee file in current directory
25+
func (c *ProjectCommand) resolveProjectName(args []string) (string, []string, error) {
26+
if len(args) > 0 && args[0] != "" {
27+
// Project name provided as argument
28+
return args[0], args[1:], nil
29+
}
30+
31+
// Try to get project name from .ee file
32+
projectName, err := GetCurrentProject()
33+
if err != nil {
34+
return "", args, fmt.Errorf("no project specified and no .ee file found in current directory: %w", err)
35+
}
36+
37+
if projectName == "" {
38+
return "", args, fmt.Errorf("no project specified and .ee file does not contain a project")
39+
}
40+
41+
return projectName, args, nil
42+
}
43+
44+
// resolveProjectNameForEnvCommand resolves project name for env commands that expect:
45+
// - 1 arg: environment name (use .ee file for project)
46+
// - 2 args: project name + environment name
47+
func (c *ProjectCommand) resolveProjectNameForEnvCommand(args []string) (string, []string, error) {
48+
switch len(args) {
49+
case 0:
50+
return "", args, fmt.Errorf("environment name is required")
51+
case 1:
52+
// Only environment name provided, use .ee file for project
53+
projectName, err := GetCurrentProject()
54+
if err != nil {
55+
return "", args, fmt.Errorf("no project specified and no .ee file found in current directory: %w", err)
56+
}
57+
if projectName == "" {
58+
return "", args, fmt.Errorf("no project specified and .ee file does not contain a project")
59+
}
60+
return projectName, args, nil
61+
default:
62+
// 2 or more args: first is project name, rest are environment-related
63+
return args[0], args[1:], nil
64+
}
65+
}
66+
2267
// NewProjectCommand creates a new ee project command
2368
func NewProjectCommand(groupId string) *cobra.Command {
2469
pc := &ProjectCommand{
@@ -127,10 +172,15 @@ func (c *ProjectCommand) newShowCommand() *cobra.Command {
127172
Short: "Show detailed information about a project",
128173
Long: `Show detailed information about a project including its environments and configuration.
129174
175+
If no project name is provided, uses the project from the .ee file in the current directory.
176+
130177
Examples:
131-
# Show project details
178+
# Show project details (using .ee file)
179+
ee project show
180+
181+
# Show specific project
132182
ee project show my-project`,
133-
Args: cobra.ExactArgs(1),
183+
Args: cobra.MaximumNArgs(1),
134184
RunE: c.runShow,
135185
}
136186
}
@@ -141,7 +191,10 @@ func (c *ProjectCommand) runShow(cmd *cobra.Command, args []string) error {
141191
return fmt.Errorf("storage not initialized")
142192
}
143193

144-
projectName := args[0]
194+
projectName, _, err := c.resolveProjectName(args)
195+
if err != nil {
196+
return err
197+
}
145198

146199
// Load the project
147200
project, err := uuidStorage.LoadProject(projectName)
@@ -181,10 +234,15 @@ func (c *ProjectCommand) newEditCommand() *cobra.Command {
181234
The editor is determined by the $EDITOR environment variable, falling back to 'vim' if not set.
182235
The project is presented as JSON for editing, and changes are validated and applied upon saving.
183236
237+
If no project name is provided, uses the project from the .ee file in the current directory.
238+
184239
Examples:
185-
# Edit a project
240+
# Edit a project (using .ee file)
241+
ee project edit
242+
243+
# Edit specific project
186244
ee project edit my-project`,
187-
Args: cobra.ExactArgs(1),
245+
Args: cobra.MaximumNArgs(1),
188246
RunE: c.runEdit,
189247
}
190248
}
@@ -195,7 +253,10 @@ func (c *ProjectCommand) runEdit(cmd *cobra.Command, args []string) error {
195253
return fmt.Errorf("storage not initialized")
196254
}
197255

198-
projectName := args[0]
256+
projectName, _, err := c.resolveProjectName(args)
257+
if err != nil {
258+
return err
259+
}
199260

200261
// Load the project
201262
project, err := uuidStorage.LoadProject(projectName)
@@ -267,10 +328,15 @@ func (c *ProjectCommand) newDeleteCommand() *cobra.Command {
267328
268329
This operation cannot be undone.
269330
331+
If no project name is provided, uses the project from the .ee file in the current directory.
332+
270333
Examples:
271-
# Delete a project
334+
# Delete a project (using .ee file)
335+
ee project delete
336+
337+
# Delete specific project
272338
ee project delete my-project`,
273-
Args: cobra.ExactArgs(1),
339+
Args: cobra.MaximumNArgs(1),
274340
RunE: c.runDelete,
275341
}
276342
}
@@ -281,7 +347,10 @@ func (c *ProjectCommand) runDelete(cmd *cobra.Command, args []string) error {
281347
return fmt.Errorf("storage not initialized")
282348
}
283349

284-
projectName := args[0]
350+
projectName, _, err := c.resolveProjectName(args)
351+
if err != nil {
352+
return err
353+
}
285354

286355
// Confirm deletion
287356
fmt.Printf(
@@ -311,14 +380,25 @@ func (c *ProjectCommand) newEnvCommand() *cobra.Command {
311380
Short: "Manage environments for projects",
312381
Long: `Manage environments for projects.
313382
383+
When no project name is provided, uses the project from the .ee file in the current directory.
384+
314385
Examples:
315-
# Add environment to project
386+
# Add environment (using .ee file)
387+
ee project env add development
388+
389+
# Add environment to specific project
316390
ee project env add my-project development
317391
318-
# Remove environment from project
392+
# Remove environment (using .ee file)
393+
ee project env remove development
394+
395+
# Remove environment from specific project
319396
ee project env remove my-project development
320397
321-
# List environments in project
398+
# List environments (using .ee file)
399+
ee project env list
400+
401+
# List environments in specific project
322402
ee project env list my-project`,
323403
}
324404

@@ -335,17 +415,22 @@ Examples:
335415

336416
func (c *ProjectCommand) newEnvAddCommand() *cobra.Command {
337417
return &cobra.Command{
338-
Use: "add [project-name] [environment-name]",
418+
Use: "add [project-name] <environment-name>",
339419
Short: "Add an environment to a project",
340420
Long: `Add an environment to a project.
341421
342422
This will create the environment entry in the project and auto-create
343423
a corresponding config sheet using the naming convention.
344424
425+
If no project name is provided, uses the project from the .ee file in the current directory.
426+
345427
Examples:
428+
# Add development environment (using .ee file)
429+
ee project env add development
430+
346431
# Add development environment to my-api project
347432
ee project env add my-api development`,
348-
Args: cobra.ExactArgs(2),
433+
Args: cobra.RangeArgs(1, 2),
349434
RunE: c.runEnvAdd,
350435
}
351436
}
@@ -356,8 +441,12 @@ func (c *ProjectCommand) runEnvAdd(cmd *cobra.Command, args []string) error {
356441
return fmt.Errorf("storage not initialized")
357442
}
358443

359-
projectName := args[0]
360-
envName := args[1]
444+
projectName, remainingArgs, err := c.resolveProjectNameForEnvCommand(args)
445+
if err != nil {
446+
return err
447+
}
448+
449+
envName := remainingArgs[0]
361450

362451
// Load the project
363452
project, err := uuidStorage.LoadProject(projectName)
@@ -412,17 +501,22 @@ func (c *ProjectCommand) runEnvAdd(cmd *cobra.Command, args []string) error {
412501

413502
func (c *ProjectCommand) newEnvRemoveCommand() *cobra.Command {
414503
return &cobra.Command{
415-
Use: "remove [project-name] [environment-name]",
504+
Use: "remove [project-name] <environment-name>",
416505
Short: "Remove an environment from a project",
417506
Long: `Remove an environment from a project.
418507
419508
This will remove the environment from the project and optionally
420509
delete the associated config sheet.
421510
511+
If no project name is provided, uses the project from the .ee file in the current directory.
512+
422513
Examples:
514+
# Remove development environment (using .ee file)
515+
ee project env remove development
516+
423517
# Remove development environment from my-api project
424518
ee project env remove my-api development`,
425-
Args: cobra.ExactArgs(2),
519+
Args: cobra.RangeArgs(1, 2),
426520
RunE: c.runEnvRemove,
427521
}
428522
}
@@ -433,8 +527,12 @@ func (c *ProjectCommand) runEnvRemove(cmd *cobra.Command, args []string) error {
433527
return fmt.Errorf("storage not initialized")
434528
}
435529

436-
projectName := args[0]
437-
envName := args[1]
530+
projectName, remainingArgs, err := c.resolveProjectNameForEnvCommand(args)
531+
if err != nil {
532+
return err
533+
}
534+
535+
envName := remainingArgs[0]
438536

439537
// Load the project
440538
project, err := uuidStorage.LoadProject(projectName)
@@ -497,10 +595,15 @@ func (c *ProjectCommand) newEnvListCommand() *cobra.Command {
497595
Short: "List environments in a project",
498596
Long: `List environments in a project with their config sheets.
499597
598+
If no project name is provided, uses the project from the .ee file in the current directory.
599+
500600
Examples:
601+
# List environments (using .ee file)
602+
ee project env list
603+
501604
# List environments in my-api project
502605
ee project env list my-api`,
503-
Args: cobra.ExactArgs(1),
606+
Args: cobra.MaximumNArgs(1),
504607
RunE: c.runEnvList,
505608
}
506609
}
@@ -511,7 +614,10 @@ func (c *ProjectCommand) runEnvList(cmd *cobra.Command, args []string) error {
511614
return fmt.Errorf("storage not initialized")
512615
}
513616

514-
projectName := args[0]
617+
projectName, _, err := c.resolveProjectName(args)
618+
if err != nil {
619+
return err
620+
}
515621

516622
// Load the project
517623
project, err := uuidStorage.LoadProject(projectName)
@@ -613,10 +719,15 @@ func (c *ProjectCommand) newEnvFixCommand() *cobra.Command {
613719
This command updates all environment config sheets in a project to use
614720
the project's schema, removing any inline schema references.
615721
722+
If no project name is provided, uses the project from the .ee file in the current directory.
723+
616724
Examples:
725+
# Fix config sheets (using .ee file)
726+
ee project env fix
727+
617728
# Fix config sheets in my-api project
618729
ee project env fix my-api`,
619-
Args: cobra.ExactArgs(1),
730+
Args: cobra.MaximumNArgs(1),
620731
RunE: c.runEnvFix,
621732
}
622733
}
@@ -627,7 +738,10 @@ func (c *ProjectCommand) runEnvFix(cmd *cobra.Command, args []string) error {
627738
return fmt.Errorf("storage not initialized")
628739
}
629740

630-
projectName := args[0]
741+
projectName, _, err := c.resolveProjectName(args)
742+
if err != nil {
743+
return err
744+
}
631745

632746
// Load the project
633747
project, err := uuidStorage.LoadProject(projectName)

0 commit comments

Comments
 (0)