-
Notifications
You must be signed in to change notification settings - Fork 29
/
addhooks.go
57 lines (48 loc) · 1.1 KB
/
addhooks.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
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: Apache-2.0
package addhooks
import (
"errors"
"fmt"
"github.com/gittuf/gittuf/internal/repository"
"github.com/spf13/cobra"
)
type options struct {
force bool
}
func (o *options) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(
&o.force,
"force",
"f",
false,
"overwrite hooks, if they already exist",
)
}
func (o *options) Run(cmd *cobra.Command, _ []string) error {
repo, err := repository.LoadRepository()
if err != nil {
return err
}
err = repo.UpdateHook(repository.HookPrePush, prePushScript, o.force)
var hookErr *repository.ErrHookExists
if errors.As(err, &hookErr) {
fmt.Fprintf(
cmd.ErrOrStderr(),
"'%s' already exists. Use --force flag or merge existing hook and the following script manually:\n\n%s\n",
string(hookErr.HookType),
prePushScript,
)
}
return err
}
func New() *cobra.Command {
o := &options{}
cmd := &cobra.Command{
Use: "add-hooks",
Short: "Add git hooks that automatically create and sync RSL",
RunE: o.Run,
DisableAutoGenTag: true,
}
o.AddFlags(cmd)
return cmd
}