1
1
package base
2
2
3
3
import (
4
- "context"
5
4
"fmt"
6
5
"os/exec"
7
6
"path/filepath"
@@ -13,77 +12,80 @@ import (
13
12
"github.com/qri-io/qri/cron"
14
13
)
15
14
16
- // DatasetSaveRunner returns a cron.RunFunc that invokes the "qri save" command
17
- func DatasetSaveRunner (basepath string ) cron.RunJobFunc {
18
- return func (ctx context.Context , streams ioes.IOStreams , job * cron.Job ) error {
19
- args := []string {"save" , job .Name }
15
+ // JobToCmd returns an operating system command that will execute the given job
16
+ // wiring operating system in/out/errout to the provided iostreams.
17
+ func JobToCmd (streams ioes.IOStreams , job * cron.Job ) * exec.Cmd {
18
+ switch job .Type {
19
+ case cron .JTDataset :
20
+ return datasetSaveCmd (streams , job )
21
+ case cron .JTShellScript :
22
+ return shellScriptCmd (streams , job )
23
+ default :
24
+ return nil
25
+ }
26
+ }
20
27
21
- if o , ok := job .Options .(* cron.DatasetOptions ); ok {
22
- if o .Title != "" {
23
- args = append (args , fmt .Sprintf (`--title="%s"` , o .Title ))
24
- }
25
- if o .Message != "" {
26
- args = append (args , fmt .Sprintf (`--message="%s"` , o .Message ))
27
- }
28
- if o .Recall != "" {
29
- args = append (args , fmt .Sprintf (`--recall="%s"` , o .Recall ))
30
- }
31
- if o .BodyPath != "" {
32
- args = append (args , fmt .Sprintf (`--body="%s"` , o .BodyPath ))
33
- }
34
- if len (o .FilePaths ) > 0 {
35
- for _ , path := range o .FilePaths {
36
- args = append (args , fmt .Sprintf (`--file="%s"` , path ))
37
- }
28
+ // datasetSaveCmd configures a "qri save" command based on job details
29
+ // wiring operating system in/out/errout to the provided iostreams.
30
+ func datasetSaveCmd (streams ioes.IOStreams , job * cron.Job ) * exec.Cmd {
31
+ args := []string {"save" , job .Name }
32
+
33
+ if o , ok := job .Options .(* cron.DatasetOptions ); ok {
34
+ if o .Title != "" {
35
+ args = append (args , fmt .Sprintf (`--title="%s"` , o .Title ))
36
+ }
37
+ if o .Message != "" {
38
+ args = append (args , fmt .Sprintf (`--message="%s"` , o .Message ))
39
+ }
40
+ if o .Recall != "" {
41
+ args = append (args , fmt .Sprintf (`--recall="%s"` , o .Recall ))
42
+ }
43
+ if o .BodyPath != "" {
44
+ args = append (args , fmt .Sprintf (`--body="%s"` , o .BodyPath ))
45
+ }
46
+ if len (o .FilePaths ) > 0 {
47
+ for _ , path := range o .FilePaths {
48
+ args = append (args , fmt .Sprintf (`--file="%s"` , path ))
38
49
}
50
+ }
39
51
40
- // TODO (b5) - config and secrets
52
+ // TODO (b5) - config and secrets
41
53
42
- boolFlags := map [string ]bool {
43
- "--publish" : o .Publish ,
44
- "--strict" : o .Strict ,
45
- "--force" : o .Force ,
46
- "--keep-format" : o .ConvertFormatToPrev ,
47
- "--no-render" : ! o .ShouldRender ,
48
- }
49
- for flag , use := range boolFlags {
50
- if use {
51
- args = append (args , flag )
52
- }
54
+ boolFlags := map [string ]bool {
55
+ "--publish" : o .Publish ,
56
+ "--strict" : o .Strict ,
57
+ "--force" : o .Force ,
58
+ "--keep-format" : o .ConvertFormatToPrev ,
59
+ "--no-render" : ! o .ShouldRender ,
60
+ }
61
+ for flag , use := range boolFlags {
62
+ if use {
63
+ args = append (args , flag )
53
64
}
54
-
55
65
}
56
66
57
- cmd := exec .Command ("qri" , args ... )
58
- // cmd.Dir = basepath
59
- cmd .Stderr = streams .ErrOut
60
- cmd .Stdout = streams .Out
61
- cmd .Stdin = streams .In
62
- return cmd .Run ()
63
67
}
68
+
69
+ cmd := exec .Command ("qri" , args ... )
70
+ // cmd.Dir = basepath
71
+ cmd .Stderr = streams .ErrOut
72
+ cmd .Stdout = streams .Out
73
+ cmd .Stdin = streams .In
74
+ return cmd
64
75
}
65
76
66
- // LocalShellScriptRunner creates a script runner anchored at a local path
67
- // The runner it wires operating sytsem command in/out/errour to the iostreams
68
- // provided by RunJobFunc. All paths are in relation to the provided base path
77
+ // shellScriptCmd creates an exec.Cmd, wires operating system in/out/errout
78
+ // to the provided iostreams.
69
79
// Commands are executed with access to the same enviornment variables as the
70
80
// process the runner is executing in
71
- // The executing command blocks until completion
72
- func LocalShellScriptRunner (basepath string ) cron.RunJobFunc {
73
- return func (ctx context.Context , streams ioes.IOStreams , job * cron.Job ) error {
74
- path := job .Name
75
- if qfs .PathKind (job .Name ) == "local" {
76
- // TODO (b5) - need to first check that path can't be found
77
- // path = filepath.Join(basepath, path)
78
- }
79
-
80
- cmd := exec .Command (path )
81
- // cmd.Dir = basepath
82
- cmd .Stderr = streams .ErrOut
83
- cmd .Stdout = streams .Out
84
- cmd .Stdin = streams .In
85
- return cmd .Run ()
86
- }
81
+ func shellScriptCmd (streams ioes.IOStreams , job * cron.Job ) * exec.Cmd {
82
+ // TODO (b5) - config and secrets as env vars
83
+
84
+ cmd := exec .Command (job .Name )
85
+ cmd .Stderr = streams .ErrOut
86
+ cmd .Stdout = streams .Out
87
+ cmd .Stdin = streams .In
88
+ return cmd
87
89
}
88
90
89
91
// PossibleShellScript checks a path to see if it might be a shell script
@@ -109,10 +111,11 @@ func DatasetToJob(ds *dataset.Dataset, periodicity string, opts *cron.DatasetOpt
109
111
110
112
job = & cron.Job {
111
113
// TODO (b5) - dataset.Dataset needs an Alias() method:
112
- Name : fmt .Sprintf ("%s/%s" , ds .Peername , ds .Name ),
113
- Periodicity : p ,
114
- Type : cron .JTDataset ,
115
- LastRun : ds .Commit .Timestamp ,
114
+ Name : fmt .Sprintf ("%s/%s" , ds .Peername , ds .Name ),
115
+ Periodicity : p ,
116
+ Type : cron .JTDataset ,
117
+ LastRunStart : ds .Commit .Timestamp ,
118
+ LastRunStop : ds .Commit .Timestamp ,
116
119
}
117
120
if opts != nil {
118
121
job .Options = opts
0 commit comments