Skip to content

Commit 677aab2

Browse files
comchangsclaude
andcommitted
refactor: #41 split cmd/tow/main.go into 8 command files
main.go (2422 lines) → 8 files: - main.go (352) — core: main, loadContext, resolveTargets, interactive pickers - cmd_deploy.go (135) — build, package, deploy, auto - cmd_lifecycle.go (238) — start, stop, restart, status, rollback - cmd_monitor.go (807) — logs, ssh, diff, doctor, metrics, presets - cmd_config.go (234) — config server/module/assign - cmd_manage.go (260) — setup, upload, install, list, download, cleanup, provision - cmd_other.go (193) — init, validate, login, unlock, threaddump, mcp-server - cmd_plugin.go (286) — plugin list/install/add/remove Pure refactor — no behavior changes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 34de4d6 commit 677aab2

8 files changed

Lines changed: 2156 additions & 2073 deletions

File tree

‎cmd/tow/cmd_config.go‎

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/neurosamAI/tow-cli/internal/config"
8+
"github.com/neurosamAI/tow-cli/internal/logger"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func newConfigCmd() *cobra.Command {
14+
cmd := &cobra.Command{
15+
Use: "config",
16+
Short: "Manage servers, modules, and assignments",
17+
}
18+
19+
// --- server subcommands ---
20+
serverCmd := &cobra.Command{Use: "server", Short: "Manage servers"}
21+
22+
serverCmd.AddCommand(&cobra.Command{
23+
Use: "add",
24+
Short: "Add a server to tow.local.yaml",
25+
Long: `Add a new server to an environment.
26+
27+
Examples:
28+
tow config server add -e prod --name kafka-4 --host 10.0.2.100
29+
tow config server add -e prod --name kafka-4 --host 10.0.2.100 --number 4 --modules kafka,zookeeper`,
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
envName, _ := cmd.Flags().GetString("environment")
32+
name, _ := cmd.Flags().GetString("name")
33+
host, _ := cmd.Flags().GetString("host")
34+
number, _ := cmd.Flags().GetInt("number")
35+
modulesStr, _ := cmd.Flags().GetString("modules")
36+
37+
if envName == "" || name == "" || host == "" {
38+
return fmt.Errorf("--environment, --name, and --host are required")
39+
}
40+
41+
var modules []string
42+
if modulesStr != "" {
43+
modules = strings.Split(modulesStr, ",")
44+
for i := range modules {
45+
modules[i] = strings.TrimSpace(modules[i])
46+
}
47+
}
48+
49+
localPath := "tow.local.yaml"
50+
if err := config.AddServer(localPath, envName, name, host, number, modules); err != nil {
51+
return err
52+
}
53+
logger.Success("Server %s (%s) added to %s in %s", name, host, envName, localPath)
54+
return nil
55+
},
56+
})
57+
58+
serverCmd.AddCommand(&cobra.Command{
59+
Use: "remove",
60+
Short: "Remove a server from tow.local.yaml",
61+
RunE: func(cmd *cobra.Command, args []string) error {
62+
envName, _ := cmd.Flags().GetString("environment")
63+
name, _ := cmd.Flags().GetString("name")
64+
if envName == "" || name == "" {
65+
return fmt.Errorf("--environment and --name are required")
66+
}
67+
if err := config.RemoveServer("tow.local.yaml", envName, name); err != nil {
68+
return err
69+
}
70+
logger.Success("Server %s removed from %s", name, envName)
71+
return nil
72+
},
73+
})
74+
75+
serverCmd.AddCommand(&cobra.Command{
76+
Use: "list",
77+
Short: "List servers in an environment",
78+
RunE: func(cmd *cobra.Command, args []string) error {
79+
envName, _ := cmd.Flags().GetString("environment")
80+
if envName == "" {
81+
return fmt.Errorf("--environment is required")
82+
}
83+
servers, err := config.ListServers("tow.local.yaml", envName)
84+
if err != nil {
85+
return err
86+
}
87+
for _, srv := range servers {
88+
mods := ""
89+
if len(srv.Modules) > 0 {
90+
mods = strings.Join(srv.Modules, ", ")
91+
}
92+
fmt.Printf(" %-20s host=%-18s modules=[%s]\n", srv.Name, srv.Host, mods)
93+
}
94+
return nil
95+
},
96+
})
97+
98+
// Add flags to server subcommands
99+
for _, sub := range serverCmd.Commands() {
100+
sub.Flags().String("name", "", "server name")
101+
sub.Flags().String("host", "", "server IP or hostname")
102+
sub.Flags().Int("number", 0, "server number (for legacy deploy_path)")
103+
sub.Flags().String("modules", "", "comma-separated module names to assign")
104+
}
105+
cmd.AddCommand(serverCmd)
106+
107+
// --- module subcommands ---
108+
moduleCmd := &cobra.Command{Use: "module", Short: "Manage modules"}
109+
110+
moduleCmd.AddCommand(&cobra.Command{
111+
Use: "add",
112+
Short: "Add a module definition to tow.yaml",
113+
Long: `Add a new module to the project config.
114+
115+
Examples:
116+
tow config module add my-api --type springboot --port 8080
117+
tow config module add my-cache --type redis --port 6379 --version 7.2.4`,
118+
Args: cobra.ExactArgs(1),
119+
RunE: func(cmd *cobra.Command, args []string) error {
120+
modType, _ := cmd.Flags().GetString("type")
121+
port, _ := cmd.Flags().GetInt("port")
122+
version, _ := cmd.Flags().GetString("version")
123+
124+
if modType == "" {
125+
return fmt.Errorf("--type is required")
126+
}
127+
128+
cfgPath, _ := cmd.Root().Flags().GetString("config")
129+
if err := config.AddModule(cfgPath, args[0], modType, port, version); err != nil {
130+
return err
131+
}
132+
logger.Success("Module %s (type=%s) added to %s", args[0], modType, cfgPath)
133+
return nil
134+
},
135+
})
136+
137+
moduleCmd.AddCommand(&cobra.Command{
138+
Use: "remove",
139+
Short: "Remove a module from tow.yaml",
140+
Args: cobra.ExactArgs(1),
141+
RunE: func(cmd *cobra.Command, args []string) error {
142+
cfgPath, _ := cmd.Root().Flags().GetString("config")
143+
if err := config.RemoveModule(cfgPath, args[0]); err != nil {
144+
return err
145+
}
146+
logger.Success("Module %s removed from %s", args[0], cfgPath)
147+
return nil
148+
},
149+
})
150+
151+
moduleCmd.AddCommand(&cobra.Command{
152+
Use: "list",
153+
Short: "List all modules",
154+
RunE: func(cmd *cobra.Command, args []string) error {
155+
cfg, _, err := loadContext(cmd)
156+
if err != nil {
157+
return err
158+
}
159+
for name, mod := range cfg.Modules {
160+
ver := mod.Version
161+
if ver == "" {
162+
ver = "-"
163+
}
164+
fmt.Printf(" %-30s type=%-12s port=%-6d version=%s\n", name, mod.Type, mod.Port, ver)
165+
}
166+
return nil
167+
},
168+
})
169+
170+
// Add flags to module subcommands
171+
for _, sub := range moduleCmd.Commands() {
172+
sub.Flags().String("type", "", "module type (springboot, node, kafka, redis, etc.)")
173+
sub.Flags().Int("port", 0, "service port")
174+
sub.Flags().String("version", "", "package version (for plugin types)")
175+
}
176+
cmd.AddCommand(moduleCmd)
177+
178+
// --- assign/unassign ---
179+
cmd.AddCommand(&cobra.Command{
180+
Use: "assign",
181+
Short: "Assign modules to a server",
182+
Long: `tow config assign -e prod --server kafka-4 --modules kafka,zookeeper,node-exporter`,
183+
RunE: func(cmd *cobra.Command, args []string) error {
184+
envName, _ := cmd.Flags().GetString("environment")
185+
server, _ := cmd.Flags().GetString("server")
186+
modulesStr, _ := cmd.Flags().GetString("modules")
187+
if envName == "" || server == "" || modulesStr == "" {
188+
return fmt.Errorf("--environment, --server, and --modules are required")
189+
}
190+
modules := strings.Split(modulesStr, ",")
191+
for i := range modules {
192+
modules[i] = strings.TrimSpace(modules[i])
193+
}
194+
if err := config.AssignModules("tow.local.yaml", envName, server, modules); err != nil {
195+
return err
196+
}
197+
logger.Success("Assigned [%s] to %s in %s", modulesStr, server, envName)
198+
return nil
199+
},
200+
})
201+
202+
cmd.AddCommand(&cobra.Command{
203+
Use: "unassign",
204+
Short: "Remove modules from a server",
205+
Long: `tow config unassign -e prod --server kafka-4 --modules zookeeper`,
206+
RunE: func(cmd *cobra.Command, args []string) error {
207+
envName, _ := cmd.Flags().GetString("environment")
208+
server, _ := cmd.Flags().GetString("server")
209+
modulesStr, _ := cmd.Flags().GetString("modules")
210+
if envName == "" || server == "" || modulesStr == "" {
211+
return fmt.Errorf("--environment, --server, and --modules are required")
212+
}
213+
modules := strings.Split(modulesStr, ",")
214+
for i := range modules {
215+
modules[i] = strings.TrimSpace(modules[i])
216+
}
217+
if err := config.UnassignModules("tow.local.yaml", envName, server, modules); err != nil {
218+
return err
219+
}
220+
logger.Success("Unassigned [%s] from %s in %s", modulesStr, server, envName)
221+
return nil
222+
},
223+
})
224+
225+
// Add shared flags to assign/unassign
226+
for _, sub := range cmd.Commands() {
227+
if sub.Name() == "assign" || sub.Name() == "unassign" {
228+
sub.Flags().String("server", "", "server name")
229+
sub.Flags().String("modules", "", "comma-separated module names")
230+
}
231+
}
232+
233+
return cmd
234+
}

‎cmd/tow/cmd_deploy.go‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/neurosamAI/tow-cli/internal/deploy"
7+
"github.com/neurosamAI/tow-cli/internal/pipeline"
8+
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func newBuildCmd() *cobra.Command {
13+
return &cobra.Command{
14+
Use: "build",
15+
Short: "Build a module locally (compile/jar/binary)",
16+
Long: `Runs the module's build command locally. Use this to build without deploying.`,
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
cfg, sshMgr, err := loadContext(cmd)
19+
if err != nil {
20+
return err
21+
}
22+
defer sshMgr.Close()
23+
24+
env, mod, _, err := resolveTargets(cmd, cfg)
25+
if err != nil {
26+
return err
27+
}
28+
29+
p := pipeline.New(cfg, sshMgr)
30+
return p.Build(mod, env)
31+
},
32+
}
33+
}
34+
35+
func newPackageCmd() *cobra.Command {
36+
return &cobra.Command{
37+
Use: "package",
38+
Short: "Package a module into a deployment artifact (tar.gz)",
39+
Long: `Creates the tar.gz artifact for a module. Use this after build, before upload.`,
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
cfg, sshMgr, err := loadContext(cmd)
42+
if err != nil {
43+
return err
44+
}
45+
defer sshMgr.Close()
46+
47+
env, mod, _, err := resolveTargets(cmd, cfg)
48+
if err != nil {
49+
return err
50+
}
51+
52+
p := pipeline.New(cfg, sshMgr)
53+
return p.Package(mod, env)
54+
},
55+
}
56+
}
57+
58+
func newDeployCmd() *cobra.Command {
59+
cmd := &cobra.Command{
60+
Use: "deploy",
61+
Short: "Deploy a module (package → upload → install → restart)",
62+
RunE: withBranchCheck("deploy", func(cmd *cobra.Command, args []string) error {
63+
cfg, sshMgr, err := loadContext(cmd)
64+
if err != nil {
65+
return err
66+
}
67+
defer sshMgr.Close()
68+
69+
env, mod, server, err := resolveTargets(cmd, cfg)
70+
if err != nil {
71+
return err
72+
}
73+
74+
yes, _ := cmd.Flags().GetBool("yes")
75+
if !yes && !confirmProdDeploy(cmd, env, mod, "deploy") {
76+
return fmt.Errorf("deployment cancelled")
77+
}
78+
79+
rolling, _ := cmd.Flags().GetBool("rolling")
80+
81+
deployer := deploy.New(cfg, sshMgr)
82+
return deployer.WithLock(env, mod, server, "deploy", func() error {
83+
deployer.WriteAuditLog(env, mod, "deploy", fmt.Sprintf("rolling=%v", rolling))
84+
p := pipeline.New(cfg, sshMgr)
85+
p.Rolling = rolling
86+
return p.Deploy(env, mod, server)
87+
})
88+
}),
89+
}
90+
cmd.Flags().Bool("rolling", false, "use rolling deployment (one server at a time)")
91+
cmd.Flags().BoolP("yes", "y", false, "skip production confirmation prompt")
92+
return cmd
93+
}
94+
95+
func newAutoCmd() *cobra.Command {
96+
cmd := &cobra.Command{
97+
Use: "auto",
98+
Short: "Full pipeline: build → package → upload → install → restart",
99+
RunE: withBranchCheck("auto", func(cmd *cobra.Command, args []string) error {
100+
cfg, sshMgr, err := loadContext(cmd)
101+
if err != nil {
102+
return err
103+
}
104+
defer sshMgr.Close()
105+
106+
env, mod, server, err := resolveTargets(cmd, cfg)
107+
if err != nil {
108+
return err
109+
}
110+
111+
yes, _ := cmd.Flags().GetBool("yes")
112+
if !yes && !confirmProdDeploy(cmd, env, mod, "auto deploy") {
113+
return fmt.Errorf("deployment cancelled")
114+
}
115+
116+
rolling, _ := cmd.Flags().GetBool("rolling")
117+
autoRollback, _ := cmd.Flags().GetBool("auto-rollback")
118+
119+
deployer := deploy.New(cfg, sshMgr)
120+
return deployer.WithLock(env, mod, server, "auto", func() error {
121+
deployer.WriteAuditLog(env, mod, "auto", fmt.Sprintf("rolling=%v auto-rollback=%v", rolling, autoRollback))
122+
p := pipeline.New(cfg, sshMgr)
123+
p.Rolling = rolling
124+
if autoRollback {
125+
return p.AutoWithRollback(env, mod, server)
126+
}
127+
return p.Auto(env, mod, server)
128+
})
129+
}),
130+
}
131+
cmd.Flags().Bool("rolling", false, "use rolling deployment (one server at a time)")
132+
cmd.Flags().Bool("auto-rollback", false, "automatically rollback if health check fails after start")
133+
cmd.Flags().BoolP("yes", "y", false, "skip production confirmation prompt")
134+
return cmd
135+
}

0 commit comments

Comments
 (0)