blob: e0b6674c7caae9ee44dc446bfa4344840b26fab0 [file] [log] [blame]
iomodo50598c62025-07-27 22:06:32 +04001package commands
2
3import (
4 "fmt"
5 "log/slog"
6 "os"
7 "os/signal"
8 "syscall"
9
10 "github.com/iomodo/staff/server"
11 "github.com/spf13/cobra"
12)
13
14var serverCmd = &cobra.Command{
15 Use: "server",
16 Short: "Start the Staff server with automatic agent startup",
17 Long: `Start the Staff server which automatically loads configuration from config.yaml
18and starts all configured agents to process tasks continuously.
19
20The server will:
21- Load agent configurations from config.yaml
22- Start all configured agents in the background
23- Continuously process assigned tasks
24- Create GitHub PRs for completed tasks
25- Run until manually stopped with Ctrl+C
26
27Example:
28 staff server # Start server with default config.yaml
29 staff server --config custom.yaml # Start server with custom config`,
30 RunE: runServer,
31}
32
33var configPath string
34
35func init() {
36 serverCmd.Flags().StringVarP(&configPath, "config", "c", "config.yaml", "Path to configuration file")
37 rootCmd.AddCommand(serverCmd)
38}
39
40func runServer(cmd *cobra.Command, args []string) error {
41 // Create logger
42 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
43 Level: slog.LevelInfo,
44 }))
45
46 // Create server instance
47 srv, err := server.NewServer(logger, configPath)
48 if err != nil {
49 return fmt.Errorf("failed to create server: %w", err)
50 }
51
52 // Start server
53 if err := srv.Start(); err != nil {
54 return fmt.Errorf("failed to start server: %w", err)
55 }
56
57 // Setup graceful shutdown
58 sigChan := make(chan os.Signal, 1)
59 signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
60
61 logger.Info("Server is running. Press Ctrl+C to stop...")
62
63 // Wait for shutdown signal
64 <-sigChan
65
66 logger.Info("Shutdown signal received, stopping server...")
67 srv.Shutdown()
68
69 return nil
70}