Skip to content

Commit c94edd8

Browse files
committed
feat(update): support updates via shell scripts
1 parent 905c731 commit c94edd8

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

base/cron.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func LocalShellScriptRunner(basepath string) cron.RunJobFunc {
2323
return func(ctx context.Context, streams ioes.IOStreams, job *cron.Job) error {
2424
path := job.Name
2525
if qfs.PathKind(job.Name) == "local" {
26-
path = filepath.Join(basepath, path)
26+
// TODO (b5) - need to first check that path can't be found
27+
// path = filepath.Join(basepath, path)
2728
}
2829

2930
cmd := exec.Command(path)

cmd/factory_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func NewTestFactory(c *regclient.Client) (tf TestFactory, err error) {
6363
rpc: nil,
6464
config: cfg,
6565
node: tnode.(*p2p.QriNode),
66+
inst: lib.NewInstanceFromConfigAndNode(cfg, tnode.(*p2p.QriNode)),
6667
}, nil
6768
}
6869

lib/lib.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,12 @@ func (inst *Instance) RPC() *rpc.Client {
529529
func (inst *Instance) Teardown() {
530530
inst.teardown()
531531
}
532+
533+
// QriPath returns the path to where qri is operating from on the local filesystem
534+
// there are situations where this will be a temporary directory
535+
func (inst *Instance) QriPath() string {
536+
if inst.cfg != nil && inst.cfg.Repo != nil && inst.cfg.Repo.Path != "" {
537+
return inst.cfg.Repo.Path
538+
}
539+
return os.TempDir()
540+
}

lib/update.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,36 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"os"
8+
"path/filepath"
79

810
"github.com/qri-io/dataset"
911
"github.com/qri-io/ioes"
1012
"github.com/qri-io/qfs"
13+
"github.com/qri-io/qri/actions"
1114
"github.com/qri-io/qri/base"
1215
"github.com/qri-io/qri/cron"
1316
"github.com/qri-io/qri/repo"
1417
)
1518

1619
// NewUpdateMethods creates a configuration handle from an instance
1720
func NewUpdateMethods(inst *Instance) *UpdateMethods {
18-
return &UpdateMethods{inst: inst}
21+
m := &UpdateMethods{
22+
inst: inst,
23+
scriptsPath: filepath.Join(inst.QriPath(), "update_scripts"),
24+
}
25+
26+
if err := os.MkdirAll(m.scriptsPath, os.ModePerm); err != nil {
27+
log.Error("creating update scripts directory: %s", err.Error())
28+
}
29+
30+
return m
1931
}
2032

2133
// UpdateMethods enapsulates logic for scheduled updates
2234
type UpdateMethods struct {
2335
inst *Instance
24-
ScriptsPath string
36+
scriptsPath string
2537
}
2638

2739
// CoreRequestsName specifies this is a Methods object
@@ -43,6 +55,12 @@ type ScheduleParams struct {
4355

4456
// Schedule creates a job and adds it to the scheduler
4557
func (m *UpdateMethods) Schedule(in *ScheduleParams, out *cron.Job) (err error) {
58+
if base.PossibleShellScript(in.Name) {
59+
if err = qfs.AbsPath(&in.Name); err != nil {
60+
return
61+
}
62+
}
63+
4664
if m.inst.rpc != nil {
4765
return m.inst.rpc.Call("UpdateMethods.Schedule", in, out)
4866
}
@@ -64,6 +82,7 @@ func (m *UpdateMethods) Schedule(in *ScheduleParams, out *cron.Job) (err error)
6482

6583
func (m *UpdateMethods) jobFromScheduleParams(p *ScheduleParams) (job *cron.Job, err error) {
6684
if base.PossibleShellScript(p.Name) {
85+
// TODO (b5) - confirm file exists & is executable
6786
return base.ShellScriptToJob(qfs.NewMemfileBytes(p.Name, nil), p.Periodicity, nil)
6887
}
6988

@@ -170,7 +189,7 @@ func (m *UpdateMethods) Run(p *Job, res *repo.DatasetRef) (err error) {
170189
err = m.runDatasetUpdate(updateParams, res)
171190

172191
case cron.JTShellScript:
173-
runner := base.LocalShellScriptRunner(m.ScriptsPath)
192+
runner := base.LocalShellScriptRunner(m.scriptsPath)
174193
err = runner(m.inst.ctx, m.inst.streams, p)
175194

176195
default:
@@ -214,10 +233,10 @@ func (m *UpdateMethods) runDatasetUpdate(p *UpdateParams, res *repo.DatasetRef)
214233
return err
215234
}
216235

217-
// if !base.InLocalNamespace(m.inst.Repo(), &ref) {
218-
// *res, err = actions.UpdateRemoteDataset(m.node, &ref, true)
219-
// return err
220-
// }
236+
if !base.InLocalNamespace(m.inst.Repo(), &ref) {
237+
*res, err = actions.UpdateRemoteDataset(m.inst.Node(), &ref, true)
238+
return err
239+
}
221240

222241
// default to recalling transfrom scripts for local updates
223242
// TODO (b5): not sure if this should be here or in client libraries
@@ -261,7 +280,6 @@ func newUpdateRunner(newInst func(ctx context.Context, streams ioes.IOStreams) (
261280
}
262281

263282
m := NewUpdateMethods(inst)
264-
m.ScriptsPath = scriptsPath
265283
res := &repo.DatasetRef{}
266284
return m.Run(job, res)
267285
}

0 commit comments

Comments
 (0)