Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track logs in git #31

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var infoCmd = &cobra.Command{
log.Fatal(err)
}

fmt.Printf("Log location: %s\n", dl.ProjectPath)
fmt.Println(dl.ProjectPath)
},
}

Expand Down
39 changes: 39 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -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")
}
33 changes: 33 additions & 0 deletions internal/daylog/daylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -59,6 +60,14 @@ 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())
output, err := git.AddAndCommit(d.ProjectPath, d.Path, msg)
if err != nil {
return fmt.Errorf("%s: %s", err, output.Stderr.String())
}
}

return nil
}

Expand All @@ -76,6 +85,30 @@ 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
output, err := git.Init(d.ProjectPath)
if err != nil {
return fmt.Errorf("%s: %s", err, output.Stderr.String())
}
fmt.Println(output.Stdout.String())

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(
Expand Down
88 changes: 88 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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")

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) (*CmdOutput, error) {
output, err := run(dir, "init")
if err != nil {
return output, err
}

return output, nil
}

func Add(dir, pattern string) (*CmdOutput, error) {
output, err := run(dir, "add", pattern)
if err != nil {
return output, err
}

return output, nil
}

func Commit(dir, msg string) (*CmdOutput, error) {
output, err := run(dir, "commit", "-m", msg)
if err != nil {
return output, err
}

return output, nil
}

func AddAndCommit(dir, pattern, msg string) (*CmdOutput, error) {
output, err := Add(dir, pattern)
if err != nil {
return output, err
}

output, err = Commit(dir, msg)
if err != nil {
return output, err
}

return output, nil
}

// exec a git command
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 &output, err
}

return &output, nil
}