From f580f0220d66f819f8af7faadbce11d6784fc515 Mon Sep 17 00:00:00 2001
From: Alisson Bruno <alissonbruno.sa@gmail.com>
Date: Tue, 30 May 2023 18:27:35 +0200
Subject: [PATCH] Move check for a git repository

Using the `Before` function we avoid having the same check duplicated in
all commands.
---
 cmd/git-pair/end.go   | 5 -----
 cmd/git-pair/info.go  | 5 -----
 cmd/git-pair/main.go  | 9 +++++++++
 cmd/git-pair/start.go | 5 -----
 4 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/cmd/git-pair/end.go b/cmd/git-pair/end.go
index fe25bf4..0dcc2a2 100644
--- a/cmd/git-pair/end.go
+++ b/cmd/git-pair/end.go
@@ -7,11 +7,6 @@ import (
 )
 
 func End() {
-	if !git.IsGitRepo() {
-		fmt.Println("Not executed from a git repository")
-		return
-	}
-
 	err := git.DisablePairingMode()
 
 	if err != nil {
diff --git a/cmd/git-pair/info.go b/cmd/git-pair/info.go
index 5a00771..6642230 100644
--- a/cmd/git-pair/info.go
+++ b/cmd/git-pair/info.go
@@ -8,11 +8,6 @@ import (
 )
 
 func Info() {
-	if !git.IsGitRepo() {
-		fmt.Println("Not executed from a git repository")
-		return
-	}
-
 	if !git.PairingModeEnabled() {
 		fmt.Println("Pairing mode is not enabled")
 		return
diff --git a/cmd/git-pair/main.go b/cmd/git-pair/main.go
index 96754e8..de4375f 100644
--- a/cmd/git-pair/main.go
+++ b/cmd/git-pair/main.go
@@ -1,11 +1,13 @@
 package main
 
 import (
+	"errors"
 	"fmt"
 	"log"
 	"os"
 
 	"github.com/inverse/git-pair/internal/diagnostics"
+	"github.com/inverse/git-pair/internal/git"
 	"github.com/urfave/cli/v2"
 )
 
@@ -17,6 +19,13 @@ func main() {
 	app := &cli.App{
 		Version: diagnostics.Version,
 		Usage:   "A tool to make it easier for git based pairing for co-authoring commits",
+		Before: func(cCtx *cli.Context) error {
+			if !git.IsGitRepo() {
+				return errors.New("Not executed from a git repository")
+			}
+
+			return nil
+		},
 		Commands: []*cli.Command{
 			{
 				Name:    "start",
diff --git a/cmd/git-pair/start.go b/cmd/git-pair/start.go
index 10dd0fe..b717cdb 100644
--- a/cmd/git-pair/start.go
+++ b/cmd/git-pair/start.go
@@ -10,11 +10,6 @@ import (
 )
 
 func Start() {
-	if !git.IsGitRepo() {
-		fmt.Println("Not executed from a git repository")
-		return
-	}
-
 	localContributors, err := contributors.GetLocalContributors()
 	if err != nil {
 		fmt.Printf("Failed to load local contributors: %s\n", err)