Skip to content

Commit

Permalink
Replaced panics with fatal exits
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Aug 4, 2019
1 parent 824c833 commit 4f40c1f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ var (
password = flag.String("password", "", "password used for auth")
)

func fatal(format string, a ...interface{}) {
fmt.Printf(format, a...)
os.Exit(1)
}

func gitAdd(w *git.Worktree, path string) error {
log.Printf("Adding file to work-tree: %s\n", path)
_, err := w.Add(path)
Expand Down Expand Up @@ -112,32 +117,32 @@ func main() {

timeout, err := time.ParseDuration(*interval)
if err != nil {
panic(err)
fatal("cannot parse interval: %s\n", err)
}
auth, err := parseAuthArgs()
if err != nil {
panic(err)
fatal("cannot parse key: %s\n", err)
}

path := flag.Args()[0]
r, err := git.PlainOpen(path)
if err != nil {
panic(err)
fatal("cannot open repository: %s\n", err)
}
w, err := r.Worktree()
if err != nil {
panic(err)
fatal("cannot access repository: %s\n", err)
}

for {
log.Println("Checking repository:", path)
err = gitPull(r, w, auth)
if err != nil {
panic(err)
fatal("cannot pull from repository: %s\n", err)
}
status, err := w.Status()
if err != nil {
panic(err)
fatal("cannot retrieve git status: %s\n", err)
}

changes := 0
Expand All @@ -149,7 +154,7 @@ func main() {
log.Printf("New file detected: %s\n", path)
err := gitAdd(w, path)
if err != nil {
panic(err)
fatal("cannot add file: %s\n", err)
}

msg += fmt.Sprintf("Add %s.\n", path)
Expand All @@ -159,7 +164,7 @@ func main() {
log.Printf("Modified file detected: %s\n", path)
err := gitAdd(w, path)
if err != nil {
panic(err)
fatal("cannot add file: %s\n", err)
}

msg += fmt.Sprintf("Update %s.\n", path)
Expand All @@ -169,7 +174,7 @@ func main() {
log.Printf("Deleted file detected: %s\n", path)
err := gitRemove(w, path)
if err != nil {
panic(err)
fatal("cannot remove file: %s\n", err)
}

msg += fmt.Sprintf("Remove %s.\n", path)
Expand All @@ -182,11 +187,11 @@ func main() {
} else {
err = gitCommit(w, msg)
if err != nil {
panic(err)
fatal("cannot commit: %s\n", err)
}
err = gitPush(r, auth)
if err != nil {
panic(err)
fatal("cannot push: %s\n", err)
}
}

Expand Down

0 comments on commit 4f40c1f

Please sign in to comment.