Skip to content

Commit

Permalink
refactor: move auth logic to api package
Browse files Browse the repository at this point in the history
* add logout command
* update login command to show if user is logged in
* fix goreleaser issues
* add github actions workflow
* update readme
* update docs
  • Loading branch information
BRO3886 committed Jan 22, 2022
1 parent 39ccccf commit a81dee3
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 212 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on:
push:
tags:
- "*"

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
6 changes: 1 addition & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ project_name: gtasks

release:
prerelease: auto
draft: true
name_template: "gtasks {{.Version}}"

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
# - go generate ./...
builds:
- env:
- CGO_ENABLED=0
Expand All @@ -27,11 +23,11 @@ archives:
windows: Windows
386: i386
amd64: x86_64

checksum:
name_template: "checksums.txt"
snapshot:
name_template: "{{ incpatch .Version }}-next"

changelog:
sort: asc
filters:
Expand Down
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ linux:
mac:
@echo "Building for mac"
GOOS=darwin GOARCH=amd64 go build -o ./bin/mac/gtasks
@ cd bin/mac
gtasks
all:
@echo "Building for every OS and Platform"
GOOS=windows GOARCH=386 GO386=softfloat go build -o ./bin/windows/gtasks.exe
Expand Down
134 changes: 80 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,55 @@

---

## Currently available commands

- [x] Login
- [x] View Task-List
- [x] Create Task-List
- [x] Update Task-List title
- [x] Delete Task-List
- [x] View Tasks
- [x] Create Tasks
- [ ] Edit Task
- [x] Mark as completed
- [x] Delete Task
## Docs

Refer to the [docs website](https://gtasks.sidv.dev) to read about available commands.

## Instructions to install

(make sure `$HOME/go/bin` is added to `$PATH`

```bash
go install github.com/BRO3886/gtasks@latest
```
or you can download the binary:

or you can download the binary:

1. Download the binary for your system (check [releases](https://github.com/BRO3886/google-tasks-cli/releases))
2. Move to a particular folder, for eg Documents
3. Append the absolute path (use ```pwd```) of the folder to ```PATH```
4. Execute ```gtasks``` from anywhere
3. Append the absolute path (use `pwd`) of the folder to `PATH`
4. Execute `gtasks` from anywhere

## Instructions to Run and Build from Source:
- Pre-requisites
- Go
- Directions to install
```bash
git clone https://github.com/BRO3886/google-tasks-cli
```
- Directions to execute

(if you're on linux)

```bash
make linux
./bin/linux/gtasks <COMMAND>
```

(if you're on windows)

```bash
make windows
./bin/windows/gtasks <COMMAND>
```

Or, you can check out the pre-compiled binaries under **Releases**

- Usage

- Pre-requisites
- Go
- Directions to install

```bash
git clone https://github.com/BRO3886/google-tasks-cli
```

- Directions to execute

(if you're on linux)

```bash
make linux
./bin/linux/gtasks <COMMAND>
```

(if you're on windows)

```bash
make windows
./bin/windows/gtasks <COMMAND>
```

Or, you can check out the pre-compiled binaries under **Releases**

- Usage

```
Usage:
gtasks [command]
Expand All @@ -79,67 +75,97 @@ Use "gtasks [command] --help" for more information about a command.
## Commands

### Help
* To see details about a command

- To see details about a command

```bash
gtasks <COMMAND> help
```

### Login
* Login
### Auth

- Login

```bash
gtasks login
```

- Logout

```bash
gtasks logout
```

### Tasklists
* Viewing Tasklists

- Viewing Tasklists

```bash
gtasks tasklists view
```

* Creating a Tasklist
- Creating a Tasklist

```bash
gtasks tasklists create -t 'title'
gtasks tasklists create --title 'title'
gtasks tasklists add -t 'title'
gtasks tasklists add --title 'title'
```

* Deleting a Tasklist
- Deleting a Tasklist

```bash
gtasks tasklists rm
```

### Tasks
* To pre-select tasklist, provide it's title as follows:

- To pre-select tasklist, provide it's title as follows:

```bash
gtasks tasks -l <title> 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
- Viewing tasks

```bash
gtasks tasks view
```

* Include completed tasks
- Include completed tasks

```bash
gtasks tasks view -i
gtasks tasks view --include-completed
```

* Adding a task
- Sort options

```bash
gtasks tasks view ... --sort [due,title,position, default=position]
```

- Adding a task

```bash
gtasks tasks add
```

* Mark task as completed
- Mark task as completed

```bash
gtasks tasks done
```

* Deleting a task
- Deleting a task

```bash
gtasks tasks rm
```
Expand Down
104 changes: 104 additions & 0 deletions api/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package api

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"

"github.com/BRO3886/gtasks/internal/config"
"github.com/BRO3886/gtasks/internal/utils"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"google.golang.org/api/tasks/v1"
)

func Login(c *oauth2.Config) error {
folderPath := config.GetInstallLocation()
// fmt.Println(folderPath)
tokFile := folderPath + "/token.json"
_, err := tokenFromFile(tokFile)
if err != nil {
tok := getTokenFromWeb(c)
saveToken(tokFile, tok)
return nil
}
return fmt.Errorf("already logged in")
}

func Logout() error {
folderPath := config.GetInstallLocation()
// fmt.Println(folderPath)
tokFile := folderPath + "/token.json"
return os.Remove(tokFile)
}

// 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
}

// Retrieve a token, saves the token, then returns the generated 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 := config.GetInstallLocation()
// fmt.Println(folderPath)
tokFile := folderPath + "/token.json"
tok, err := tokenFromFile(tokFile)
if err != nil {
tok = getTokenFromWeb(c)
saveToken(tokFile, 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)
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 {
utils.ErrorP("Unable to read authorization code: %v", err)
}

tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
utils.ErrorP("Unable to retrieve token from web: %v", err)
}
return tok
}

// Retrieves a token from a local file.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}

// Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) {
utils.Warn("Saving credential file\n")
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
utils.ErrorP("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}
Loading

0 comments on commit a81dee3

Please sign in to comment.