You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.3 KiB

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