Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 1b12a0c

Browse files
author
Eric Crosson
committed
Add is adding to toml idempotently
1 parent 591aabd commit 1b12a0c

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/cmd/git-ledger/add.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package main
22

33
import (
44
"fmt"
5+
"regexp"
56
"strings"
67

78
"github.com/urfave/cli"
8-
// "github.com/BurntSushi/toml"
9-
"github.com/ericcrosson/git-ledger/src/git-ledger"
9+
"github.com/git-hook/git-ledger/src/git-ledger"
1010
"github.com/codeskyblue/go-sh"
1111
)
1212

@@ -23,12 +23,14 @@ func getRemote(dir string) string {
2323
func getSlug(dir string) string {
2424
session := sh.NewSession()
2525
session.SetDir(dir)
26-
out, err := session.Command("git", "remote", "get-url", getRemote(dir)).Command("sed", "-r", "s#[^/:]*/##").Output()
26+
out, err := session.Command("git", "remote", "get-url", getRemote(dir)).Output()
2727
output := strings.TrimSpace(fmt.Sprintf("%s", out))
28+
reg := regexp.MustCompile(`[^/:]*/[^/:]*$`)
29+
res := reg.FindStringSubmatch(output)
2830
if err != nil {
2931
panic(err)
3032
}
31-
return output
33+
return res[0]
3234
}
3335

3436
func add(c *cli.Context) error {
@@ -44,5 +46,8 @@ func add(c *cli.Context) error {
4446
fmt.Println("record is ", record)
4547
fmt.Println("path is ", ledger.Path())
4648

49+
record.RemoveFromLedger()
50+
record.WriteToLedger()
51+
4752
return nil
4853
}

src/git-ledger/ledger.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,61 @@
11
package ledger
22

33
import (
4+
"os"
5+
"fmt"
46
"path"
7+
"io/ioutil"
8+
59
"github.com/jmalloc/grit/src/grit/pathutil"
10+
"github.com/BurntSushi/toml"
611
)
712

813
type Record struct {
914
Path string
1015
Slug string
1116
}
1217

18+
type Records struct {
19+
Record []Record
20+
}
21+
1322
func Path() string {
1423
home, _ := pathutil.HomeDir()
1524
return path.Join(home, ".git-ledger")
1625
}
26+
27+
func (r Record) String() string {
28+
return fmt.Sprintf("[[Record]]\npath = \"%s\"\nslug = \"%s\"\n\n", r.Path, r.Slug)
29+
}
30+
31+
func (r Record) RemoveFromLedger() {
32+
b, err := ioutil.ReadFile(Path())
33+
if err != nil {
34+
fmt.Println(err)
35+
}
36+
str := string(b)
37+
38+
var ledger Records
39+
if _, err := toml.Decode(str, &ledger); err != nil {
40+
panic(err)
41+
}
42+
43+
var content string
44+
for _, s := range ledger.Record {
45+
if s.Path != r.Path {
46+
content = content + s.String() + "\n"
47+
}
48+
}
49+
50+
ioutil.WriteFile(Path(), []byte(content), 0644)
51+
52+
}
53+
54+
func (r Record) WriteToLedger() {
55+
f, err := os.OpenFile(Path(), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
56+
if err != nil {
57+
panic(err)
58+
}
59+
defer f.Close()
60+
fmt.Fprintf(f, r.String())
61+
}

0 commit comments

Comments
 (0)