Skip to content

Commit 9b13394

Browse files
authored
Add 'src action create' cmd to create empty action definition (#172)
* Add 'src action create' cmd to create empty action definition This fixes #153 but depends on #171. * Allow $schema property in Action Definition
1 parent a1844ed commit 9b13394

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

cmd/src/actions_create.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
)
9+
10+
const actionDefinitionTemplate = `{
11+
"$schema": "https://github.com/sourcegraph/src-cli/tree/master/schema/actions.schema.json",
12+
"scopeQuery": "",
13+
"steps": [
14+
]
15+
}
16+
`
17+
18+
func init() {
19+
usage := `
20+
Create an empty action definition in action.json (if not -o flag is given). This command is meant to help with creating action definitions to be used with 'src actions exec'.
21+
22+
Examples:
23+
24+
Create a new action definition in action.json:
25+
26+
$ src actions create
27+
28+
Create a new action definition in ~/Documents/my-action.json:
29+
30+
$ src actions create -o ~/Documents/my-action.json
31+
`
32+
33+
flagSet := flag.NewFlagSet("create", flag.ExitOnError)
34+
usageFunc := func() {
35+
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src actions %s':\n", flagSet.Name())
36+
flagSet.PrintDefaults()
37+
fmt.Println(usage)
38+
}
39+
40+
var (
41+
fileFlag = flagSet.String("o", "action.json", "The destination file name. Default value is 'action.json'")
42+
)
43+
44+
handler := func(args []string) error {
45+
flagSet.Parse(args)
46+
47+
if _, err := os.Stat(*fileFlag); !os.IsNotExist(err) {
48+
return fmt.Errorf("file %q already exists", *fileFlag)
49+
}
50+
51+
return ioutil.WriteFile(*fileFlag, []byte(actionDefinitionTemplate), 0644)
52+
}
53+
54+
// Register the command.
55+
actionsCommands = append(actionsCommands, &command{
56+
flagSet: flagSet,
57+
handler: handler,
58+
usageFunc: usageFunc,
59+
})
60+
}

schema/actions.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"steps"
1212
],
1313
"properties": {
14+
"$schema": {
15+
"description": "URL of the JSON Schema for an Action Definition.",
16+
"type": "string",
17+
"minLength": 1
18+
},
1419
"scopeQuery": {
1520
"description": "A Sourcegraph search query to generate a list of repositories over which to run the action. Use 'src actions scope-query' to see which repositories are matched by the query.",
1621
"type": "string",

0 commit comments

Comments
 (0)