11package projects
22
33import (
4- "bufio"
54 "fmt"
65 "os"
7- "os/exec"
8- "strings"
6+ "path/filepath"
7+
8+ "beacon/internal/config"
9+ "beacon/internal/deploy"
10+ "beacon/internal/state"
11+ "beacon/internal/util"
912
1013 "github.com/spf13/cobra"
1114)
1215
1316func createRedeployCommand (pm * ProjectManager ) * cobra.Command {
1417 return & cobra.Command {
1518 Use : "redeploy <project-name>" ,
16- Short : "Pull latest changes and re-run the deploy command " ,
17- Long : `Fetches the latest code for a project and re-runs its deploy command .
19+ Short : "Run a full deploy cycle for a project " ,
20+ Long : `Triggers a full deploy for a project using its existing configuration .
1821
19- The project must be in the inventory (beacon projects list) and have a
20- working directory with a git repository. The deploy command is read from
21- the project's env file (BEACON_DEPLOY_CMD ).` ,
22+ Loads the project's env file, builds the deploy config (same as beacon deploy),
23+ and runs the full deploy cycle (clone + deploy command for git projects,
24+ or the configured deploy flow for docker projects ).` ,
2225 Example : ` beacon projects redeploy myapp` ,
2326 Args : cobra .ExactArgs (1 ),
2427 Run : func (cmd * cobra.Command , args []string ) {
@@ -30,87 +33,32 @@ the project's env file (BEACON_DEPLOY_CMD).`,
3033 }
3134}
3235
33- // Redeploy pulls latest code and re- runs the deploy command for a project.
36+ // Redeploy runs a full deploy cycle for a project.
3437func (pm * ProjectManager ) Redeploy (projectName string ) error {
35- inv , err := LoadInventory (pm .paths .GetProjectsFilePath ())
36- if err != nil {
37- return fmt .Errorf ("load inventory: %w" , err )
38- }
39- entry := GetProject (inv , projectName )
40- if entry == nil {
41- return fmt .Errorf ("project %q not found in inventory (run `beacon projects list` to see available projects)" , projectName )
42- }
43-
44- location := entry .Location
45- if location == "" {
46- location = pm .paths .GetProjectWorkingDir (projectName )
47- }
48- if _ , err := os .Stat (location ); err != nil {
49- return fmt .Errorf ("project directory %s does not exist" , location )
38+ if ! pm .paths .ProjectExists (projectName ) {
39+ return fmt .Errorf ("project %q not found (run `beacon projects list` to see available projects)" , projectName )
5040 }
5141
5242 envFile := pm .paths .GetProjectEnvFile (projectName )
53- deployCmd := readEnvVar (envFile , "BEACON_DEPLOY_CMD" )
54-
55- fmt .Printf ("Redeploying %s in %s\n " , projectName , location )
56-
57- fmt .Println ("Pulling latest changes..." )
58- pull := exec .Command ("git" , "pull" )
59- pull .Dir = location
60- pull .Stdout = os .Stdout
61- pull .Stderr = os .Stderr
62- if err := pull .Run (); err != nil {
63- return fmt .Errorf ("git pull failed: %w" , err )
43+ if _ , err := os .Stat (envFile ); err != nil {
44+ return fmt .Errorf ("env file not found: %s" , envFile )
6445 }
6546
66- if deployCmd == "" {
67- fmt .Println ("No BEACON_DEPLOY_CMD configured — skipping deploy command." )
68- fmt .Println ("Done (code updated, no deploy command ran)." )
69- return nil
47+ if err := util .LoadEnvFile (envFile ); err != nil {
48+ return fmt .Errorf ("load env file: %w" , err )
7049 }
7150
72- fmt . Printf ( "Running deploy command: %s \n " , deployCmd )
51+ cfg := config . Load ( )
7352
74- secureEnvPath := readEnvVar (envFile , "BEACON_SECURE_ENV_PATH" )
75- command := deployCmd
76- if secureEnvPath != "" {
77- if _ , err := os .Stat (os .ExpandEnv (secureEnvPath )); err == nil {
78- command = fmt .Sprintf ("set -a && . %s && set +a && %s" , secureEnvPath , deployCmd )
79- }
80- }
53+ statusDir := filepath .Join (os .Getenv ("HOME" ), ".beacon" , cfg .ProjectDir )
54+ status := state .NewStatus (statusDir )
55+
56+ fmt .Printf ("Deploying %s (%s) from %s\n " , projectName , cfg .DeploymentType , cfg .LocalPath )
8157
82- deploy := exec .Command ("sh" , "-c" , command )
83- deploy .Dir = location
84- deploy .Stdout = os .Stdout
85- deploy .Stderr = os .Stderr
86- if err := deploy .Run (); err != nil {
87- return fmt .Errorf ("deploy command failed: %w" , err )
58+ if err := deploy .Deploy (cfg , "" , status ); err != nil {
59+ return err
8860 }
8961
9062 fmt .Printf ("Redeploy of %s complete.\n " , projectName )
9163 return nil
9264}
93-
94- // readEnvVar reads a specific variable from a KEY=VALUE env file.
95- func readEnvVar (path , key string ) string {
96- f , err := os .Open (path )
97- if err != nil {
98- return ""
99- }
100- defer func () { _ = f .Close () }()
101-
102- prefix := key + "="
103- scanner := bufio .NewScanner (f )
104- for scanner .Scan () {
105- line := strings .TrimSpace (scanner .Text ())
106- if strings .HasPrefix (line , "#" ) {
107- continue
108- }
109- if strings .HasPrefix (line , prefix ) {
110- val := strings .TrimPrefix (line , prefix )
111- val = strings .Trim (val , "\" '" )
112- return val
113- }
114- }
115- return ""
116- }
0 commit comments