diff --git a/README.md b/README.md index f1f60e5..57fe659 100644 --- a/README.md +++ b/README.md @@ -75,45 +75,67 @@ Use "gtasks [command] --help" for more information about a command. ## Commands +### Help * To see details about a command ```bash gtasks help ``` +### Login * Login ```bash gtasks login ``` + +### Tasklists * Viewing Tasklists ```bash gtasks tasklists view ``` + * Creating a Tasklist ```bash gtasks tasklists create -t 'title' gtasks tasklists create --title 'title' ``` + * Deleting a Tasklist ```bash gtasks tasklists rm ``` + +### Tasks +* To pre-select tasklist, provide it's title as follows: +```bash +gtasks tasks -l subcommand [--subcommand-flags] +``` +Examples: +```bash +gtasks tasks [--tasklist|-l] "DSC VIT" view [--include-completed | -i] +``` +**Note:** If the `-l` flag is not provided you will be able to choose a tasklist from the prompt + * Viewing tasks ```bash gtasks tasks view ``` -* Viewing completed tasks + +* Include completed tasks ```bash -gtasks tasks view -c -gtasks tasks view --completed +gtasks tasks view -i +gtasks tasks view --include-completed ``` + * Adding a task ```bash gtasks tasks add ``` + * Mark task as completed ```bash gtasks tasks done ``` + * Deleting a task ```bash gtasks tasks rm diff --git a/api/tasklists.go b/api/tasklists.go new file mode 100644 index 0000000..0b24f65 --- /dev/null +++ b/api/tasklists.go @@ -0,0 +1,54 @@ +package api + +import ( + "errors" + + "github.com/BRO3886/gtasks/internal/utils" + "google.golang.org/api/tasks/v1" +) + +type TaskList []tasks.TaskList + +func (e TaskList) Len() int { + return len(e) +} + +func (e TaskList) Less(i, j int) bool { + return e[i].Title < e[j].Title +} + +func (e TaskList) Swap(i, j int) { + e[i], e[j] = e[j], e[i] +} + +func GetTaskLists(srv *tasks.Service) ([]tasks.TaskList, error) { + r, err := srv.Tasklists.List().Do() + if err != nil { + utils.ErrorP("Unable to retrieve task lists. %v\n", err) + } + + var list []tasks.TaskList + + if len(r.Items) == 0 { + return nil, errors.New("no Tasklist found") + } + + for _, item := range r.Items { + list = append(list, *item) + } + + return list, nil +} + +func UpdateTaskList(srv *tasks.Service, tl *tasks.TaskList) (*tasks.TaskList, error) { + r, err := srv.Tasklists.Patch(tl.Id, tl).Do() + if err != nil { + return nil, err + } + return r, nil +} + +func DeleteTaskList(srv *tasks.Service, tID string) error { + err := srv.Tasklists.Delete(tID).Do() + return err +} diff --git a/internal/tasks.go b/api/tasks.go similarity index 68% rename from internal/tasks.go rename to api/tasks.go index 3faac10..9ff148a 100644 --- a/internal/tasks.go +++ b/api/tasks.go @@ -1,9 +1,9 @@ -package internal +package api import ( "errors" - "log" + "github.com/BRO3886/gtasks/internal/utils" "google.golang.org/api/tasks/v1" ) @@ -17,15 +17,26 @@ func CreateTask(srv *tasks.Service, task *tasks.Task, tasklistID string) (*tasks } //GetTasks used to retreive tasks -func GetTasks(srv *tasks.Service, id string, showCompleted bool) ([]*tasks.Task, error) { - r, err := srv.Tasks.List(id).ShowHidden(showCompleted).Do() +func GetTasks(srv *tasks.Service, id string, includeCompleted bool) ([]*tasks.Task, error) { + r, err := srv.Tasks.List(id).ShowHidden(includeCompleted).Do() if err != nil { - log.Fatalf("Unable to retrieve tasks. %v", err) + utils.ErrorP("Unable to retrieve tasks. %v", err) } if len(r.Items) == 0 { return nil, errors.New("no Tasks found") } - return r.Items, nil + + if includeCompleted { + return r.Items, nil + } else { + var list []*tasks.Task + for _, task := range r.Items { + if task.Status != "completed" { + list = append(list, task) + } + } + return list, nil + } } //GetTaskInfo to get more info about a task diff --git a/cmd/login.go b/cmd/login.go index 43f297f..8424ce7 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -4,13 +4,15 @@ import ( "context" "encoding/json" "fmt" - "log" "net/http" "os" - "github.com/BRO3886/gtasks/internal" + "github.com/BRO3886/gtasks/internal/config" + "github.com/BRO3886/gtasks/internal/utils" "github.com/spf13/cobra" "golang.org/x/oauth2" + "google.golang.org/api/option" + "google.golang.org/api/tasks/v1" ) // loginCmd represents the login command @@ -19,8 +21,8 @@ var loginCmd = &cobra.Command{ Short: "Logging into Google Tasks", Long: `This command uses the credentials.json file and makes a request to get your tokens`, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - getClient(config) + c := config.ReadCredentials() + getClient(c) }, } @@ -29,35 +31,35 @@ func init() { } // Retrieve a token, saves the token, then returns the generated client. -func getClient(config *oauth2.Config) *http.Client { +func getClient(c *oauth2.Config) *http.Client { // The file token.json stores the user's access and refresh tokens, and is // created automatically when the authorization flow completes for the first // time. - folderPath := internal.GetInstallLocation() + folderPath := config.GetInstallLocation() // fmt.Println(folderPath) tokFile := folderPath + "/token.json" tok, err := tokenFromFile(tokFile) if err != nil { - tok = getTokenFromWeb(config) + tok = getTokenFromWeb(c) saveToken(tokFile, tok) } - return config.Client(context.Background(), tok) + return c.Client(context.Background(), tok) } // Request a token from the web, then returns the retrieved token. func getTokenFromWeb(config *oauth2.Config) *oauth2.Token { authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline) - fmt.Printf("Go to the following link in your browser then type the "+ - "authorization code: \n%v\nEnter the code: ", authURL) + utils.Warn("Go to the following link in your browser then type the "+ + "authorization code: \n%v\n\nEnter the code: ", authURL) var authCode string if _, err := fmt.Scan(&authCode); err != nil { - log.Fatalf("Unable to read authorization code: %v", err) + utils.ErrorP("Unable to read authorization code: %v", err) } tok, err := config.Exchange(context.TODO(), authCode) if err != nil { - log.Fatalf("Unable to retrieve token from web: %v", err) + utils.ErrorP("Unable to retrieve token from web: %v", err) } return tok } @@ -76,11 +78,24 @@ func tokenFromFile(file string) (*oauth2.Token, error) { // Saves a token to a file path. func saveToken(path string, token *oauth2.Token) { - fmt.Printf("Saving credential file to: %s\n", path) + utils.Info("Saving credential file to: %s\n", path) f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { - log.Fatalf("Unable to cache oauth token: %v", err) + utils.ErrorP("Unable to cache oauth token: %v", err) } defer f.Close() json.NewEncoder(f).Encode(token) } + +//gets the tasks service +func getService() *tasks.Service { + c := config.ReadCredentials() + client := getClient(c) + + srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) + if err != nil { + utils.ErrorP("Unable to retrieve tasks Client %v", err) + } + + return srv +} diff --git a/cmd/root.go b/cmd/root.go index 26a9334..16f85d6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,8 +2,8 @@ package cmd import ( "fmt" - "os" + "github.com/BRO3886/gtasks/internal/utils" "github.com/spf13/cobra" homedir "github.com/mitchellh/go-homedir" @@ -16,7 +16,7 @@ var cfgFile string var rootCmd = &cobra.Command{ Use: "gtasks", Short: "A CLI Tool for Google Tasks", - Version: "0.9.2", + Version: "0.9.3", Long: ` A CLI Tool for managing your Google Tasks: @@ -31,8 +31,7 @@ var rootCmd = &cobra.Command{ // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + utils.ErrorP("%s\n", err.Error()) } } @@ -51,8 +50,7 @@ func initConfig() { // Find home directory. home, err := homedir.Dir() if err != nil { - fmt.Println(err) - os.Exit(1) + utils.ErrorP("%v", err) } // Search config in home directory with name ".google-tasks-cli" (without extension). diff --git a/cmd/tasklists.go b/cmd/tasklists.go index 3e39978..753e397 100644 --- a/cmd/tasklists.go +++ b/cmd/tasklists.go @@ -1,16 +1,13 @@ package cmd import ( - "context" "errors" - "fmt" - "log" - "github.com/BRO3886/gtasks/internal" + "github.com/BRO3886/gtasks/api" + "github.com/BRO3886/gtasks/internal/utils" "github.com/fatih/color" "github.com/manifoldco/promptui" "github.com/spf13/cobra" - "google.golang.org/api/option" "google.golang.org/api/tasks/v1" ) @@ -47,21 +44,14 @@ var showlistsCmd = &cobra.Command{ Short: "view tasklists", Long: `view task lists for the account currently signed in`, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) - - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) + srv := getService() + list, err := api.GetTaskLists(srv) if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) - } - - list, err := internal.GetTaskLists(srv) - if err != nil { - log.Fatalf("Error %v", err) + utils.ErrorP("Error: %v\n", err) } for index, i := range list { - fmt.Printf("[%d] %s\n", index+1, i.Title) + utils.Print("[%d] %s\n", index+1, i.Title) } }, @@ -72,24 +62,18 @@ var createlistsCmd = &cobra.Command{ Short: "create tasklist", Long: `Create tasklist for the currently signed in account`, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) - - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) - if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) - } + srv := getService() if title == "" { - fmt.Println("Title should not be empty. Use -t for title.\nExamples:\ngtasks tasklists create -t <TITLE>\ngtasks tasklists create --title <TITLE>") + utils.Warn("%s\n", "Title should not be empty. Use -t for title.\nExamples:\ngtasks tasklists create -t <TITLE>\ngtasks tasklists create --title <TITLE>") return } t := &tasks.TaskList{Title: title} r, err := srv.Tasklists.Insert(t).Do() if err != nil { - log.Fatalf("Unable to create task list. %v", err) + utils.ErrorP("Unable to create task list. %v", err) } title = "" - fmt.Println(color.GreenString("Created: ") + r.Title) + utils.Info("task list created: %s", r.Title) }, } @@ -98,20 +82,13 @@ var removeListCmd = &cobra.Command{ Short: "remove tasklist", Long: `Remove a tasklist for the currently signed in account`, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) - - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) + srv := getService() + list, err := api.GetTaskLists(srv) if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) + utils.ErrorP("Error %v", err) } - list, err := internal.GetTaskLists(srv) - if err != nil { - log.Fatalf("Error %v", err) - } - - fmt.Println("Choose a Tasklist:") + utils.Print("Choose a Tasklist: ") var l []string for _, i := range list { l = append(l, i.Title) @@ -126,14 +103,14 @@ var removeListCmd = &cobra.Command{ color.Red("Error: " + err.Error()) return } - fmt.Printf("%s: %s\n", color.YellowString("Deleting list"), result) + utils.Print("%s: %s\n", utils.WarnStyle.Sprint("Deleting list..."), result) - err = internal.DeleteTaskList(srv, list[option].Id) + err = api.DeleteTaskList(srv, list[option].Id) if err != nil { - color.Red("Error deleting tasklist: " + err.Error()) + utils.ErrorP("Error deleting tasklist: %s", err.Error()) return } - color.Green("Tasklist deleted") + utils.Info("Tasklist deleted") }, } @@ -142,24 +119,18 @@ var updateTitleCmd = &cobra.Command{ Short: "update tasklist title", Long: `Update tasklist title for the currently signed in account`, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) - - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) - if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) - } + srv := getService() if title == "" { - fmt.Println("Title should not be empty. Use -t for title.\nExamples:\ngtasks tasklists create -t <TITLE>\ngtasks tasklists create --title <TITLE>") + utils.Warn("Title should not be empty. Use -t for title.\nExamples:\ngtasks tasklists update -t <TITLE>\ngtasks tasklists update --title <TITLE>\n") return } - list, err := internal.GetTaskLists(srv) + list, err := api.GetTaskLists(srv) if err != nil { - log.Fatalf("Error %v", err) + utils.ErrorP(utils.Error("Error %v", err)) } - fmt.Println("Choose a Tasklist:") + utils.Print("Choose a Tasklist:") var l []string for _, i := range list { l = append(l, i.Title) @@ -171,18 +142,16 @@ var updateTitleCmd = &cobra.Command{ } option, _, err := prompt.Run() if err != nil { - color.Red("Error: " + err.Error()) - return + utils.ErrorP("Error: %s", err.Error()) } t := list[option] t.Title = title - _, err = internal.UpdateTaskList(srv, t) + _, err = api.UpdateTaskList(srv, &t) if err != nil { - color.Red("Error updating tasklist: " + err.Error()) - return + utils.ErrorP("Error updating tasklist: ", err.Error()) } - color.Green("Tasklist title updated") + utils.Info("Tasklist title updated") }, } diff --git a/cmd/tasks.go b/cmd/tasks.go index 8971ecf..5507c66 100644 --- a/cmd/tasks.go +++ b/cmd/tasks.go @@ -2,21 +2,20 @@ package cmd import ( "bufio" - "context" "fmt" - "log" "os" "runtime" + "sort" "strconv" "strings" "time" - "github.com/BRO3886/gtasks/internal" + "github.com/BRO3886/gtasks/api" + "github.com/BRO3886/gtasks/internal/utils" "github.com/fatih/color" "github.com/manifoldco/promptui" "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" - "google.golang.org/api/option" "google.golang.org/api/tasks/v1" ) @@ -24,10 +23,17 @@ import ( var tasksCmd = &cobra.Command{ Use: "tasks", Short: "View, create, and delete tasks in a tasklist", - // Long: ` - // View, create, list and delete tasks in a tasklist - // for the currently signed in account. - // `, + Long: ` + View, create, list and delete tasks in a tasklist + for the currently signed in account. + Usage: + [WITH LIST FLAG] + gtasks tasks -l "<task-list name>" view|add|rm|done + + [WITHOUT LIST FLAG] + gtasks tasks view|add|rm|done + * You would be prompted to select a tasklist + `, } var viewTasksCmd = &cobra.Command{ @@ -38,37 +44,12 @@ var viewTasksCmd = &cobra.Command{ tasklist for the currently signed in account `, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) - - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) - if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) - } - - list, err := internal.GetTaskLists(srv) - if err != nil { - log.Fatalf("Error %v", err) - } + srv := getService() + tList := getTaskLists(srv) - fmt.Println("Choose a Tasklist:") - var l []string - for _, i := range list { - l = append(l, i.Title) - } - - prompt := promptui.Select{ - Label: "Select Tasklist", - Items: l, - } - option, result, err := prompt.Run() - if err != nil { - color.Red("Error: " + err.Error()) - return - } - fmt.Printf("Tasks in %s:\n", result) + utils.Print("Tasks in %s:\n", tList.Title) - tasks, err := internal.GetTasks(srv, list[option].Id, showCompletedFlag) + tasks, err := api.GetTasks(srv, tList.Id, includeCompletedFlag) if err != nil { color.Red(err.Error()) return @@ -113,43 +94,17 @@ var createTaskCmd = &cobra.Command{ tasklist for the currently signed in account `, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) - - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) - if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) - } - - list, err := internal.GetTaskLists(srv) - if err != nil { - log.Fatalf("Error %v", err) - } - - fmt.Println("Choose a Tasklist:") - var l []string - for _, i := range list { - l = append(l, i.Title) - } - - prompt := promptui.Select{ - Label: "Select Tasklist", - Items: l, - } - option, result, err := prompt.Run() - if err != nil { - color.Red("Error: " + err.Error()) - return - } - fmt.Println("Creating task in " + result) + srv := getService() + tList := getTaskLists(srv) + utils.Warn("Creating task in %s\n", tList.Title) reader := bufio.NewReader(os.Stdin) - fmt.Printf("Title: ") + utils.Print("Title: ") title := getInput(reader) - fmt.Printf("Note: ") + utils.Print("Note: ") notes := getInput(reader) - fmt.Printf("Due Date (dd/mm/yyyy): ") + utils.Print("Due Date (dd/mm/yyyy): ") dateInput := getInput(reader) var dateString string @@ -173,14 +128,15 @@ var createTaskCmd = &cobra.Command{ t := time.Date(y, time.Month(m), d, 12, 0, 0, 0, time.UTC) dateString = t.Format(time.RFC3339) } + task := &tasks.Task{Title: title, Notes: notes, Due: dateString} - _, err = internal.CreateTask(srv, task, list[option].Id) + _, err := api.CreateTask(srv, task, tList.Id) if err != nil { color.Red("Unable to create task: %v", err) return } - color.Green("Task created") + utils.Info("Task created\n") }, } @@ -192,38 +148,12 @@ var markCompletedCmd = &cobra.Command{ in a selected tasklist for the currently signed in account `, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) + srv := getService() + tList := getTaskLists(srv) + utils.Print("Tasks in %s:\n", tList.Title) + tID := tList.Id - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) - if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) - } - - list, err := internal.GetTaskLists(srv) - if err != nil { - log.Fatalf("Error %v", err) - } - - fmt.Println("Choose a Tasklist:") - var l []string - for _, i := range list { - l = append(l, i.Title) - } - - prompt := promptui.Select{ - Label: "Select Tasklist", - Items: l, - } - option, result, err := prompt.Run() - if err != nil { - color.Red("Error: " + err.Error()) - return - } - fmt.Printf("Tasks in %s:\n", result) - tID := list[option].Id - - tasks, err := internal.GetTasks(srv, tID, false) + tasks, err := api.GetTasks(srv, tID, false) if err != nil { color.Red(err.Error()) return @@ -234,12 +164,12 @@ var markCompletedCmd = &cobra.Command{ tString = append(tString, i.Title) } - prompt = promptui.Select{ + prompt := promptui.Select{ Label: "Select Task", Items: tString, } - option, _, err = prompt.Run() + option, _, err := prompt.Run() if err != nil { color.Red("Error: " + err.Error()) return @@ -247,7 +177,7 @@ var markCompletedCmd = &cobra.Command{ t := tasks[option] t.Status = "completed" - _, err = internal.UpdateTask(srv, t, tID) + _, err = api.UpdateTask(srv, t, tID) if err != nil { color.Red("Unable to mark task as completed: %v", err) return @@ -264,38 +194,12 @@ var deleteTaskCmd = &cobra.Command{ for the currently signed in account `, Run: func(cmd *cobra.Command, args []string) { - config := internal.ReadCredentials() - client := getClient(config) - - srv, err := tasks.NewService(context.Background(), option.WithHTTPClient(client)) - if err != nil { - log.Fatalf("Unable to retrieve tasks Client %v", err) - } - - list, err := internal.GetTaskLists(srv) - if err != nil { - log.Fatalf("Error %v", err) - } - - fmt.Println("Choose a Tasklist:") - var l []string - for _, i := range list { - l = append(l, i.Title) - } - - prompt := promptui.Select{ - Label: "Select Tasklist", - Items: l, - } - option, result, err := prompt.Run() - if err != nil { - color.Red("Error: " + err.Error()) - return - } - fmt.Printf("Tasks in %s:\n", result) - tID := list[option].Id + srv := getService() + tList := getTaskLists(srv) + utils.Print("Tasks in %s:\n", tList.Title) + tID := tList.Id - tasks, err := internal.GetTasks(srv, tID, false) + tasks, err := api.GetTasks(srv, tID, false) if err != nil { color.Red(err.Error()) return @@ -306,12 +210,12 @@ var deleteTaskCmd = &cobra.Command{ tString = append(tString, i.Title) } - prompt = promptui.Select{ + prompt := promptui.Select{ Label: "Select Task", Items: tString, } - option, _, err = prompt.Run() + option, _, err := prompt.Run() if err != nil { color.Red("Error: " + err.Error()) return @@ -320,19 +224,21 @@ var deleteTaskCmd = &cobra.Command{ t := tasks[option] t.Status = "completed" - err = internal.DeleteTask(srv, t.Id, tID) + err = api.DeleteTask(srv, t.Id, tID) if err != nil { color.Red("Unable to delete task: %v", err) return } - fmt.Printf("%s: %s\n", color.GreenString("Deleted"), t.Title) + utils.Info("Deleted: %s\n", t.Title) }, } -var showCompletedFlag bool +var includeCompletedFlag bool +var taskListFlag string func init() { - viewTasksCmd.Flags().BoolVarP(&showCompletedFlag, "include-completed", "i", false, "use this flag to include completed tasks") + viewTasksCmd.Flags().BoolVarP(&includeCompletedFlag, "include-completed", "i", false, "use this flag to include completed tasks") + tasksCmd.PersistentFlags().StringVarP(&taskListFlag, "tasklist", "l", "", "use this flag to specify a tasklist") tasksCmd.AddCommand(viewTasksCmd, createTaskCmd, markCompletedCmd, deleteTaskCmd) rootCmd.AddCommand(tasksCmd) } @@ -346,3 +252,50 @@ func getInput(reader *bufio.Reader) string { } return title } + +func getTaskLists(srv *tasks.Service) tasks.TaskList { + list, err := api.GetTaskLists(srv) + if err != nil { + utils.ErrorP("Error %v", err) + } + + sort.SliceStable(list, func(i, j int) bool { + return list[i].Title <= list[j].Title + }) + + index := -1 + + if taskListFlag != "" { + + var titles []string + for _, tasklist := range list { + titles = append(titles, tasklist.Title) + } + + index = sort.SearchStrings(titles, taskListFlag) + + if !(index >= 0 && index < len(list) && list[index].Title == taskListFlag) { + utils.ErrorP("%s\n", "incorrect task-list name") + } + + } else { + utils.Print("Choose a Tasklist:") + var l []string + for _, i := range list { + l = append(l, i.Title) + } + + prompt := promptui.Select{ + Label: "Select Tasklist", + Items: l, + } + option, _, err := prompt.Run() + if err != nil { + utils.ErrorP("Error: %s", err.Error()) + } + + index = option + } + + return list[index] +} diff --git a/internal/credentials.go b/internal/config/credentials.go similarity index 85% rename from internal/credentials.go rename to internal/config/credentials.go index f0a0b0e..5b234f3 100644 --- a/internal/credentials.go +++ b/internal/config/credentials.go @@ -1,10 +1,10 @@ -package internal +package config import ( "io/ioutil" - "log" "os" + "github.com/BRO3886/gtasks/internal/utils" "golang.org/x/oauth2" "golang.org/x/oauth2/google" "google.golang.org/api/tasks/v1" @@ -15,18 +15,15 @@ func ReadCredentials() *oauth2.Config { folderPath := GetInstallLocation() b, err := ioutil.ReadFile(folderPath + "/config.json") if err != nil { - log.Fatalf("Unable to read client secret file: %v", err) + utils.ErrorP("Unable to read client secret file: %v", err) } config, err := google.ConfigFromJSON(b, tasks.TasksScope) if err != nil { - log.Fatalf("Unable to parse client secret file to config: %v", err) + utils.ErrorP("Unable to parse client secret file to config: %v", err) } return config } - - - func GenerateConfig() { credString := ` { diff --git a/internal/get_install_loc.go b/internal/config/get_install_loc.go similarity index 65% rename from internal/get_install_loc.go rename to internal/config/get_install_loc.go index 2866a91..4c805b5 100644 --- a/internal/get_install_loc.go +++ b/internal/config/get_install_loc.go @@ -1,8 +1,7 @@ -package internal +package config import ( - "log" - + "github.com/BRO3886/gtasks/internal/utils" "github.com/kardianos/osext" ) @@ -10,7 +9,7 @@ import ( func GetInstallLocation() string { folderPath, err := osext.ExecutableFolder() if err != nil { - log.Fatal(err) + utils.ErrorP("Get install location: %s", err.Error()) } return folderPath } diff --git a/internal/tasklists.go b/internal/tasklists.go deleted file mode 100644 index e0f4f87..0000000 --- a/internal/tasklists.go +++ /dev/null @@ -1,33 +0,0 @@ -package internal - -import ( - "errors" - "log" - - "google.golang.org/api/tasks/v1" -) - -func GetTaskLists(srv *tasks.Service) ([]*tasks.TaskList, error) { - r, err := srv.Tasklists.List().Do() - if err != nil { - log.Fatalf("Unable to retrieve task lists. %v", err) - } - - if len(r.Items) == 0 { - return nil, errors.New("no Tasklist found") - } - return r.Items, nil -} - -func UpdateTaskList(srv *tasks.Service, tl *tasks.TaskList) (*tasks.TaskList, error) { - r, err := srv.Tasklists.Patch(tl.Id, tl).Do() - if err != nil { - return nil, err - } - return r, nil -} - -func DeleteTaskList(srv *tasks.Service, tID string) error { - err := srv.Tasklists.Delete(tID).Do() - return err -} diff --git a/internal/utils/logging.go b/internal/utils/logging.go new file mode 100644 index 0000000..ebe6e12 --- /dev/null +++ b/internal/utils/logging.go @@ -0,0 +1,23 @@ +package utils + +import ( + "os" + + "github.com/fatih/color" +) + +var WarnStyle = color.New(color.FgHiYellow, color.Bold) +var ErrorStyle = color.New(color.FgHiRed, color.Bold) +var InfoStyle = color.New(color.FgHiGreen, color.Bold) +var PrintStyle = color.New(color.FgWhite) + +var Warn = WarnStyle.PrintfFunc() +var Error = ErrorStyle.SprintfFunc() + +func ErrorP(format string, a ...interface{}) { + ErrorStyle.Printf(format, a...) + os.Exit(1) +} + +var Info = InfoStyle.PrintfFunc() +var Print = PrintStyle.PrintfFunc() diff --git a/main.go b/main.go index 0b62a4f..976fc68 100644 --- a/main.go +++ b/main.go @@ -19,14 +19,14 @@ import ( "io/ioutil" "github.com/BRO3886/gtasks/cmd" - "github.com/BRO3886/gtasks/internal" + "github.com/BRO3886/gtasks/internal/config" ) func main() { - folderPath := internal.GetInstallLocation() + folderPath := config.GetInstallLocation() _, err := ioutil.ReadFile(folderPath + "/config.json") if err != nil { - internal.GenerateConfig() + config.GenerateConfig() } cmd.Execute() }