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,
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
},

View File

@ -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

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
func (w *FileSet) All() ([]File, error) {
return w.RecursiveListFiles(w.root)

View File

@ -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()