forked from cavaliercoder/y10k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (165 loc) · 4.18 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"errors"
"fmt"
"github.com/urfave/cli"
"os"
"os/signal"
)
var (
QuietMode bool
DebugMode bool
YumfilePath string
LogFilePath string
TmpBasePath string
TmpYumConfPath string
TmpYumLogFile string
TmpYumCachePath string
)
func main() {
// ensure logfile handle gets cleaned up
defer CloseLogFile()
// route request
app := cli.NewApp()
app.Name = "y10k"
app.Version = "0.3.0"
app.Author = "Ryan Armstrong"
app.Email = "ryan@cavaliercoder.com"
app.Usage = "simplified yum mirror management"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "logfile, l",
Usage: "redirect output to a log file",
EnvVar: "Y10K_LOGFILE",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "less verbose",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "print debug output",
EnvVar: "Y10K_DEBUG",
},
cli.StringFlag{
Name: "tmppath, t",
Usage: "path to y10k temporary objects",
Value: "/tmp/y10k",
EnvVar: "Y10K_TMPPATH",
},
}
app.Commands = []cli.Command{
{
Name: "yumfile",
Usage: "work with a Yumfile",
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Usage: "path to Yumfile",
Value: "./Yumfile",
},
},
Before: func(context *cli.Context) error {
YumfilePath = context.String("file")
return nil
},
Subcommands: []cli.Command{
{
Name: "validate",
Usage: "validate a Yumfile's syntax",
Action: ActionYumfileValidate,
},
{
Name: "list",
Usage: "list repositories in a Yumfile",
Action: ActionYumfileList,
},
{
Name: "sync",
Usage: "syncronize repos described in a Yumfile",
Action: ActionYumfileSync,
},
},
},
{
Name: "version",
Usage: "print the version of y10k",
Action: func(context *cli.Context) {
fmt.Printf("%s version %s\n", app.Name, app.Version)
},
},
}
app.Before = func(context *cli.Context) error {
// set globals from command line context
QuietMode = context.GlobalBool("quiet")
DebugMode = context.GlobalBool("debug")
LogFilePath = context.GlobalString("logfile")
TmpBasePath = context.GlobalString("tmppath")
TmpYumConfPath = context.GlobalString("tmppath") + "/" + "yum.conf"
TmpYumLogFile = context.GlobalString("tmppath") + "/" + "yum.log"
TmpYumCachePath = context.GlobalString("tmppath") + "/" + "cache"
// configure logging
InitLogFile()
return nil
}
// sig handler
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
Printf("Caught SIGINT/Ctrl-C. Cleaning up...\n")
if cmd != nil {
Printf("Attempting to terminate %s (PID: %d)...\n", cmd.Path, cmd.Process.Pid)
cmd.Process.Kill()
}
Printf("Exiting\n")
os.Exit(2)
}
}()
app.Run(os.Args)
}
// ActionYumfileValidate processes the 'yumfile validate' command
func ActionYumfileValidate(context *cli.Context) {
yumfile, err := LoadYumfile(YumfilePath)
PanicOn(err)
Printf("Yumfile appears valid (%d repos)\n", len(yumfile.Repos))
}
// ActionYumfileList processes the 'yumfile list' command
func ActionYumfileList(context *cli.Context) {
yumfile, err := LoadYumfile(YumfilePath)
PanicOn(err)
repoCount := len(yumfile.Repos)
padding := (len(fmt.Sprintf("%d", repoCount)) * 2) + 1
for i, repo := range yumfile.Repos {
Printf("%*s %s -> %s\n", padding, fmt.Sprintf("%d/%d", i+1, repoCount), repo.ID, repo.LocalPath)
}
}
// ActionYumfileSync processes the 'yumfile sync' command
func ActionYumfileSync(context *cli.Context) {
yumfile, err := LoadYumfile(YumfilePath)
PanicOn(err)
repo := context.Args().First()
if repo == "" {
// sync/update all repos in Yumfile
if err := yumfile.SyncAll(); err != nil {
Fatalf(err, "Error running Yumfile")
}
} else {
// sync/update one repo in the Yumfile
mirror := yumfile.GetRepoByID(repo)
if mirror == nil {
Fatalf(nil, "No such repo found in Yumfile: %s", repo)
}
if err := yumfile.Sync([]Repo{*mirror}); err != nil {
Fatalf(err, "Error syncronizing repo '%s'", mirror.ID)
}
}
}
func PanicOn(err error) {
if err != nil {
Fatalf(err, "Fatal error")
}
}
func NewErrorf(format string, a ...interface{}) error {
return errors.New(fmt.Sprintf(format, a...))
}