Respect project root in sync command (#63)

This commit is contained in:
Pieter Noordhuis 2022-09-16 15:18:46 +02:00 committed by GitHub
parent ec6c58f1d1
commit 7cad8bda81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 13 deletions

View File

@ -19,10 +19,11 @@ var syncCmd = &cobra.Command{
PreRunE: project.Configure, PreRunE: project.Configure,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context() ctx := cmd.Context()
wsc := project.Get(ctx).WorkspacesClient() prj := project.Get(ctx)
wsc := prj.WorkspacesClient()
if *remotePath == "" { if *remotePath == "" {
me, err := project.Get(ctx).Me() me, err := prj.Me()
if err != nil { if err != nil {
return err return err
} }
@ -42,11 +43,12 @@ var syncCmd = &cobra.Command{
return fmt.Errorf("repo not found, please ensure %s exists", *remotePath) 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 { if err != nil {
return err return err
} }
syncCallback := getRemoteSyncCallback(ctx, *remotePath, wsc) syncCallback := getRemoteSyncCallback(ctx, root, *remotePath, wsc)
err = spawnSyncRoutine(ctx, fileSet, *interval, syncCallback) err = spawnSyncRoutine(ctx, fileSet, *interval, syncCallback)
return err return err
}, },

View File

@ -40,7 +40,7 @@ func putFile(ctx context.Context, path string, content io.Reader) error {
return apiClient.Post(ctx, apiPath, content, nil) 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 { return func(d diff) error {
for _, filePath := range d.delete { for _, filePath := range d.delete {
err := wsc.Workspace.Delete(ctx, err := wsc.Workspace.Delete(ctx,
@ -55,7 +55,7 @@ func getRemoteSyncCallback(ctx context.Context, remoteDir string, wsc *workspace
log.Printf("[INFO] Deleted %s", filePath) log.Printf("[INFO] Deleted %s", filePath)
} }
for _, filePath := range d.put { for _, filePath := range d.put {
f, err := os.Open(filePath) f, err := os.Open(filepath.Join(root, filePath))
if err != nil { if err != nil {
return err return err
} }
@ -93,13 +93,8 @@ func (w *watchdog) main(ctx context.Context, applyDiff func(diff) error) {
defer w.wg.Done() defer w.wg.Done()
// load from json or sync it every time there's an action // load from json or sync it every time there's an action
state := snapshot{} state := snapshot{}
root, err := git.Root() root := w.files.Root()
if err != nil { err := state.loadSnapshot(root)
log.Printf("[ERROR] cannot find project root: %s", err)
w.failure = err
return
}
err = state.loadSnapshot(root)
if err != nil { if err != nil {
log.Printf("[ERROR] cannot load snapshot: %s", err) log.Printf("[ERROR] cannot load snapshot: %s", err)
w.failure = err w.failure = err

View File

@ -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 // Return all tracked files for Repo
func (w *FileSet) All() ([]File, error) { func (w *FileSet) All() ([]File, error) {
return w.RecursiveListFiles(w.root) return w.RecursiveListFiles(w.root)

View File

@ -16,6 +16,8 @@ import (
type project struct { type project struct {
mu sync.Mutex mu sync.Mutex
root string
config *Config config *Config
wsc *workspaces.WorkspacesClient wsc *workspaces.WorkspacesClient
me *scim.User me *scim.User
@ -52,6 +54,7 @@ func Initialize(ctx context.Context, root string) (context.Context, error) {
} }
p := project{ p := project{
root: root,
config: &config, config: &config,
} }
@ -74,6 +77,10 @@ func (p *project) WorkspacesClient() *workspaces.WorkspacesClient {
return p.wsc return p.wsc
} }
func (p *project) Root() string {
return p.root
}
func (p *project) Me() (*scim.User, error) { func (p *project) Me() (*scim.User, error) {
p.mu.Lock() p.mu.Lock()
defer p.mu.Unlock() defer p.mu.Unlock()