From 1ec6223ea6a20154c69fd827dd677e3bb65cf867 Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Sun, 21 Jan 2024 22:20:30 -0800 Subject: [PATCH 1/6] init repo init repo --- cmd/init.go | 39 ++++++++++++++++++++ internal/daylog/daylog.go | 26 ++++++++++++++ internal/git/git.go | 76 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 cmd/init.go create mode 100644 internal/git/git.go diff --git a/cmd/init.go b/cmd/init.go new file mode 100644 index 0000000..bf875e5 --- /dev/null +++ b/cmd/init.go @@ -0,0 +1,39 @@ +package cmd + +import ( + "log" + + "github.com/notnmeyer/daylog-cli/internal/daylog" + "github.com/spf13/cobra" +) + +var initCmd = &cobra.Command{ + Use: "init", + Short: "Creare a Git repository for your project.", + Long: "Creare a Git repository for your project.", + Run: func(cmd *cobra.Command, args []string) { + dl, err := daylog.New(args, config.Project) + if err != nil { + log.Fatal(err) + } + + err = dl.InitGitRepo() + if err != nil { + log.Fatal(err) + } + }, +} + +func init() { + rootCmd.AddCommand(initCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // initCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/internal/daylog/daylog.go b/internal/daylog/daylog.go index 947ef03..fe47d66 100644 --- a/internal/daylog/daylog.go +++ b/internal/daylog/daylog.go @@ -11,6 +11,7 @@ import ( "github.com/adrg/xdg" "github.com/markusmobius/go-dateparser" "github.com/notnmeyer/daylog-cli/internal/editor" + "github.com/notnmeyer/daylog-cli/internal/git" "github.com/notnmeyer/daylog-cli/internal/output-formatter" ) @@ -76,6 +77,31 @@ func (d *DayLog) Show(format string) (string, error) { return contents, nil } +func (d *DayLog) InitGitRepo() error { + // check if a repo exists for the project + if exists, err := git.RepoExists(d.ProjectPath); exists { + if err != nil { + return err + } + return fmt.Errorf("%s already appears to be a git repo\n", d.ProjectPath) + } + + // init a new repo + repo, err := git.Init(d.ProjectPath) + if err != nil { + return err + } + fmt.Printf("initialized Git repository %s\n", repo) + + // make an initial commit with any existing log files + err = git.AddAndCommit(d.ProjectPath, ".", "Initial") + if err != nil { + return err + } + + return nil +} + // returns the complete path to log file func logPath(path string, year, month, day int) (string, error) { path, err := createDir( diff --git a/internal/git/git.go b/internal/git/git.go new file mode 100644 index 0000000..2a64644 --- /dev/null +++ b/internal/git/git.go @@ -0,0 +1,76 @@ +package git + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" +) + +// returns true if dit contains a git repo +func RepoExists(dir string) (bool, error) { + gitDir := filepath.Join(dir, ".git") + + info, err := os.Stat(gitDir) + switch { + case os.IsNotExist(err): + return false, nil + case err != nil: + return false, err + case !info.IsDir(): + return false, fmt.Errorf("%s is not a directory\n", gitDir) + } + + return true, nil +} + +func Init(dir string) (string, error) { + err := runCmd("init", dir) + if err != nil { + return "", err + } + + return filepath.Join(dir, ".git"), nil +} + +func Add(dir, pattern string) error { + err := runCmd("-C", dir, "add", pattern) + if err != nil { + return err + } + + return nil +} + +func Commit(dir, msg string) error { + err := runCmd("-C", dir, "commit", "-m", msg) + if err != nil { + return err + } + + return nil +} + +func AddAndCommit(dir, pattern, msg string) error { + err := Add(dir, pattern) + if err != nil { + return err + } + + err = Commit(dir, msg) + if err != nil { + return err + } + + return nil +} + +// exec a git command +func runCmd(args ...string) error { + cmd := exec.Command("git", args...) + if err := cmd.Run(); err != nil { + return err + } + + return nil +} From 2ffed21123f55d085ba03fa53c73275d3debb6ab Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Sun, 21 Jan 2024 22:21:25 -0800 Subject: [PATCH 2/6] change initial commit message change initial commit message --- internal/daylog/daylog.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/daylog/daylog.go b/internal/daylog/daylog.go index fe47d66..eee9c31 100644 --- a/internal/daylog/daylog.go +++ b/internal/daylog/daylog.go @@ -94,7 +94,7 @@ func (d *DayLog) InitGitRepo() error { fmt.Printf("initialized Git repository %s\n", repo) // make an initial commit with any existing log files - err = git.AddAndCommit(d.ProjectPath, ".", "Initial") + err = git.AddAndCommit(d.ProjectPath, ".", "Initial commit") if err != nil { return err } From 8b6fc613926963b2c3b951e8a7f62c3eb56185fa Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Sun, 21 Jan 2024 22:46:23 -0800 Subject: [PATCH 3/6] commit changes on edit commit changes on edit --- internal/daylog/daylog.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/daylog/daylog.go b/internal/daylog/daylog.go index eee9c31..f5ff0cb 100644 --- a/internal/daylog/daylog.go +++ b/internal/daylog/daylog.go @@ -60,6 +60,11 @@ func (d *DayLog) Edit() error { return err } + if d.gitEnabled() { + msg := fmt.Sprintf("update log for %d/%d/%d\n", d.Date.Year(), int(d.Date.Month()), d.Date.Day()) + git.AddAndCommit(d.ProjectPath, d.Path, msg) + } + return nil } @@ -102,6 +107,11 @@ func (d *DayLog) InitGitRepo() error { return nil } +func (d *DayLog) gitEnabled() bool { + exists, _ := git.RepoExists(d.ProjectPath) + return exists +} + // returns the complete path to log file func logPath(path string, year, month, day int) (string, error) { path, err := createDir( From 30c008648a01648096942549ab446e4befbdcbf4 Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:09:00 -0800 Subject: [PATCH 4/6] capture stderr stdout when running git commands capture stderr stdout when running git commands --- internal/daylog/daylog.go | 16 +++++++----- internal/git/git.go | 54 ++++++++++++++++++++++++--------------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/internal/daylog/daylog.go b/internal/daylog/daylog.go index f5ff0cb..2f531bd 100644 --- a/internal/daylog/daylog.go +++ b/internal/daylog/daylog.go @@ -62,7 +62,10 @@ func (d *DayLog) Edit() error { if d.gitEnabled() { msg := fmt.Sprintf("update log for %d/%d/%d\n", d.Date.Year(), int(d.Date.Month()), d.Date.Day()) - git.AddAndCommit(d.ProjectPath, d.Path, msg) + output, err := git.AddAndCommit(d.ProjectPath, d.Path, msg) + if err != nil { + return fmt.Errorf("%s: %s", err, output.Stderr.String()) + } } return nil @@ -92,17 +95,18 @@ func (d *DayLog) InitGitRepo() error { } // init a new repo - repo, err := git.Init(d.ProjectPath) + output, err := git.Init(d.ProjectPath) if err != nil { - return err + return fmt.Errorf("%s: %s", err, output.Stderr.String()) } - fmt.Printf("initialized Git repository %s\n", repo) + fmt.Println(output.Stdout.String()) // make an initial commit with any existing log files - err = git.AddAndCommit(d.ProjectPath, ".", "Initial commit") + output, err = git.AddAndCommit(d.ProjectPath, ".", "Initial commit") if err != nil { - return err + return fmt.Errorf("%s: %s", err, output.Stderr.String()) } + fmt.Println(output.Stdout.String()) return nil } diff --git a/internal/git/git.go b/internal/git/git.go index 2a64644..9e40784 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -1,12 +1,18 @@ package git import ( + "bytes" "fmt" "os" "os/exec" "path/filepath" ) +type CmdOutput struct { + Stdout bytes.Buffer + Stderr bytes.Buffer +} + // returns true if dit contains a git repo func RepoExists(dir string) (bool, error) { gitDir := filepath.Join(dir, ".git") @@ -24,53 +30,59 @@ func RepoExists(dir string) (bool, error) { return true, nil } -func Init(dir string) (string, error) { - err := runCmd("init", dir) +func Init(dir string) (*CmdOutput, error) { + output, err := run(dir, "init") if err != nil { - return "", err + return output, err } - return filepath.Join(dir, ".git"), nil + return output, nil } -func Add(dir, pattern string) error { - err := runCmd("-C", dir, "add", pattern) +func Add(dir, pattern string) (*CmdOutput, error) { + output, err := run(dir, "add", pattern) if err != nil { - return err + return output, err } - return nil + return output, nil } -func Commit(dir, msg string) error { - err := runCmd("-C", dir, "commit", "-m", msg) +func Commit(dir, msg string) (*CmdOutput, error) { + output, err := run(dir, "commit", "-m", msg) if err != nil { - return err + return output, err } - return nil + return output, nil } -func AddAndCommit(dir, pattern, msg string) error { - err := Add(dir, pattern) +func AddAndCommit(dir, pattern, msg string) (*CmdOutput, error) { + output, err := Add(dir, pattern) if err != nil { - return err + return output, err } - err = Commit(dir, msg) + output, err = Commit(dir, msg) if err != nil { - return err + return output, err } - return nil + return output, nil } // exec a git command -func runCmd(args ...string) error { +func run(path string, args ...string) (*CmdOutput, error) { + args = append([]string{"-C", path}, args...) cmd := exec.Command("git", args...) + + var output CmdOutput + cmd.Stdout = &output.Stdout + cmd.Stderr = &output.Stderr + if err := cmd.Run(); err != nil { - return err + return &output, err } - return nil + return &output, nil } From 0808a2e6d6b9981470fc3e48a63ec0e226bb693c Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:34:25 -0800 Subject: [PATCH 5/6] info: just output the directory without a heading for ease of using it in pipes info: just output the directory without a heading for ease of using it in pipes --- cmd/info.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/info.go b/cmd/info.go index cf9a061..44bcba0 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -18,7 +18,7 @@ var infoCmd = &cobra.Command{ log.Fatal(err) } - fmt.Printf("Log location: %s\n", dl.ProjectPath) + fmt.Println(dl.ProjectPath) }, } From 0294db9c156582a54144e5c6ece9770ea9cf36dc Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:56:33 -0800 Subject: [PATCH 6/6] dont add existing files dont add existing files --- internal/daylog/daylog.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/internal/daylog/daylog.go b/internal/daylog/daylog.go index 2f531bd..4b7cfad 100644 --- a/internal/daylog/daylog.go +++ b/internal/daylog/daylog.go @@ -101,13 +101,6 @@ func (d *DayLog) InitGitRepo() error { } fmt.Println(output.Stdout.String()) - // make an initial commit with any existing log files - output, err = git.AddAndCommit(d.ProjectPath, ".", "Initial commit") - if err != nil { - return fmt.Errorf("%s: %s", err, output.Stderr.String()) - } - fmt.Println(output.Stdout.String()) - return nil }