Skip to content

Commit 0a3f2de

Browse files
authored
Merge pull request #19 from continuedev/nate/list
Nate/list
2 parents f294d11 + fa4cbe0 commit 0a3f2de

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

β€Ž.continue/rules/add-feature.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
alwaysApply: false
3+
---
4+
5+
# Adding a feature
6+
7+
When you want to add a new feature to the CLI, you should take the following steps:
8+
9+
1. Begin by making updates to the [spec](../../spec/index.md) so that the feature is described well in English
10+
2. Implement the feature to align with the spec, writing unit tests for any pure functions as you go
11+
3. Check that the tests pass and fix any problems
12+
4. If a new command was added, or a change was made that would alter the behavior of a command, make sure to update the golden file tests

β€Ž.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ bin/
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
25+
.DS_Store

β€Žcmd/list.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"rules-cli/internal/formats"
6+
"rules-cli/internal/ruleset"
7+
8+
"github.com/fatih/color"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
// listCmd represents the list command
13+
var listCmd = &cobra.Command{
14+
Use: "list",
15+
Short: "List all rules currently installed in the project",
16+
Long: `List all rules currently installed in the project, similar to 'npm list'.
17+
This command reads from the rules.json file and displays the installed rules
18+
with their versions in a tree-like format.
19+
20+
The command only reads local files and does not require network access.`,
21+
Example: ` rules list`,
22+
Args: cobra.NoArgs,
23+
RunE: runListCommand,
24+
}
25+
26+
// runListCommand implements the main logic for the list command
27+
func runListCommand(cmd *cobra.Command, args []string) error {
28+
// Get rules.json path
29+
rulesJSONPath, err := formats.GetRulesJSONPath(format)
30+
if err != nil {
31+
return fmt.Errorf("failed to get rules.json path: %w", err)
32+
}
33+
34+
// Load the ruleset
35+
rs, err := ruleset.LoadRuleSet(rulesJSONPath)
36+
if err != nil {
37+
return fmt.Errorf("No rules.json file found in current directory\nRun 'rules init' to initialize a new project")
38+
}
39+
40+
// Display the project name and version
41+
color.Cyan("%s@%s", rs.Name, rs.Version)
42+
43+
// Check if rules are empty
44+
if len(rs.Rules) == 0 {
45+
color.Yellow("(empty)")
46+
return nil
47+
}
48+
49+
// Sort rules for consistent output
50+
ruleNames := make([]string, 0, len(rs.Rules))
51+
for name := range rs.Rules {
52+
ruleNames = append(ruleNames, name)
53+
}
54+
55+
// Simple alphabetical sort
56+
for i := 0; i < len(ruleNames); i++ {
57+
for j := i + 1; j < len(ruleNames); j++ {
58+
if ruleNames[i] > ruleNames[j] {
59+
ruleNames[i], ruleNames[j] = ruleNames[j], ruleNames[i]
60+
}
61+
}
62+
}
63+
64+
// Display rules in tree format
65+
for i, name := range ruleNames {
66+
version := rs.Rules[name]
67+
68+
// Use appropriate tree characters
69+
var prefix string
70+
if i == len(ruleNames)-1 {
71+
prefix = "└── "
72+
} else {
73+
prefix = "β”œβ”€β”€ "
74+
}
75+
76+
fmt.Printf("%s%s@%s\n", prefix, name, version)
77+
}
78+
79+
return nil
80+
}
81+
82+
func init() {
83+
rootCmd.AddCommand(listCmd)
84+
}

β€Žspec/commands/list.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Rules List Command
2+
3+
## Overview
4+
5+
The `rules list` command displays all rules currently installed in the project, similar to `npm list`. It reads from the `rules.json` file and shows the installed rules with their versions in a tree-like format.
6+
7+
## Usage
8+
9+
```bash
10+
rules list [flags]
11+
```
12+
13+
## Behavior
14+
15+
1. **Check for rules.json**: Verifies that a `rules.json` file exists in the current directory
16+
2. **Parse Dependencies**: Reads and parses the rules from the `rules.json` file
17+
3. **Display Tree**: Shows the rules in a hierarchical tree format with versions
18+
19+
## Output Format
20+
21+
### Default Tree Format
22+
23+
```
24+
25+
β”œβ”€β”€ [email protected]
26+
β”œβ”€β”€ workos/[email protected]
27+
└── gh:owner/[email protected]
28+
```
29+
30+
## Error Cases
31+
32+
### No rules.json Found
33+
34+
```
35+
Error: No rules.json file found in current directory
36+
Run 'rules init' to initialize a new project
37+
```
38+
39+
**Exit Code**: 1
40+
41+
### Invalid rules.json Format
42+
43+
```
44+
Error: Invalid rules.json format
45+
Failed to parse JSON: <error details>
46+
```
47+
48+
**Exit Code**: 1
49+
50+
### Empty Rules
51+
52+
When no rules are installed:
53+
54+
```
55+
56+
(empty)
57+
```
58+
59+
## Examples
60+
61+
### Basic Usage
62+
63+
```bash
64+
$ rules list
65+
66+
β”œβ”€β”€ [email protected]
67+
β”œβ”€β”€ workos/[email protected]
68+
└── gh:owner/[email protected]
69+
```
70+
71+
## Implementation Notes
72+
73+
- Should be fast and not require network calls (only reads local files)
74+
- Should respect terminal width for formatting

β€Žspec/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ rules-cli/
2424
β”‚ β”œβ”€β”€ create.go # Create command
2525
β”‚ β”œβ”€β”€ add.go # Add command
2626
β”‚ β”œβ”€β”€ remove.go # Remove command
27+
β”‚ β”œβ”€β”€ list.go # List command
2728
β”‚ β”œβ”€β”€ render.go # Render command
2829
β”‚ β”œβ”€β”€ install.go # Install command
2930
β”‚ β”œβ”€β”€ publish.go # Publish command
@@ -82,6 +83,7 @@ This is the body of the rule. It supports Markdown syntax.
8283
- [`rules create`](commands/create.md) - Creates a new rule file in the rules directory
8384
- [`rules add`](commands/add.md) - Adds a rule to the project
8485
- [`rules remove`](commands/remove.md) - Removes a rule from the project
86+
- [`rules list`](commands/list.md) - Lists all rules currently installed in the project
8587
- [`rules render`](commands/render.md) - Renders existing rules to a specified format
8688
- [`rules install`](commands/install.md) - Synchronizes the `.rules` directory with the contents of `rules.json`
8789

β€Žtests/golden/h/help.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Available Commands:
1414
help Help about any command
1515
init Initialize a new rules directory
1616
install Synchronize rules directory with rules.json
17+
list List all rules currently installed in the project
1718
login Authenticate with the registry service
1819
logout Log out from the registry service
1920
publish Publish a rule package to the registry

0 commit comments

Comments
Β (0)