/
cmd_script_config.go
45 lines (38 loc) · 1.25 KB
/
cmd_script_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package edit
import (
"fmt"
"io"
"io/fs"
"github.com/9seconds/chore/internal/cli/completions"
"github.com/9seconds/chore/internal/cli/validators"
"github.com/9seconds/chore/internal/paths"
"github.com/9seconds/chore/internal/script"
"github.com/spf13/cobra"
)
func NewScriptConfig() *cobra.Command {
return &cobra.Command{
Use: "script-config namespace script",
Aliases: []string{"c"},
Short: "Edit chore script configuration TOML",
ValidArgsFunction: completions.CompleteNamespaceScript,
Args: cobra.MatchAll(
cobra.ExactArgs(2), //nolint: gomnd
validators.Script(0, 1),
),
Run: main(func(args []string, content io.Writer) (string, fs.FileMode, error) {
namespace, _ := script.ExtractRealNamespace(args[0])
scr := &script.Script{
Namespace: namespace,
Executable: args[1],
}
if err := script.EnsureDir(paths.ConfigNamespace(namespace)); err != nil {
return "", 0, fmt.Errorf("cannot ensure namespace dir: %w", err)
}
tpl := getTemplate("static/edit-script-config-template.toml")
if err := tpl.Execute(content, scr); err != nil {
return "", 0, fmt.Errorf("cannot render default template: %w", err)
}
return scr.ConfigPath(), ConfigDefaultPermission, nil
}),
}
}