arnaucube
arnaucube/wttPublic Self-hosted repository
wtt/work.go
work.goRaw
  1package main
  2
  3import (
  4	"encoding/json"
  5	"errors"
  6	"fmt"
  7	"io/ioutil"
  8	"os"
  9	"strconv"
 10	"time"
 11
 12	"github.com/fatih/color"
 13)
 14
 15type Project struct {
 16	Name    string   `json:"name"`
 17	Streaks []Streak `json:"streaks"`
 18}
 19type Streak struct {
 20	Start       time.Time     `json:"start"`
 21	End         time.Time     `json:"end"`
 22	Duration    time.Duration `json:"duration"`
 23	Description string        `json:"description"`
 24}
 25type Work struct {
 26	Projects           []Project `json:"projects"`
 27	CurrentProjectName string    `json:"currentProjectName"`
 28}
 29
 30var work Work
 31
 32func readProjects() {
 33	file, err := ioutil.ReadFile(filePath)
 34	if err != nil {
 35		//file not exists, create directory and file
 36		color.Yellow(directoryPath + " not exists, creating directory")
 37		_ = os.Mkdir(directoryPath, os.ModePerm)
 38		saveWork()
 39	}
 40	content := string(file)
 41	json.Unmarshal([]byte(content), &work)
 42}
 43
 44func saveWork() {
 45
 46	jsonProjects, err := json.Marshal(work)
 47	check(err)
 48	err = ioutil.WriteFile(filePath, jsonProjects, 0644)
 49	check(err)
 50}
 51func getProjectIByName(name string) int {
 52	for i, project := range work.Projects {
 53		if project.Name == name {
 54			return i
 55		}
 56	}
 57	return -1
 58}
 59func projectExist(name string) bool {
 60	for _, project := range work.Projects {
 61		if project.Name == name {
 62			return true
 63		}
 64	}
 65	return false
 66}
 67func newProject(name string) error {
 68	//first, check if the project name is not taken yet
 69	if projectExist(name) {
 70		color.Red("Project name: " + name + ", already exists")
 71		return errors.New("project name already exist")
 72	}
 73	var newProject Project
 74	newProject.Name = name
 75	work.Projects = append(work.Projects, newProject)
 76	return nil
 77}
 78func listProjectsDetails() {
 79	fmt.Println("")
 80	fmt.Println("")
 81	fmt.Println("Listing projects")
 82	fmt.Println("")
 83	for k, project := range work.Projects {
 84		fmt.Println("project " + strconv.Itoa(k))
 85		fmt.Print("name: ")
 86		color.Blue(project.Name)
 87		for k2, streak := range project.Streaks {
 88			fmt.Println("	streak: " + strconv.Itoa(k2))
 89			fmt.Print("	Start:")
 90			fmt.Println(streak.Start)
 91			fmt.Print("	End:")
 92			fmt.Println(streak.End)
 93			fmt.Print("	Duration:")
 94			fmt.Println(streak.Duration)
 95		}
 96		fmt.Println("")
 97		showHoursByDays(project.Name)
 98		fmt.Println("")
 99	}
100	if work.CurrentProjectName != "" {
101		fmt.Print("Current working project: ")
102		color.Blue(work.CurrentProjectName)
103	}
104}
105func listProjects() {
106	fmt.Println("")
107	fmt.Println("")
108	fmt.Println("Listing projects")
109	fmt.Println("")
110	for k, project := range work.Projects {
111		fmt.Println("project " + strconv.Itoa(k))
112		fmt.Print("name: ")
113		color.Blue(project.Name)
114		showHoursByDays(project.Name)
115		fmt.Println("")
116	}
117	if work.CurrentProjectName != "" {
118		fmt.Print("Current working project: ")
119		color.Blue(work.CurrentProjectName)
120	}
121}
122func deleteProject(name string) {
123	i := getProjectIByName(name)
124	if i < 0 {
125		color.Red("Project name: " + name + ", no exists")
126		return
127	}
128	work.Projects = append(work.Projects[:i], work.Projects[i+1:]...)
129}
130func showHoursByDays(name string) {
131	timeFormat := "Mon, 01/02/06"
132	i := getProjectIByName(name)
133	if i < 0 {
134		color.Red("Project name: " + name + ", no exists")
135		return
136	}
137	p := work.Projects[i]
138	days := make(map[string]time.Duration)
139	for _, streak := range p.Streaks {
140		days[streak.Start.Format(timeFormat)] = addDurations(days[streak.Start.Format(timeFormat)], streak.Duration)
141	}
142	for day, hours := range days {
143		fmt.Print("	Day: " + day)
144		fmt.Print(", total time: ")
145		fmt.Println(hours)
146	}
147}
148func addDurations(d1 time.Duration, d2 time.Duration) time.Duration {
149	s := d1.Seconds() + d2.Seconds()
150	r := time.Duration(s) * time.Second
151	return r
152}