Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

handle projects from multiple accounts in the same directory to enable seamless account switching (fixes #134)

Dane Schneidera44585f82a3d
diff --git a/app/cli/auth/auth.go b/app/cli/auth/auth.go
index 9eb99c33..2186cb90 100644
--- a/app/cli/auth/auth.go
+++ b/app/cli/auth/auth.go
@@ -85,7 +85,6 @@ func MustResolveAuth(requireOrg bool) {
term.OutputErrorAndExit("Error writing auth: %v", err)
}
}
-
}
func RefreshInvalidToken() error {
diff --git a/app/cli/cmd/plans.go b/app/cli/cmd/plans.go
index a5185a7f..db4ab8d8 100644
--- a/app/cli/cmd/plans.go
+++ b/app/cli/cmd/plans.go
@@ -58,7 +58,7 @@ func listActive() {
var childProjectIdsWithPaths [][2]string
go func() {
- res, err := fs.GetParentProjectIdsWithPaths()
+ res, err := fs.GetParentProjectIdsWithPaths(auth.Current.UserId)
if err != nil {
errCh <- fmt.Errorf("error getting parent project ids with paths: %v", err)
@@ -73,7 +73,7 @@ func listActive() {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
- res, err := fs.GetChildProjectIdsWithPaths(ctx)
+ res, err := fs.GetChildProjectIdsWithPaths(ctx, auth.Current.UserId)
if err != nil {
log.Println(err.Error())
diff --git a/app/cli/fs/fs.go b/app/cli/fs/fs.go
index 8fd858c3..a723b515 100644
--- a/app/cli/fs/fs.go
+++ b/app/cli/fs/fs.go
@@ -64,14 +64,14 @@ func init() {
term.OutputErrorAndExit(err.Error())
}
- PlandexDir = findPlandex(Cwd)
+ FindPlandexDir()
if PlandexDir != "" {
ProjectRoot = Cwd
}
}
func FindOrCreatePlandex() (string, bool, error) {
- PlandexDir = findPlandex(Cwd)
+ FindPlandexDir()
if PlandexDir != "" {
ProjectRoot = Cwd
return PlandexDir, false, nil
@@ -402,24 +402,30 @@ func GetPlandexIgnore(dir string) (*ignore.GitIgnore, error) {
return nil, nil
}
-func GetParentProjectIdsWithPaths() ([][2]string, error) {
+func GetParentProjectIdsWithPaths(currentUserId string) ([][2]string, error) {
var parentProjectIds [][2]string
currentDir := filepath.Dir(Cwd)
for currentDir != "/" {
plandexDir := findPlandex(currentDir)
- projectSettingsPath := filepath.Join(plandexDir, "project.json")
+ projectSettingsPath := filepath.Join(plandexDir, "projects-v2.json")
if _, err := os.Stat(projectSettingsPath); err == nil {
bytes, err := os.ReadFile(projectSettingsPath)
if err != nil {
return nil, fmt.Errorf("error reading projectId file: %s", err)
}
- var settings types.CurrentProjectSettings
- err = json.Unmarshal(bytes, &settings)
+ var settingsByAccount types.CurrentProjectSettingsByAccount
+ err = json.Unmarshal(bytes, &settingsByAccount)
if err != nil {
- term.OutputErrorAndExit("error unmarshalling project.json: %v", err)
+ term.OutputErrorAndExit("error unmarshalling projects-v2.json: %v", err)
+ }
+
+ settings := settingsByAccount[currentUserId]
+
+ if settings == nil {
+ return parentProjectIds, nil
}
projectId := string(settings.Id)
@@ -431,7 +437,7 @@ func GetParentProjectIdsWithPaths() ([][2]string, error) {
return parentProjectIds, nil
}
-func GetChildProjectIdsWithPaths(ctx context.Context) ([][2]string, error) {
+func GetChildProjectIdsWithPaths(ctx context.Context, currentUserId string) ([][2]string, error) {
var childProjectIds [][2]string
err := filepath.Walk(Cwd, func(path string, info os.FileInfo, err error) error {
@@ -463,17 +469,23 @@ func GetChildProjectIdsWithPaths(ctx context.Context) ([][2]string, error) {
if info.IsDir() && path != Cwd {
plandexDir := findPlandex(path)
- projectSettingsPath := filepath.Join(plandexDir, "project.json")
+ projectSettingsPath := filepath.Join(plandexDir, "projects-v2.json")
if _, err := os.Stat(projectSettingsPath); err == nil {
bytes, err := os.ReadFile(projectSettingsPath)
if err != nil {
return fmt.Errorf("error reading projectId file: %s", err)
}
- var settings types.CurrentProjectSettings
- err = json.Unmarshal(bytes, &settings)
+ var settingsByAccount types.CurrentProjectSettingsByAccount
+ err = json.Unmarshal(bytes, &settingsByAccount)
if err != nil {
- term.OutputErrorAndExit("error unmarshalling project.json: %v", err)
+ term.OutputErrorAndExit("error unmarshalling projects-v2.json: %v", err)
+ }
+
+ settings := settingsByAccount[currentUserId]
+
+ if settings == nil {
+ return nil
}
projectId := string(settings.Id)
@@ -534,6 +546,10 @@ func GetBaseDirForFilePaths(paths []string) string {
return baseDir
}
+func FindPlandexDir() {
+ PlandexDir = findPlandex(Cwd)
+}
+
func findPlandex(baseDir string) string {
var dir string
if os.Getenv("PLANDEX_ENV") == "development" {
diff --git a/app/cli/lib/current.go b/app/cli/lib/current.go
index cff4edac..6b92b6ab 100644
--- a/app/cli/lib/current.go
+++ b/app/cli/lib/current.go
@@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"plandex/api"
+ "plandex/auth"
"plandex/fs"
"plandex/term"
"plandex/types"
@@ -34,9 +35,15 @@ func MaybeResolveProject() {
}
func resolveProject(mustResolve, shouldCreate bool) {
- if fs.PlandexDir == "" && mustResolve && shouldCreate {
- _, _, err := fs.FindOrCreatePlandex()
- if err != nil {
+ if fs.PlandexDir == "" {
+ var err error
+ if shouldCreate {
+ _, _, err = fs.FindOrCreatePlandex()
+ } else {
+ fs.FindPlandexDir()
+ }
+
+ if err != nil && mustResolve {
term.OutputErrorAndExit("error finding or creating plandex: %v", err)
}
}
@@ -53,36 +60,51 @@ func resolveProject(mustResolve, shouldCreate bool) {
return
}
- // check if project.json exists in PlandexDir
- path := filepath.Join(fs.PlandexDir, "project.json")
+ MigrateLegacyProjectFile(auth.Current.UserId)
+
+ // check if projects-v2.json exists in PlandexDir
+ path := filepath.Join(fs.PlandexDir, "projects-v2.json")
_, err := os.Stat(path)
if os.IsNotExist(err) {
- log.Println("project.json does not exist")
+ log.Println("projects-v2.json does not exist")
log.Println("Initializing project")
- mustInitProject()
+ mustInitProject(nil)
} else if err != nil {
- term.OutputErrorAndExit("error checking if project.json exists: %v", err)
+ term.OutputErrorAndExit("error checking if projects-v2.json exists: %v", err)
}
- // read project.json
- bytes, err := os.ReadFile(path)
+ var settings *types.CurrentProjectSettings
+ var loadProjectSettings func()
+ loadProjectSettings = func() {
+ // read projects-v2.json
+ bytes, err := os.ReadFile(path)
- if err != nil {
- term.OutputErrorAndExit("error reading project.json: %v", err)
- }
+ if err != nil {
+ term.OutputErrorAndExit("error reading projects-v2.json: %v", err)
+ }
- var settings types.CurrentProjectSettings
- err = json.Unmarshal(bytes, &settings)
+ var settingsByAccount types.CurrentProjectSettingsByAccount
+ err = json.Unmarshal(bytes, &settingsByAccount)
- if err != nil {
- term.OutputErrorAndExit("error unmarshalling project.json: %v", err)
+ if err != nil {
+ term.OutputErrorAndExit("error unmarshalling projects-v2.json: %v", err)
+ }
+
+ settings = settingsByAccount[auth.Current.UserId]
+ if settings == nil {
+ mustInitProject(&settingsByAccount)
+ loadProjectSettings()
+ }
}
+ loadProjectSettings()
+
CurrentProjectId = settings.Id
+ MigrateLegacyCurrentPlanFile(auth.Current.UserId)
HomeCurrentProjectDir = filepath.Join(fs.HomePlandexDir, CurrentProjectId)
- HomeCurrentPlanPath = filepath.Join(HomeCurrentProjectDir, "current_plan.json")
+ HomeCurrentPlanPath = filepath.Join(HomeCurrentProjectDir, "current-plans-v2.json")
err = os.MkdirAll(HomeCurrentProjectDir, os.ModePerm)
@@ -91,6 +113,7 @@ func resolveProject(mustResolve, shouldCreate bool) {
}
MustLoadCurrentPlan()
+ MigrateLegacyPlanSettingsFile(auth.Current.UserId)
}
func MustLoadCurrentPlan() {
@@ -104,22 +127,26 @@ func MustLoadCurrentPlan() {
if os.IsNotExist(err) {
return
} else if err != nil {
- term.OutputErrorAndExit("error checking if current_plan.json exists: %v", err)
+ term.OutputErrorAndExit("error checking if current-plans-v2.json exists: %v", err)
}
// Read the contents of the file
fileBytes, err := os.ReadFile(HomeCurrentPlanPath)
if err != nil {
- term.OutputErrorAndExit("error reading current_plan.json: %v", err)
+ term.OutputErrorAndExit("error reading current-plans-v2.json: %v", err)
}
- var currentPlan types.CurrentPlanSettings
- err = json.Unmarshal(fileBytes, &currentPlan)
+ var currentPlansByAccount types.CurrentPlanSettingsByAccount
+ err = json.Unmarshal(fileBytes, &currentPlansByAccount)
if err != nil {
- term.OutputErrorAndExit("error unmarshalling current_plan.json: %v", err)
+ term.OutputErrorAndExit("error unmarshalling current-plans-v2.json: %v", err)
}
- CurrentPlanId = currentPlan.Id
+ currentPlan := currentPlansByAccount[auth.Current.UserId]
+
+ if currentPlan != nil {
+ CurrentPlanId = currentPlan.Id
+ }
if CurrentPlanId != "" {
err = loadCurrentBranch()
@@ -144,7 +171,7 @@ func loadCurrentBranch() error {
return fmt.Errorf("no current plan")
}
- path := filepath.Join(HomeCurrentProjectDir, CurrentPlanId, "settings.json")
+ path := filepath.Join(HomeCurrentProjectDir, CurrentPlanId, "settings-v2.json")
// Check if the file exists
_, err := os.Stat(path)
@@ -152,18 +179,23 @@ func loadCurrentBranch() error {
if os.IsNotExist(err) {
return nil
} else if err != nil {
- return fmt.Errorf("error checking if settings.json exists: %v", err)
+ return fmt.Errorf("error checking if settings-v2.json exists: %v", err)
}
fileBytes, err := os.ReadFile(path)
if err != nil {
- term.OutputErrorAndExit("error reading settings.json: %v", err)
+ term.OutputErrorAndExit("error reading settings-v2.json: %v", err)
}
- var settings types.PlanSettings
- err = json.Unmarshal(fileBytes, &settings)
+ var settingsByAccount types.PlanSettingsByAccount
+ err = json.Unmarshal(fileBytes, &settingsByAccount)
if err != nil {
- term.OutputErrorAndExit("error unmarshalling settings.json: %v", err)
+ term.OutputErrorAndExit("error unmarshalling settings-v2.json: %v", err)
+ }
+ settings := settingsByAccount[auth.Current.UserId]
+
+ if settings == nil {
+ return nil
}
CurrentBranch = settings.Branch
@@ -171,8 +203,7 @@ func loadCurrentBranch() error {
return nil
}
-func mustInitProject() {
- log.Println("Calling api.CreateProject()")
+func mustInitProject(existingSettings *types.CurrentProjectSettingsByAccount) {
res, apiErr := api.Client.CreateProject(shared.CreateProjectRequest{Name: filepath.Base(fs.ProjectRoot)})
if apiErr != nil {
@@ -183,11 +214,20 @@ func mustInitProject() {
CurrentProjectId = res.Id
- // write project.json
- path := filepath.Join(fs.PlandexDir, "project.json")
- bytes, err := json.Marshal(types.CurrentProjectSettings{
+ var settingsByAccount types.CurrentProjectSettingsByAccount
+ if existingSettings != nil {
+ settingsByAccount = *existingSettings
+ } else {
+ settingsByAccount = types.CurrentProjectSettingsByAccount{}
+ }
+
+ settingsByAccount[auth.Current.UserId] = &types.CurrentProjectSettings{
Id: CurrentProjectId,
- })
+ }
+
+ // write projects-v2.json
+ path := filepath.Join(fs.PlandexDir, "projects-v2.json")
+ bytes, err := json.Marshal(settingsByAccount)
if err != nil {
term.OutputErrorAndExit("error marshalling project settings: %v", err)
@@ -196,12 +236,12 @@ func mustInitProject() {
err = os.WriteFile(path, bytes, os.ModePerm)
if err != nil {
- term.OutputErrorAndExit("error writing project.json: %v", err)
+ term.OutputErrorAndExit("error writing projects-v2.json: %v", err)
}
- log.Println("Wrote project.json")
+ log.Println("Wrote projects-v2.json")
- // write current_plan.json to PlandexHomeDir/[projectId]/current_plan.json
+ // write current-plans-v2.json to PlandexHomeDir/[projectId]/current-plans-v2.json
dir := filepath.Join(fs.HomePlandexDir, CurrentProjectId)
err = os.MkdirAll(dir, os.ModePerm)
@@ -209,9 +249,11 @@ func mustInitProject() {
term.OutputErrorAndExit("error creating project dir: %v", err)
}
- path = filepath.Join(dir, "current_plan.json")
- bytes, err = json.Marshal(types.CurrentPlanSettings{
- Id: "",
+ path = filepath.Join(dir, "current-plans-v2.json")
+ bytes, err = json.Marshal(types.CurrentPlanSettingsByAccount{
+ auth.Current.UserId: &types.CurrentPlanSettings{
+ Id: "",
+ },
})
if err != nil {
@@ -221,8 +263,8 @@ func mustInitProject() {
err = os.WriteFile(path, bytes, os.ModePerm)
if err != nil {
- term.OutputErrorAndExit("error writing current_plan.json: %v", err)
+ term.OutputErrorAndExit("error writing current-plans-v2.json: %v", err)
}
- log.Println("Wrote current_plan.json")
+ log.Println("Wrote current-plans-v2.json")
}
diff --git a/app/cli/lib/legacy_files.go b/app/cli/lib/legacy_files.go
new file mode 100644
index 00000000..2796c2b1
--- /dev/null
+++ b/app/cli/lib/legacy_files.go
@@ -0,0 +1,163 @@
+package lib
+
+import (
+ "encoding/json"
+ "log"
+ "os"
+ "path/filepath"
+ "plandex/fs"
+ "plandex/term"
+ "plandex/types"
+)
+
+func MigrateLegacyProjectFile(currentUserId string) {
+ if fs.PlandexDir == "" {
+ return
+ }
+
+ if currentUserId == "" {
+ return
+ }
+
+ // Migrate project.json to projects-v2.json
+ // New formats map to a user id so we can handle multiple accounts in the same plandex dir
+ projectPath := filepath.Join(fs.PlandexDir, "project.json")
+ if _, err := os.Stat(projectPath); err == nil {
+ var settings types.CurrentProjectSettings
+ bytes, err := os.ReadFile(projectPath)
+ if err != nil {
+ term.OutputErrorAndExit("error reading project.json: %v", err)
+ }
+
+ err = json.Unmarshal(bytes, &settings)
+ if err != nil {
+ term.OutputErrorAndExit("error unmarshalling project.json: %v", err)
+ }
+
+ v2Path := filepath.Join(fs.PlandexDir, "projects-v2.json")
+ settingsByAccount := types.CurrentProjectSettingsByAccount{
+ currentUserId: &settings,
+ }
+ bytes, err = json.Marshal(settingsByAccount)
+ if err != nil {
+ term.OutputErrorAndExit("error marshalling projects-v2.json: %v", err)
+ }
+
+ err = os.WriteFile(v2Path, bytes, 0644)
+ if err != nil {
+ term.OutputErrorAndExit("error writing projects-v2.json: %v", err)
+ }
+
+ // Delete the v1 file after successful migration
+ if err := os.Remove(projectPath); err != nil {
+ term.OutputErrorAndExit("could not delete old project.json: %v", err)
+ }
+ log.Println("Migrated project.json to projects-v2.json")
+
+ } else if !os.IsNotExist(err) {
+ term.OutputErrorAndExit("error checking for project.json: %v", err)
+ }
+}
+
+func MigrateLegacyCurrentPlanFile(currentUserId string) {
+ if fs.PlandexDir == "" {
+ return
+ }
+
+ if currentUserId == "" {
+ return
+ }
+
+ if CurrentProjectId == "" {
+ return
+ }
+
+ // Migrate current_plan.json to current-plans-v2.json
+ planPath := filepath.Join(fs.HomePlandexDir, CurrentProjectId, "current_plan.json")
+
+ if _, err := os.Stat(planPath); err == nil {
+ var settings types.CurrentPlanSettings
+ bytes, err := os.ReadFile(planPath)
+ if err != nil {
+ term.OutputErrorAndExit("error reading current_plan.json: %v", err)
+ }
+
+ err = json.Unmarshal(bytes, &settings)
+ if err != nil {
+ term.OutputErrorAndExit("error unmarshalling current_plan.json: %v", err)
+ }
+
+ v2Path := filepath.Join(fs.HomePlandexDir, CurrentProjectId, "current-plans-v2.json")
+ settingsByAccount := types.CurrentPlanSettingsByAccount{
+ currentUserId: &settings,
+ }
+ bytes, err = json.Marshal(settingsByAccount)
+ if err != nil {
+ term.OutputErrorAndExit("error marshalling current-plans-v2.json: %v", err)
+ }
+
+ err = os.WriteFile(v2Path, bytes, 0644)
+ if err != nil {
+ term.OutputErrorAndExit("error writing current-plans-v2.json: %v", err)
+ }
+
+ // Delete the v1 file after successful migration
+ if err := os.Remove(planPath); err != nil {
+ term.OutputErrorAndExit("could not delete old current_plan.json: %v", err)
+ }
+ log.Println("Migrated current_plan.json to current-plans-v2.json")
+ } else if !os.IsNotExist(err) {
+ term.OutputErrorAndExit("error checking for current_plan.json: %v", err)
+ }
+}
+
+func MigrateLegacyPlanSettingsFile(currentUserId string) {
+ if fs.PlandexDir == "" {
+ return
+ }
+
+ if currentUserId == "" {
+ return
+ }
+
+ if CurrentPlanId == "" {
+ return
+ }
+
+ // Migrate settings.json to settings-v2.json for current plan
+ settingsPath := filepath.Join(fs.HomePlandexDir, CurrentProjectId, CurrentPlanId, "settings.json")
+ if _, err := os.Stat(settingsPath); err == nil {
+ var settings types.PlanSettings
+ bytes, err := os.ReadFile(settingsPath)
+ if err != nil {
+ term.OutputErrorAndExit("error reading settings.json: %v", err)
+ }
+
+ err = json.Unmarshal(bytes, &settings)
+ if err != nil {
+ term.OutputErrorAndExit("error unmarshalling settings.json: %v", err)
+ }
+
+ v2Path := filepath.Join(fs.HomePlandexDir, CurrentProjectId, CurrentPlanId, "settings-v2.json")
+ settingsByAccount := types.PlanSettingsByAccount{
+ currentUserId: &settings,
+ }
+ bytes, err = json.Marshal(settingsByAccount)
+ if err != nil {
+ term.OutputErrorAndExit("error marshalling settings-v2.json: %v", err)
+ }
+
+ err = os.WriteFile(v2Path, bytes, 0644)
+ if err != nil {
+ term.OutputErrorAndExit("error writing settings-v2.json: %v", err)
+ }
+
+ // Delete the v1 file after successful migration
+ if err := os.Remove(settingsPath); err != nil {
+ term.OutputErrorAndExit("could not delete old settings.json: %v", err)
+ }
+ log.Println("Migrated settings.json to settings-v2.json")
+ } else if !os.IsNotExist(err) {
+ term.OutputErrorAndExit("error checking for settings.json: %v", err)
+ }
+}
diff --git a/app/cli/lib/plans.go b/app/cli/lib/plans.go
index 0bca18d5..f1a4c08b 100644
--- a/app/cli/lib/plans.go
+++ b/app/cli/lib/plans.go
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "plandex/auth"
"plandex/fs"
"plandex/types"
"sync"
@@ -70,8 +71,10 @@ func WriteCurrentBranch(branch string) error {
return fmt.Errorf("no current plan")
}
- settings := types.PlanSettings{
- Branch: branch,
+ settings := types.PlanSettingsByAccount{
+ auth.Current.UserId: &types.PlanSettings{
+ Branch: branch,
+ },
}
bytes, err := json.Marshal(settings)
@@ -88,7 +91,7 @@ func WriteCurrentBranch(branch string) error {
return fmt.Errorf("error creating plan dir: %v", err)
}
- path := filepath.Join(dir, "settings.json")
+ path := filepath.Join(dir, "settings-v2.json")
err = os.WriteFile(path, bytes, 0644)
@@ -146,28 +149,33 @@ func getPlanCurrentBranch(planId string) (string, error) {
return "", fmt.Errorf("no current project")
}
- path := filepath.Join(fs.HomePlandexDir, CurrentProjectId, planId, "settings.json")
+ v2Path := filepath.Join(fs.HomePlandexDir, CurrentProjectId, planId, "settings-v2.json")
- // Check if settings.json exists
- _, err := os.Stat(path)
+ var settings *types.PlanSettings
- if os.IsNotExist(err) {
- return "main", nil
- } else if err != nil {
- return "", fmt.Errorf("error checking if settings.json exists: %v", err)
- }
-
- bytes, err := os.ReadFile(path)
+ // check if settings-v2.json exists
+ _, err := os.Stat(v2Path)
+ if err == nil {
+ // read settings-v2.json
+ var settingsByAccount types.PlanSettingsByAccount
+ bytes, err := os.ReadFile(v2Path)
+ if err != nil {
+ return "", fmt.Errorf("error reading settings-v2.json: %v", err)
+ }
+ err = json.Unmarshal(bytes, &settingsByAccount)
+ if err != nil {
+ return "", fmt.Errorf("error unmarshalling settings-v2.json: %v", err)
+ }
- if err != nil {
- return "", fmt.Errorf("error reading plan settings: %v", err)
+ settings = settingsByAccount[auth.Current.UserId]
+ } else if os.IsNotExist(err) {
+ return "main", nil
+ } else {
+ return "", fmt.Errorf("error checking if settings-v2.json exists: %v", err)
}
- var settings types.PlanSettings
- err = json.Unmarshal(bytes, &settings)
-
- if err != nil {
- return "", fmt.Errorf("error unmarshalling plan settings: %v", err)
+ if settings == nil {
+ return "main", nil
}
return settings.Branch, nil
diff --git a/app/cli/types/types.go b/app/cli/types/types.go
index 99c176c6..b4331388 100644
--- a/app/cli/types/types.go
+++ b/app/cli/types/types.go
@@ -45,6 +45,10 @@ type CurrentProjectSettings struct {
Id string `json:"id"`
}
+type CurrentPlanSettingsByAccount map[string]*CurrentPlanSettings
+type PlanSettingsByAccount map[string]*PlanSettings
+type CurrentProjectSettingsByAccount map[string]*CurrentProjectSettings
+
type ChangesUIScrollReplacement struct {
OldContent string
NewContent string