forked from skelterjohn/gorf
/
gorf.go
108 lines (89 loc) · 1.92 KB
/
gorf.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2011 John Asmuth. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"io"
"strings"
"path/filepath"
"os"
"fmt"
"flag"
"gonicetrace.googlecode.com/hg/nicetrace"
)
var (
LocalRoot = "."
Usage bool
)
var UsageText = `Usage: gorf [flags] <command>
commands:
changes
clear
move <old path> <new path> [<name>+]
moveall <old path> <new path>
merge <old path> <new path>
pkg <path> <new name>
rename <path> [<type>.]<old name> <new name>
scan <path>
undo
`
func MakeErr(format string, args ...interface{}) (os.Error) {
return os.NewError(fmt.Sprintf(format, args...))
}
func main() {
defer nicetrace.Print()
var err os.Error
erf := func(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
}
defer func() {
if err != nil {
erf("%v\n", err)
}
}()
flag.StringVar(&LocalRoot, "r", ".", "Local package root")
flag.BoolVar(&Usage, "?", false, "Print usage and quit")
flag.Parse()
norollCmds := map[string]func([]string) os.Error {
"undo" : UndoCmd,
"scan" : ScanCmd,
"changes" : ChangesCmd,
"clear" : ClearCmd,
}
rollCmds := map[string]func([]string) os.Error {
"pkg" : PkgCmd,
"rename" : RenameCmd,
"move" : MoveCmd,
"moveall" : MoveAllCmd,
"merge" : MergeCmd,
}
foo, ok := norollCmds[flag.Arg(0)]
if ok && Usage {
fmt.Print(Help(flag.Arg(0)))
return
}
if !ok {
foo, ok = rollCmds[flag.Arg(0)]
err = RollbackUndos()
if err != nil {
return
}
if ok {
var out io.Writer
out, err = os.Create(filepath.Join(LocalRoot, ".change.0.gorfc"))
if err != nil {
return
}
cmdline := strings.Join(flag.Args(), " ")
fmt.Fprintf(out, "%s\n", cmdline)
}
//out.Close()
}
if !ok || Usage || len(flag.Args()) == 0 {
erf(UsageText)
erf("flags\n")
flag.PrintDefaults()
return
}
err = foo(flag.Args()[1:])
}