-
Notifications
You must be signed in to change notification settings - Fork 3
/
checkout.go
51 lines (43 loc) · 1.17 KB
/
checkout.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
package cli
import (
"github.com/knoebber/dotfile/dotfile"
"github.com/knoebber/usererror"
"gopkg.in/alecthomas/kingpin.v2"
)
type checkoutCommand struct {
alias string
commitHash string
force bool
}
func (c *checkoutCommand) run(*kingpin.ParseContext) error {
s, err := loadFile(c.alias)
if err != nil {
return err
}
if c.commitHash == "" {
c.commitHash = s.FileData.Revision
}
if !c.force {
clean, err := dotfile.IsClean(s, c.commitHash)
if err != nil {
return err
}
if !clean {
return usererror.Format("%q has uncommitted changes, use -f to override", c.alias)
}
}
if err := dotfile.Checkout(s, c.commitHash); err != nil {
return err
}
return nil
}
func addCheckoutSubCommandToApplication(app *kingpin.Application) {
cc := new(checkoutCommand)
c := app.Command("checkout", "revert a file to a previously committed state").Action(cc.run)
c.Arg("alias", "name of file to revert changes in").
HintAction(flags.defaultAliasList).
Required().
StringVar(&cc.alias)
c.Arg("commit-hash", "the revision to revert to").StringVar(&cc.commitHash)
c.Flag("force", "revert a file with uncommitted changes").Short('f').BoolVar(&cc.force)
}