/
main.go
504 lines (427 loc) · 12.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/zmarcantel/hearth/config"
"github.com/zmarcantel/hearth/repository"
"github.com/zmarcantel/hearth/repository/pkg"
"github.com/codegangsta/cli"
git "gopkg.in/libgit2/git2go.v23"
)
func main() {
app := init_flags()
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
//==================================================
// default action
//==================================================
// TODO: what should this be?
func action_default(ctx *cli.Context) {
repo, err := repository.Open()
if err != nil {
log.Fatal(err)
}
index, err := repo.Index()
if err != nil {
log.Fatalf("could not get index: %s", err.Error())
}
defer index.Free()
diff_opts := git.DiffOptions{
Flags: git.DiffNormal,
IgnoreSubmodules: git.SubmoduleIgnoreAll,
Pathspec: []string{repo.Path},
ContextLines: 4,
}
diff, err := repo.DiffIndexToWorkdir(index, &diff_opts)
if err != nil {
log.Fatalf("could not diff index to workdir: %s", err.Error())
}
defer diff.Free()
err = diff.ForEach(func(delta git.DiffDelta, idk float64) (git.DiffForEachHunkCallback, error) {
// TODO: is the float percentage changed?
// if rename
if delta.OldFile.Path != delta.NewFile.Path {
// TODO: colored output based on delta.Similarity
fmt.Printf("[ renamed ] %s --> %s\n", delta.OldFile.Path, delta.NewFile.Path)
} else {
fmt.Printf("[ changed ] %s", delta.NewFile.Path)
}
return nil, nil
}, git.DiffDetailFiles)
if err != nil {
log.Fatalf("could not iterate diffs: %s", err.Error())
}
}
//==================================================
// init action
//==================================================
func action_init(ctx *cli.Context) {
// get the default repo path (overwritten below) and the forced config path
repo_path := repository.DefaultPath()
config_final_path := config.Path()
// if the user wants a different repo, let them
if ctx.IsSet("repo") {
repo_path = opts.RepoPath
}
// create the repo (and starter config + any misc files)
repo, err := repository.Create(repo_path, opts.RepoOrigin)
if err != nil {
log.Fatalf("could not initialize repository: %s", err.Error())
}
defer repo.Free()
// symlink {REPO_DIR}/.hearthrc --> $HOME/.hearthrc
config_src_path := path.Join(repo.Path, config.Name)
if err := os.Symlink(config_src_path, config_final_path); err != nil {
log.Fatalf("could not link hearth config into home directory: %s", err.Error())
}
}
//==================================================
// env action
//==================================================
func action_env(ctx *cli.Context) {
if len(ctx.Args()) < 1 {
log.Fatalf("no branch name given")
} else if len(ctx.Args()) > 1 {
log.Fatalf("too many branch names given")
}
repo, err := repository.Open()
if err != nil {
log.Fatal(err)
}
// TODO: pull first to get new branches
branch_name := ctx.Args()[0]
branch, err := repo.LookupBranch(branch_name, git.BranchLocal)
if err != nil && ctx.Bool("no-create") == false {
// no branch so make one
branch, err = repo.NewBranch(branch_name)
if err != nil {
log.Fatal(err)
}
}
defer branch.Free()
err = repo.CheckoutBranch(branch)
if err != nil {
log.Fatalf("could not checkout branch: %s", err.Error())
}
}
//==================================================
// create action
//==================================================
func action_create_package(ctx *cli.Context) {
// check our args are in bound, break them out in vars for later flexibility
max_args := 1
num_args := len(ctx.Args())
if num_args > max_args {
log.Fatalf("too many arguments/packages (%d), max=%d", len(ctx.Args()), max_args)
} else if num_args == 0 {
log.Fatal("no package name provided")
}
// get the config to read the repo directory
repo, err := repository.Open()
if err != nil {
log.Fatalf("could not open repository: %s", err.Error())
}
// check the package does not already exist
package_name := ctx.Args()[0]
package_path := path.Join(repo.Path, package_name)
if strings.HasPrefix(package_path, "~/") {
package_path = path.Join(os.Getenv("HOME"), package_path[2:])
}
if _, err := os.Stat(package_name); err == nil {
}
// create the dir
err = os.Mkdir(package_path, 0755) // TODO: right perms?
if err != nil {
log.Fatalf("could not create package: %s", err.Error())
}
new_pkg := pkg.Info{
Name: package_name,
}
install_target := ctx.String("target")
cmd := ctx.String("cmd")
pre_cmd := ctx.String("pre")
post_cmd := ctx.String("post")
// check if we were given a target (mutually exclusive with install commands
// if not a target option, check for install commands
if install_target != "" {
home_path := os.Getenv("HOME")
if strings.HasPrefix(install_target, home_path) {
new_pkg.Target = path.Join("~", install_target[len(home_path):])
} else {
new_pkg.Target = install_target
}
} else if cmd != "" {
new_pkg.InstallCmd.Cmd = cmd
if pre_cmd != "" {
new_pkg.InstallCmd.PreCmd = pre_cmd
}
if post_cmd != "" {
new_pkg.InstallCmd.PostCmd = post_cmd
}
}
// add package to config's map and write it out
// no state to cleanup on disk if writing config fails
repo.Config.Packages[package_name] = new_pkg
if err := repo.Config.Write(path.Join(repo.Path, config.Name)); err != nil {
log.Fatalf("could not write config after adding package: %s", err.Error())
}
// create a file if asked
if file_name := ctx.String("file"); file_name != "" {
file_path := path.Join(package_path, file_name)
f, err := os.Create(file_path)
if err != nil {
log.Fatalf("could not create initial file: %s", file_path)
}
// if we need to make exec...
if ctx.Bool("exec") {
// get current perms. could be os dependent on creation
s, err := f.Stat()
if err != nil {
log.Fatalf("error getting file data after creation: %s", err.Error())
}
// OR the existing mode with executable for all users
// TODO: for all users?
if err := f.Chmod(s.Mode() | os.FileMode(0111)); err != nil {
log.Fatalf("could not makefile executable: %s", err.Error())
}
}
if ctx.Bool("edit") {
editor := os.ExpandEnv("$EDITOR")
if len(editor) > 0 {
editor_path, err := exec.LookPath(editor)
if err != nil {
log.Fatalf("could not find %s: %s", editor, err.Error())
}
cmd := exec.Command(editor_path, file_path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatalf("error while running %s: %s", editor_path, err.Error())
}
} else {
log.Printf("WARN: no default editor in environment -- skipping.")
}
}
}
}
//==================================================
// remove action
//==================================================
func action_remove_package(ctx *cli.Context) {
conf, err := config.Open()
if err != nil {
log.Fatal(err)
}
args := ctx.Args()
if len(args) == 0 {
log.Fatalf("no package name given.")
}
for _, p := range args {
if _, exists := conf.Packages[p]; exists == false {
log.Printf("pakage %s does not exist", p)
continue
}
dir := path.Join(conf.BaseDirectory, p)
err = os.RemoveAll(dir)
if err != nil {
log.Fatal(err)
}
delete(conf.Packages, p)
}
if err := conf.Write(config.Path()); err != nil {
log.Fatal(err)
}
}
//==================================================
// modify action
//==================================================
func action_modify_package(ctx *cli.Context) {
conf, err := config.Open()
if err != nil {
log.Fatal(err)
}
args := ctx.Args()
if len(args) == 0 {
log.Fatalf("no package name given.")
}
for _, p := range args {
pack, exists := conf.Packages[p]
if exists == false {
log.Printf("pakage %s does not exist", p)
continue
}
target := ctx.String("target")
cmd := ctx.String("cmd")
pre_cmd := ctx.String("pre")
post_cmd := ctx.String("post")
if len(target) > 0 {
home_path := os.Getenv("HOME")
if strings.HasPrefix(target, home_path) {
pack.Target = path.Join("~", target[len(home_path):])
} else {
pack.Target = target
}
} else if cmd != "" {
pack.InstallCmd.Cmd = cmd
if pre_cmd != "" {
pack.InstallCmd.PreCmd = pre_cmd
}
if post_cmd != "" {
pack.InstallCmd.PostCmd = post_cmd
}
}
conf.Packages[p] = pack
}
if err := conf.Write(config.Path()); err != nil {
log.Fatal(err)
}
}
//==================================================
// install action
//==================================================
func action_install(ctx *cli.Context) {
args := ctx.Args()
if len(args) == 0 {
log.Fatalf("no package name given.")
}
repo, err := repository.Open()
if err != nil {
log.Fatal(err)
}
for _, p := range args {
pack, exists := repo.GetPackage(p)
if exists == false {
log.Fatalf("cannot install unknown package: %s", p)
}
fmt.Printf("[ install ] %s to ", p)
err := pack.Install(path.Join(repo.Path, p))
if err != nil {
log.Fatal(err) // TODO: allow skipping errors
}
}
}
//==================================================
// update action
//==================================================
func action_update(ctx *cli.Context) {
args := ctx.Args()
if len(args) == 0 {
log.Fatalf("no package name given.")
}
repo, err := repository.Open()
if err != nil {
log.Fatal(err)
}
for _, p := range args {
pack, exists := repo.GetPackage(p)
if exists == false {
log.Fatalf("cannot update unknown package: %s", p)
}
fmt.Printf("[ update ] %s... ", p)
err := pack.Update(path.Join(repo.Path, p))
if err != nil {
log.Fatal(err) // TODO: allow skipping errors
}
fmt.Printf("done!\n")
}
}
//==================================================
// pull action
//==================================================
func action_pull(ctx *cli.Context) {
repo, err := repository.Open()
if err != nil {
log.Fatal(err)
}
defer repo.Free()
err = repo.Pull()
if err != nil {
log.Fatal(err)
}
// get what was changed so we can run callbacks
changed, err := repo.ChangedInLastCommit()
if err != nil {
log.Fatal(err)
}
cache := make(map[string]bool)
// iterate them
for _, changed_path := range changed {
// get the relpath of the path that was changed
pkg_name, err := filepath.Rel(repo.Path, changed_path)
if err != nil {
continue
}
// this relpath may be a file or down deep in the tree
// but strip it down to the package name
pkg_name = filepath.SplitList(pkg_name)[0]
pkg_name = strings.TrimRight(pkg_name, "/") // TODO: get generic separator
// skip if we have run the commands for this package already
if _, cached := cache[pkg_name]; cached {
continue
}
cache[pkg_name] = true
// skip this if not a package
pack, exists := repo.GetPackage(pkg_name)
if exists == false {
continue
}
// take either install or update action based on the created or
// modified status of the package in the commit
if repo.CreatedInLast(changed_path) && ctx.IsSet("install") {
err := pack.Install(path.Join(repo.Path, pkg_name))
if err != nil {
// TODO: give arg to not fatal on error
log.Fatal(err)
}
} else if ctx.IsSet("upgrade") {
err := pack.Update(path.Join(repo.Path, pkg_name))
if err != nil {
// TODO: give arg to not fatal on error
log.Fatal(err)
}
}
}
}
//==================================================
// upgrade action
//==================================================
func action_upgrade(ctx *cli.Context) {
panic("upgrade command not currently supported. Will be included after PR: https://github.com/codegangsta/cli/pull/234")
}
//==================================================
// save action
//==================================================
func action_save(ctx *cli.Context) {
repo, err := repository.Open()
if err != nil {
log.Fatal(err)
}
defer repo.Free()
msg := ctx.String("message")
c, err := repo.CommitAll(msg)
if err != nil {
log.Fatal(err)
}
defer c.Free()
if ctx.IsSet("no-push") == false {
err = repo.Push("master") // TODO: not only master
if err != nil {
log.Fatal(err)
}
}
}
//==================================================
// tag action
//==================================================
func action_tag(ctx *cli.Context) {
panic("tag command not implemented")
}