Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
snturk committed Jan 6, 2024
0 parents commit dd02b23
Show file tree
Hide file tree
Showing 13 changed files with 526 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# Go workspace files
.idea/

# vscode
.vscode/

# DS_Store
.DS_Store

# Time entries for Timmy
.timmy.entries/
46 changes: 46 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "timmy",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.timmy.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
43 changes: 43 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright © 2024 Muratcan Senturk <[email protected]>
*/
package cmd

import (
"fmt"
"github.com/snturk/timmy/internal/service"

"github.com/spf13/cobra"
)

// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "Start the time tracker",
Long: `This will start the time tracker with provided task name.`,
Run: func(cmd *cobra.Command, args []string) {
// Task name is required
if len(args) < 1 {
fmt.Println("Task name is required")
return
}

// Get task name from user
taskName := args[0]

err := service.StartTimeEntry(taskName)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Starting time tracker for task: ", taskName)
},
}

func init() {
rootCmd.AddCommand(startCmd)

// Get task name from user
startCmd.Flags().StringP("task", "t", "", "Task name")
}
35 changes: 35 additions & 0 deletions cmd/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"github.com/snturk/timmy/internal/service"
"github.com/spf13/cobra"
)

// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print("Stopping time tracker... ")
err := service.StopTimeEntry()
if err != nil {
fmt.Errorf("error while stopping time entry: %v", err)
return
}
fmt.Println("Done.")
},
}

func init() {
rootCmd.AddCommand(stopCmd)
}
34 changes: 34 additions & 0 deletions cmd/today.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"
"github.com/snturk/timmy/internal/service"
"github.com/spf13/cobra"
)

// todayCmd represents the today command
var todayCmd = &cobra.Command{
Use: "today",
Short: "A brief description of your daily time entries",
Long: `A brief description of your daily time entries. For example:
Your daily time entries:
- Task 1: 1h 30m
- Task 2: 2h 15m
- Task 3: 3h 45m
Total: 7h 30m
`,
Run: func(cmd *cobra.Command, args []string) {
err := service.PrintTodayBrief()
if err != nil {
fmt.Errorf("error while printing today brief: %v", err)
return
}
},
}

func init() {
rootCmd.AddCommand(todayCmd)
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/snturk/timmy

go 1.19

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 6 additions & 0 deletions internal/constants/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package constants

const (
DateTimeFormat = "2006-01-02 15:04:05"
FileDataDivider = " | "
)
67 changes: 67 additions & 0 deletions internal/model/time-entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package model

import (
"bytes"
"github.com/snturk/timmy/internal/constants"
"strconv"
"time"
)

// TimeEntry represents a single time entry.
type TimeEntry struct {
// The start date and time of the entry.
Start time.Time `json:"start"`
// The end date and time of the entry, if it has ended.
End time.Time `json:"end",omitempty`
// The task that the entry is for.
Task string `json:"task"`
// Is the entry currently running?
Running bool `json:"running"`
}

// String returns a string representation of the time entry.
func (entry TimeEntry) String() string {
// 'Start' - 'End' - 'Task' - 'Running'
var entryString bytes.Buffer
entryString.WriteString(entry.Start.Format(constants.DateTimeFormat))
entryString.WriteString(constants.FileDataDivider)
entryString.WriteString(entry.End.Format(constants.DateTimeFormat))
entryString.WriteString(constants.FileDataDivider)
entryString.WriteString(entry.Task)
entryString.WriteString(constants.FileDataDivider)
entryString.WriteString(strconv.FormatBool(entry.Running))

return entryString.String()
}

// ParseTimeEntry parses a string representation of a time entry.
func ParseTimeEntry(entryString string) (TimeEntry, error) {
var entry TimeEntry
var err error

// 'Start' - 'End' - 'Task' - 'Running'
entryData := bytes.Split([]byte(entryString), []byte(constants.FileDataDivider))

// Parse the start time.
entry.Start, err = time.Parse(constants.DateTimeFormat, string(entryData[0]))
if err != nil {
return entry, err
}

// Parse the end time.
entry.End, err = time.Parse(constants.DateTimeFormat, string(entryData[1]))
if err != nil {
return entry, err
}

// Parse the task.
entry.Task = string(entryData[2])

// Parse the running status.
entry.Running, err = strconv.ParseBool(string(entryData[3]))
if err != nil {
return entry, err
}

return entry, nil
}
Loading

0 comments on commit dd02b23

Please sign in to comment.