From 7cad8bda81f0b1fe98daa3f7bd41d286bd2144cc Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 16 Sep 2022 15:18:46 +0200 Subject: [PATCH] Respect project root in sync command (#63) --- cmd/sync/sync.go | 10 ++++++---- cmd/sync/watchdog.go | 13 ++++--------- git/fileset.go | 5 +++++ project/project.go | 7 +++++++ 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/cmd/sync/sync.go b/cmd/sync/sync.go index 012ca48d..1b2f7fff 100644 --- a/cmd/sync/sync.go +++ b/cmd/sync/sync.go @@ -19,10 +19,11 @@ var syncCmd = &cobra.Command{ PreRunE: project.Configure, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - wsc := project.Get(ctx).WorkspacesClient() + prj := project.Get(ctx) + wsc := prj.WorkspacesClient() if *remotePath == "" { - me, err := project.Get(ctx).Me() + me, err := prj.Me() if err != nil { return err } @@ -42,11 +43,12 @@ var syncCmd = &cobra.Command{ return fmt.Errorf("repo not found, please ensure %s exists", *remotePath) } - fileSet, err := git.GetFileSet() + root := prj.Root() + fileSet := git.NewFileSet(root) if err != nil { return err } - syncCallback := getRemoteSyncCallback(ctx, *remotePath, wsc) + syncCallback := getRemoteSyncCallback(ctx, root, *remotePath, wsc) err = spawnSyncRoutine(ctx, fileSet, *interval, syncCallback) return err }, diff --git a/cmd/sync/watchdog.go b/cmd/sync/watchdog.go index 2e6d7c3f..a4cd9361 100644 --- a/cmd/sync/watchdog.go +++ b/cmd/sync/watchdog.go @@ -40,7 +40,7 @@ func putFile(ctx context.Context, path string, content io.Reader) error { return apiClient.Post(ctx, apiPath, content, nil) } -func getRemoteSyncCallback(ctx context.Context, remoteDir string, wsc *workspaces.WorkspacesClient) func(localDiff diff) error { +func getRemoteSyncCallback(ctx context.Context, root, remoteDir string, wsc *workspaces.WorkspacesClient) func(localDiff diff) error { return func(d diff) error { for _, filePath := range d.delete { err := wsc.Workspace.Delete(ctx, @@ -55,7 +55,7 @@ func getRemoteSyncCallback(ctx context.Context, remoteDir string, wsc *workspace log.Printf("[INFO] Deleted %s", filePath) } for _, filePath := range d.put { - f, err := os.Open(filePath) + f, err := os.Open(filepath.Join(root, filePath)) if err != nil { return err } @@ -93,13 +93,8 @@ func (w *watchdog) main(ctx context.Context, applyDiff func(diff) error) { defer w.wg.Done() // load from json or sync it every time there's an action state := snapshot{} - root, err := git.Root() - if err != nil { - log.Printf("[ERROR] cannot find project root: %s", err) - w.failure = err - return - } - err = state.loadSnapshot(root) + root := w.files.Root() + err := state.loadSnapshot(root) if err != nil { log.Printf("[ERROR] cannot load snapshot: %s", err) w.failure = err diff --git a/git/fileset.go b/git/fileset.go index c7015458..d38df1c9 100644 --- a/git/fileset.go +++ b/git/fileset.go @@ -61,6 +61,11 @@ func NewFileSet(root string) FileSet { } } +// Return root for fileset. +func (w *FileSet) Root() string { + return w.root +} + // Return all tracked files for Repo func (w *FileSet) All() ([]File, error) { return w.RecursiveListFiles(w.root) diff --git a/project/project.go b/project/project.go index eb3810bf..cdf2ade6 100644 --- a/project/project.go +++ b/project/project.go @@ -16,6 +16,8 @@ import ( type project struct { mu sync.Mutex + root string + config *Config wsc *workspaces.WorkspacesClient me *scim.User @@ -52,6 +54,7 @@ func Initialize(ctx context.Context, root string) (context.Context, error) { } p := project{ + root: root, config: &config, } @@ -74,6 +77,10 @@ func (p *project) WorkspacesClient() *workspaces.WorkspacesClient { return p.wsc } +func (p *project) Root() string { + return p.root +} + func (p *project) Me() (*scim.User, error) { p.mu.Lock() defer p.mu.Unlock()