Skip to content

Commit db90f28

Browse files
committed
Refactor secrets, info, and logs commands for consistency and robustness
1 parent 416581c commit db90f28

File tree

14 files changed

+169
-174
lines changed

14 files changed

+169
-174
lines changed

.claude/settings.local.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"WebFetch(domain:raw.githubusercontent.com)",
5+
"WebFetch(domain:github.com)",
6+
"Bash(go doc:*)",
7+
"Bash(go build:*)",
8+
"Bash(./ws-cli template:*)",
9+
"Bash(./ws-cli:*)",
10+
"WebSearch",
11+
"mcp__ide__getDiagnostics",
12+
"Bash(go mod:*)"
13+
],
14+
"deny": [],
15+
"ask": []
16+
}
17+
}

.todo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- add a vault.yaml `version: v1`
2+
- remove WS_SECRET_* during startup
3+
- document the removal of WS_SECRET_* and WS_PASS during startup
4+
- create a WS_PASSWORD_*_FILE
5+
- create WS_PASSWORD from ws-cli secrets generate

CLAUDE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Requirements
2+
3+
1. **Separation of concerns**
4+
Keep argument/flag parsing at the edges. Commands should delegate to exported functions that contain the business logic (MVC-inspired: parsing ≠ logic).
5+
6+
2. **Command tree wiring**
7+
The root command registers its direct children (e.g., `rootCmd.AddCommand(log.LogCmd)`), and each child registers its own subcommands.
8+
9+
3. **Pragmatic structure**
10+
Avoid over-engineering: no DI frameworks and don’t create `internal/*` packages for every command. Place shared logic in importable modules so it’s reusable and testable without duplication.
11+
12+
4. **Dependency policy**
13+
Prefer native/standard library solutions over third-party packages whenever possible.
14+
15+
5. **Testing**
16+
For tests, use the `gotest.tools/v3/assert` library instead of `if/fail` conditions.
17+
18+
6. **Backwards compatibility**
19+
This is not a public library; legacy compatibility isn’t required.
20+
21+
7. **CLI UX**
22+
Add colorized output to make the CLI more user-friendly.
23+
24+
8. **Comments**
25+
Do not not add comments unless specifically instructed

cmd/info/info.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,56 @@ import (
55
"fmt"
66
"io"
77
"os"
8-
"strings"
98

109
"github.com/kloudkit/ws-cli/internals/config"
10+
internalIO "github.com/kloudkit/ws-cli/internals/io"
1111
"github.com/kloudkit/ws-cli/internals/styles"
1212
"github.com/spf13/cobra"
1313
)
1414

15-
func readJsonFile() map[string]any {
16-
var content map[string]any
17-
18-
data, _ := os.ReadFile(config.DefaultManifestPath)
19-
20-
_ = json.Unmarshal(data, &content)
21-
22-
return content
15+
type Manifest struct {
16+
Version string `json:"version"`
17+
VSCode struct {
18+
Version string `json:"version"`
19+
} `json:"vscode"`
2320
}
2421

25-
func readJson(content map[string]any, key string) string {
26-
keys := strings.Split(key, ".")
27-
var value any = content
28-
29-
for _, k := range keys {
30-
m, ok := value.(map[string]any)
31-
if !ok {
32-
return ""
33-
}
22+
func readManifest() (*Manifest, error) {
23+
if !internalIO.FileExists(config.DefaultManifestPath) {
24+
return nil, fmt.Errorf("manifest not found at %s", config.DefaultManifestPath)
25+
}
3426

35-
value = m[k]
27+
data, err := os.ReadFile(config.DefaultManifestPath)
28+
if err != nil {
29+
return nil, fmt.Errorf("failed to read manifest: %w", err)
3630
}
3731

38-
if str, ok := value.(string); ok {
39-
return str
32+
var m Manifest
33+
if err := json.Unmarshal(data, &m); err != nil {
34+
return nil, fmt.Errorf("failed to parse manifest: %w", err)
4035
}
4136

42-
return fmt.Sprintf("%v", value)
37+
return &m, nil
4338
}
4439

4540
func showVersion(writer io.Writer) {
46-
content := readJsonFile()
41+
manifest, err := readManifest()
42+
if err != nil {
43+
styles.PrintWarning(writer, fmt.Sprintf("Could not read workspace version: %v", err))
44+
fmt.Fprintf(writer, "%s\n", styles.Title().Render("Versions"))
45+
t := styles.Table().Rows(
46+
[]string{"ws-cli", Version},
47+
)
48+
fmt.Fprintln(writer, t.Render())
49+
return
50+
}
4751

4852
fmt.Fprintf(writer, "%s\n", styles.Title().Render("Versions"))
4953

5054
t := styles.Table().Rows(
51-
[]string{"workspace", readJson(content, "version")},
55+
[]string{"workspace", manifest.Version},
5256
[]string{"ws-cli", Version},
53-
[]string{"VSCode", readJson(content, "vscode.version")},
57+
[]string{"VSCode", manifest.VSCode.Version},
5458
)
5559

5660
fmt.Fprintln(writer, t.Render())

cmd/logs/logs.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package logs
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/kloudkit/ws-cli/internals/logger"
87
"github.com/kloudkit/ws-cli/internals/styles"
@@ -28,13 +27,13 @@ func execute(cmd *cobra.Command, args []string) error {
2827
{"warn", "Warning messages"},
2928
{"error", "Error messages only"},
3029
})
31-
os.Exit(1)
30+
return fmt.Errorf("invalid log level")
3231
}
3332

3433
reader, err := logger.NewReader(tail, level)
3534
if err != nil {
3635
styles.PrintError(cmd.ErrOrStderr(), fmt.Sprintf("Failed to initialize log reader: %s", err))
37-
os.Exit(1)
36+
return err
3837
}
3938

4039
if follow {
@@ -45,7 +44,7 @@ func execute(cmd *cobra.Command, args []string) error {
4544

4645
if err != nil {
4746
styles.PrintError(cmd.ErrOrStderr(), fmt.Sprintf("Error reading logs: %s", err))
48-
os.Exit(1)
47+
return err
4948
}
5049

5150
return nil

cmd/secrets/encrypt.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@ var encryptCmd = &cobra.Command{
3737
return handleOutput(cmd, cfg, encrypted, "Encrypted Value", "Secret encrypted successfully", true)
3838
},
3939
}
40-
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package generate
1+
package secrets
22

33
import "github.com/spf13/cobra"
44

5-
var GenerateCmd = &cobra.Command{
5+
var generateCmd = &cobra.Command{
66
Use: "generate",
77
Short: "Generate master keys or login password hashes",
88
}
99

1010
func init() {
11-
GenerateCmd.AddCommand(masterCmd, loginCmd)
11+
generateCmd.AddCommand(masterCmd, loginCmd)
1212
}

cmd/secrets/generate/helpers.go

Lines changed: 0 additions & 103 deletions
This file was deleted.

cmd/secrets/generate/login.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

cmd/secrets/generate/master.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)