Skip to content

Commit 299952c

Browse files
committed
refactor(cli): Decouple Go routines to internal packages for Phase 1 Migration
1 parent d06b855 commit 299952c

6 files changed

Lines changed: 233 additions & 184 deletions

File tree

server/cli/qxctl/cmd/infra.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package cmd
22

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

76
"github.com/spf13/cobra"
87
"github.com/spf13/viper"
9-
"github.com/zalando/go-keyring"
8+
"github.com/QuanuX/qxctl/pkg/infra"
109
)
1110

1211
var infraCmd = &cobra.Command{
@@ -103,17 +102,10 @@ var infraSetTokenCmd = &cobra.Command{
103102
Use: "set-token [token]",
104103
Short: "Securely saves the DigitalOcean API Token to the native OS Keyring (Zero-Disk storage)",
105104
Args: cobra.ExactArgs(1),
106-
Run: func(cmd *cobra.Command, args []string) {
105+
RunE: func(cmd *cobra.Command, args []string) error {
107106
token := args[0]
108-
service := "quanux_terraform"
109-
user := "do_token"
110-
111-
err := keyring.Set(service, user, token)
112-
if err != nil {
113-
log.Fatalf("FATAL: Failed to inject token into the keyring: %v", err)
114-
}
115-
fmt.Println("SUCCESS: DigitalOcean token securely locked into the OS Keychain.")
116-
fmt.Printf("The token will perfectly synchronize with Viper Target: %s\n", viper.GetString("infra.infra.status.target"))
107+
target := viper.GetString("infra.infra.status.target")
108+
return infra.SetToken(token, target)
117109
},
118110
}
119111

server/cli/qxctl/cmd/probe.go

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"net"
6-
"sync"
7-
"time"
8-
9-
"github.com/charmbracelet/lipgloss"
104
"github.com/spf13/cobra"
115
"github.com/spf13/viper"
12-
"github.com/zalando/go-keyring"
13-
)
14-
15-
var (
16-
probeHeaderStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#00FFFF")).MarginBottom(1)
17-
probeOkStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#00FF00"))
18-
probeFailStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#FF0000"))
19-
probeLabelStyle = lipgloss.NewStyle().Width(35)
6+
"github.com/QuanuX/qxctl/pkg/probe"
207
)
218

229
var probeCmd = &cobra.Command{
@@ -30,76 +17,10 @@ var probeCmd = &cobra.Command{
3017
var probeProbeCmd = &cobra.Command{
3118
Use: "probe",
3219
Short: "Neural Tap: The cluster diagnostic stethoscope and surgical kit",
33-
Run: func(cmd *cobra.Command, args []string) {
34-
fmt.Println(probeHeaderStyle.Render("Initializing QuanuX High-Frequency Health Probe (Concurrency: 6...)\n"))
35-
36-
endpoints := map[string]string{
37-
"NATS JetStream Edge Core": "1.1.1.1:53",
38-
"QuanuX Telemetry Router (GCP)": "google.com:80",
39-
"Cloudflare Dual-Thread IPC": "8.8.8.8:53",
40-
"Local DuckDB Ingestion Loop": "127.0.0.1:4222",
41-
"Cython CGO Interlock Supervisor": "127.0.0.1:8080",
42-
}
43-
44-
var wg sync.WaitGroup
45-
var mu sync.Mutex
46-
results := make([]string, 0, len(endpoints)+1)
47-
48-
start := time.Now()
49-
50-
for label, addr := range endpoints {
51-
wg.Add(1)
52-
go func(l, a string) {
53-
defer wg.Done()
54-
timeout := time.Duration(viper.GetInt("probe.probe.probe.timeout")) * time.Millisecond
55-
conn, err := net.DialTimeout("tcp", a, timeout)
56-
57-
status := probeFailStyle.Render("[X] OFFLINE ")
58-
if err == nil {
59-
status = probeOkStyle.Render("[✔] ONLINE ")
60-
conn.Close()
61-
}
62-
63-
res := fmt.Sprintf("%s %s %s", status, probeLabelStyle.Render(l), lipgloss.NewStyle().Foreground(lipgloss.Color("#666")).Render("("+a+")"))
64-
65-
mu.Lock()
66-
results = append(results, res)
67-
mu.Unlock()
68-
}(label, addr)
69-
}
70-
71-
wg.Add(1)
72-
go func() {
73-
defer wg.Done()
74-
75-
// Natively Ping the Hardware Apple Keychain via zalando/go-keyring
76-
_, err := keyring.Get("QuanuX_Probe", "health_check_ping")
77-
78-
status := probeFailStyle.Render("[X] OFFLINE ")
79-
// ErrNotFound is the correct behavior if the keychain is successfully unlocked and readable!
80-
if err == nil || err == keyring.ErrNotFound {
81-
status = probeOkStyle.Render("[✔] ONLINE ")
82-
}
83-
84-
res := fmt.Sprintf("%s %s %s", status, probeLabelStyle.Render("Hardware OS Keyring Access"), lipgloss.NewStyle().Foreground(lipgloss.Color("#666")).Render("(Native Apple Keychain API)"))
85-
86-
mu.Lock()
87-
results = append(results, res)
88-
mu.Unlock()
89-
}()
90-
91-
wg.Wait()
92-
elapsed := time.Since(start)
93-
94-
for _, r := range results {
95-
fmt.Println(r)
96-
}
97-
98-
fmt.Printf("\nDiagnostic sweep completed natively in %s.\n", elapsed)
99-
100-
if viper.GetBool("probe.probe.probe.fix") {
101-
fmt.Println(probeHeaderStyle.Render("\nAuto-Suture Protocol [--fix] engaged. Attempting localized repairs via cluster rollout..."))
102-
}
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
timeout := viper.GetInt("probe.probe.probe.timeout")
22+
isFix := viper.GetBool("probe.probe.probe.fix")
23+
return probe.ExecuteDiagnostics(cmd.Context(), timeout, isFix)
10324
},
10425
}
10526

server/cli/qxctl/cmd/skills.go

Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"io/ioutil"
6-
"path/filepath"
7-
"strings"
8-
"os"
9-
10-
"github.com/charmbracelet/lipgloss"
114
"github.com/spf13/cobra"
125
"github.com/spf13/viper"
13-
)
14-
15-
var (
16-
skillHeaderStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#00FF00")).MarginBottom(1)
17-
skillNameStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#00BFFF")).Bold(true)
18-
skillDescStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#AAAAAA")).PaddingLeft(4)
6+
"github.com/QuanuX/qxctl/pkg/skills"
197
)
208

219
var skillsCmd = &cobra.Command{
2210
Use: "skills",
23-
Short: "Manage and discover Agent Skills",
11+
Short: "QuanuX Agent Skills Registry",
2412
Run: func(cmd *cobra.Command, args []string) {
2513
cmd.Help()
2614
},
@@ -29,88 +17,20 @@ var skillsCmd = &cobra.Command{
2917
var skillsListCmd = &cobra.Command{
3018
Use: "list",
3119
Short: "List all available agent skills",
32-
Run: func(cmd *cobra.Command, args []string) {
20+
RunE: func(cmd *cobra.Command, args []string) error {
3321
baseDir := viper.GetString("skills.skills.list.dir")
34-
35-
fmt.Println(skillHeaderStyle.Render(fmt.Sprintf("QuanuX Global Skills Registry (Path: %s):", baseDir)))
36-
37-
found := 0
38-
39-
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
40-
if err != nil {
41-
return nil
42-
}
43-
if info.IsDir() && (info.Name() == ".git" || info.Name() == "venv" || info.Name() == "node_modules" || info.Name() == "__pycache__") {
44-
return filepath.SkipDir
45-
}
46-
47-
nameLower := strings.ToLower(info.Name())
48-
if !info.IsDir() && (nameLower == "skill.md" || nameLower == "skills.md") {
49-
skillName := filepath.Base(filepath.Dir(path))
50-
51-
fmt.Println(skillNameStyle.Render("► " + strings.ReplaceAll(strings.Title(skillName), "_", " ")))
52-
53-
content, _ := ioutil.ReadFile(path)
54-
lines := strings.Split(string(content), "\n")
55-
desc := "No description provided."
56-
for _, line := range lines {
57-
if strings.HasPrefix(line, "description: ") {
58-
desc = strings.TrimPrefix(line, "description: ")
59-
break
60-
}
61-
}
62-
63-
fmt.Println(skillDescStyle.Render(desc) + "\n")
64-
found++
65-
}
66-
return nil
67-
})
68-
69-
if err != nil {
70-
fmt.Printf("Error walking skills directory: %v\n", err)
71-
return
72-
}
73-
74-
if found == 0 {
75-
fmt.Println("No active skills mapped in the directory.")
76-
} else {
77-
fmt.Println(skillHeaderStyle.Render(fmt.Sprintf("%d Active Skills Loaded.", found)))
78-
}
22+
return skills.ListSkills(baseDir)
7923
},
8024
}
8125

8226
var skillsReadCmd = &cobra.Command{
8327
Use: "read [skill_name]",
84-
Short: "Read the content of a specific skill",
28+
Short: "Read the SKILL.md for a specific skill",
8529
Args: cobra.ExactArgs(1),
86-
Run: func(cmd *cobra.Command, args []string) {
30+
RunE: func(cmd *cobra.Command, args []string) error {
8731
skillName := args[0]
8832
baseDir := viper.GetString("skills.skills.read.dir")
89-
90-
var foundPath string
91-
_ = filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
92-
if err != nil {
93-
return nil
94-
}
95-
if info.IsDir() && (info.Name() == ".git" || info.Name() == "venv" || info.Name() == "node_modules" || info.Name() == "__pycache__") {
96-
return filepath.SkipDir
97-
}
98-
nameLower := strings.ToLower(info.Name())
99-
if !info.IsDir() && (nameLower == "skill.md" || nameLower == "skills.md") && filepath.Base(filepath.Dir(path)) == skillName {
100-
foundPath = path
101-
}
102-
return nil
103-
})
104-
105-
if foundPath == "" {
106-
fmt.Printf("Error: Could not find skill '%s' in any directory.\n", skillName)
107-
return
108-
}
109-
110-
content, _ := ioutil.ReadFile(foundPath)
111-
112-
fmt.Println(skillHeaderStyle.Render(fmt.Sprintf("--- %s ---\n", strings.ToUpper(skillName))))
113-
fmt.Println(string(content))
33+
return skills.ReadSkill(baseDir, skillName)
11434
},
11535
}
11636

@@ -125,4 +45,3 @@ func init() {
12545
skillsReadCmd.Flags().String("dir", ".", "Base directory to crawl")
12646
viper.BindPFlag("skills.skills.read.dir", skillsReadCmd.Flags().Lookup("dir"))
12747
}
128-
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package infra
2+
3+
import (
4+
"fmt"
5+
"github.com/zalando/go-keyring"
6+
)
7+
8+
// SetToken abstracts the OS Keyring constraints inside the QuanuX infrastructure package
9+
func SetToken(token, target string) error {
10+
service := "QuanuX"
11+
user := "do_token"
12+
13+
err := keyring.Set(service, user, token)
14+
if err != nil {
15+
return fmt.Errorf("FATAL: Failed to inject token into the keyring: %w", err)
16+
}
17+
18+
fmt.Println("SUCCESS: DigitalOcean token securely locked into the OS Keychain.")
19+
fmt.Printf("The token will perfectly synchronize with Viper Target: %s\n", target)
20+
return nil
21+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package probe
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net"
7+
"sync"
8+
"time"
9+
10+
"github.com/charmbracelet/lipgloss"
11+
"github.com/zalando/go-keyring"
12+
)
13+
14+
var (
15+
probeHeaderStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#00FFFF")).MarginBottom(1)
16+
probeOkStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#00FF00"))
17+
probeFailStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#FF0000"))
18+
probeLabelStyle = lipgloss.NewStyle().Width(35)
19+
)
20+
21+
func ExecuteDiagnostics(ctx context.Context, timeout int, isFix bool) error {
22+
fmt.Println(probeHeaderStyle.Render("Initializing QuanuX High-Frequency Health Probe (Concurrency: 6...)\n"))
23+
24+
endpoints := map[string]string{
25+
"NATS JetStream Edge Core": "1.1.1.1:53",
26+
"QuanuX Telemetry Router (GCP)": "google.com:80",
27+
"Cloudflare Dual-Thread IPC": "8.8.8.8:53",
28+
"Local DuckDB Ingestion Loop": "127.0.0.1:4222",
29+
"Cython CGO Interlock Supervisor": "127.0.0.1:8080",
30+
}
31+
32+
var wg sync.WaitGroup
33+
var mu sync.Mutex
34+
results := make([]string, 0, len(endpoints)+1)
35+
36+
start := time.Now()
37+
38+
for label, addr := range endpoints {
39+
wg.Add(1)
40+
go func(l, a string) {
41+
defer wg.Done()
42+
43+
var dialer net.Dialer
44+
dialer.Timeout = time.Duration(timeout) * time.Millisecond
45+
conn, err := dialer.DialContext(ctx, "tcp", a)
46+
47+
status := probeFailStyle.Render("[X] OFFLINE ")
48+
if err == nil {
49+
status = probeOkStyle.Render("[✔] ONLINE ")
50+
conn.Close()
51+
}
52+
53+
res := fmt.Sprintf("%s %s %s", status, probeLabelStyle.Render(l), lipgloss.NewStyle().Foreground(lipgloss.Color("#666")).Render("("+a+")"))
54+
55+
mu.Lock()
56+
results = append(results, res)
57+
mu.Unlock()
58+
}(label, addr)
59+
}
60+
61+
wg.Add(1)
62+
go func() {
63+
defer wg.Done()
64+
65+
_, err := keyring.Get("QuanuX_Probe", "health_check_ping")
66+
67+
status := probeFailStyle.Render("[X] OFFLINE ")
68+
if err == nil || err == keyring.ErrNotFound {
69+
status = probeOkStyle.Render("[✔] ONLINE ")
70+
}
71+
72+
res := fmt.Sprintf("%s %s %s", status, probeLabelStyle.Render("Hardware OS Keyring Access"), lipgloss.NewStyle().Foreground(lipgloss.Color("#666")).Render("(Native Apple Keychain API)"))
73+
74+
mu.Lock()
75+
results = append(results, res)
76+
mu.Unlock()
77+
}()
78+
79+
wg.Wait()
80+
elapsed := time.Since(start)
81+
82+
for _, r := range results {
83+
fmt.Println(r)
84+
}
85+
86+
fmt.Printf("\nDiagnostic sweep completed natively in %s.\n", elapsed)
87+
88+
if isFix {
89+
fmt.Println(probeHeaderStyle.Render("\nAuto-Suture Protocol [--fix] engaged. Attempting localized repairs via cluster rollout..."))
90+
}
91+
92+
return nil
93+
}

0 commit comments

Comments
 (0)