arnaucube
arnaucube/wttPublic Self-hosted repository
wtt/main.go
main.goRaw
  1package main
  2
  3import (
  4	"fmt"
  5	"log"
  6	"os"
  7	"os/user"
  8	"time"
  9
 10	"github.com/fatih/color"
 11)
 12
 13var directoryPath string
 14var filePath string
 15
 16func main() {
 17	usr, err := user.Current()
 18	if err != nil {
 19		log.Fatal(err)
 20	}
 21	directoryPath = usr.HomeDir + "/.wtt"
 22	filePath = directoryPath + "/work.json"
 23	readProjects()
 24
 25	//get the command line parameters
 26	if len(os.Args) > 1 {
 27		switch os.Args[1] {
 28		case "new", "n":
 29			//create project os.Args[2]
 30			if len(os.Args) > 2 {
 31				projectName := os.Args[2]
 32				color.Green("creating new project: " + projectName)
 33				err := newProject(projectName)
 34				if err != nil {
 35					break
 36				}
 37				saveWork()
 38			} else {
 39				color.Red("No project name specified")
 40			}
 41			break
 42		case "list", "ls":
 43			if len(os.Args) > 2 {
 44				param := os.Args[2]
 45				if param == "-a" {
 46					listProjectsDetails()
 47				}
 48			} else {
 49				//list projects
 50				listProjects()
 51			}
 52			break
 53		case "start", "s":
 54			//check if already there is any project started
 55			if work.CurrentProjectName != "" {
 56				color.Red("Can not start project, already project " + work.CurrentProjectName + " running")
 57				break
 58			}
 59			//start project os.Args[2]
 60			if len(os.Args) > 2 {
 61				projectName := os.Args[2]
 62				i := getProjectIByName(projectName)
 63				if i < 0 {
 64					color.Red("Project name: " + projectName + ", no exists")
 65					break
 66				}
 67				var newStreak Streak
 68				newStreak.Start = time.Now()
 69				work.Projects[i].Streaks = append(work.Projects[i].Streaks, newStreak)
 70				work.CurrentProjectName = projectName
 71				saveWork()
 72				fmt.Println("starting to work in project " + work.Projects[i].Name)
 73			} else {
 74				color.Red("No project name to start selected")
 75			}
 76			break
 77		case "stop":
 78			if work.CurrentProjectName == "" {
 79				color.Red("no project started to stop")
 80				break
 81			}
 82			i := getProjectIByName(work.CurrentProjectName)
 83			if i < 0 {
 84				color.Red("Project name: " + work.CurrentProjectName + ", no exists")
 85				break
 86			}
 87			j := len(work.Projects[i].Streaks) - 1
 88			work.Projects[i].Streaks[j].End = time.Now()
 89			work.Projects[i].Streaks[j].Duration = time.Now().Sub(work.Projects[i].Streaks[j].Start)
 90			work.CurrentProjectName = ""
 91			saveWork()
 92			color.Green("Worked " + work.Projects[i].Streaks[j].Duration.String() + " in the project " + work.Projects[i].Name)
 93			break
 94		case "rm":
 95			if len(os.Args) > 2 {
 96				projectName := os.Args[2]
 97				if work.CurrentProjectName == projectName {
 98					work.CurrentProjectName = ""
 99				}
100				deleteProject(projectName)
101				saveWork()
102				color.Yellow("Project " + projectName + " deleted")
103			} else {
104				color.Red("no project name specified")
105			}
106
107			break
108		case "current", "c":
109			if work.CurrentProjectName != "" {
110				fmt.Print("Current working project: ")
111				color.Blue(work.CurrentProjectName)
112			} else {
113				fmt.Println("No current working project.")
114			}
115		case "help", "h":
116			fmt.Println("./wtt new {projectname}")
117			fmt.Println("./wtt ls")
118			fmt.Println("./wtt ls -a")
119			fmt.Println("./wtt start {projectname}")
120			fmt.Println("./wtt stop")
121			fmt.Println("./wtt rm")
122			fmt.Println("./wtt current")
123			fmt.Println("./wtt help")
124		default:
125			color.Red("option not exists")
126			os.Exit(1)
127		}
128	} else {
129		color.Red("no option selected")
130		fmt.Println("Can run 'help' for commands information")
131		fmt.Println("./wtt help")
132		os.Exit(1)
133	}
134}