Skip to content

Commit

Permalink
- Add CLI support
Browse files Browse the repository at this point in the history
- Add clear cmd
- Add list cmd
- Add version cmd
- Add sync cmd
  • Loading branch information
shellbear committed Sep 4, 2019
1 parent 21a4308 commit 9272b4c
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 2 deletions.
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Created by .ignore support plugin (hsz.mobi)
### Example user template template
### Example user template

# Google
credentials.json
token.json

# IntelliJ project files
.idea
*.iml
out
gen
### Go template
# Binaries for programs and plugins
epical
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# epical
Epitech calendar
# EpiCal
A simple to synchronize your Epitech events with Google calendar.

# Requirements

To run this script you have to use your Epitech auto login token which can be found [here](https://intra.epitech.eu/admin/autolog).
The URL is of the form `https://intra.epitech.eu/auth-XXX...`
The token used by the script is only the right part of the URL after the `auth-` part.

## Installation
```go
go build .
```

## Commands

### Sync
A command to synchronize Epitech events with your Google calendar.
```go
./epical --token YOUR_EPITECH_AUTOLOGIN_TOKEN sync
```

### List
A command to list Epitech events.
```go
./epical --token YOUR_EPITECH_AUTOLOGIN_TOKEN list
```

### Clear
Delete all events previously created by EpiCal.
```go
./epical --token YOUR_EPITECH_AUTOLOGIN_TOKEN clear
```
16 changes: 16 additions & 0 deletions cmd/clear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"fmt"
"github.com/ShellBear/epical/pkg/epical"
"github.com/spf13/cobra"
)

var clearCmd = &cobra.Command{
Use: "clear",
Short: "Clear all calendar events generated by Epical",
Run: func(cmd *cobra.Command, args []string) {
epical.ClearEvents(cmd.Flag("credentials").Value.String())
fmt.Println("Successfully cleared calendar events.")
},
}
38 changes: 38 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

var (
EpitechToken string
Credentials string
)

var cli = &cobra.Command{
Use: "epical",
Short: "Synchronize your Epitech's events with Google calendar",
Long: "A fast and simple way to sync your Epitech calendar with Google.\n" +
"Complete documentation is available at https://github.com/shellbear/epical",
}

func Execute() {
cli.PersistentFlags().StringVarP(&EpitechToken, "token", "t", "", "Epitech API Token")
cli.PersistentFlags().StringVarP(&Credentials, "credentials", "c", "credentials.json", "Google API credentials")

cli.MarkPersistentFlagRequired("token")

if err := cli.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cli.AddCommand(versionCmd)
cli.AddCommand(clearCmd)
cli.AddCommand(listCmd)
cli.AddCommand(syncCmd)
}
14 changes: 14 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/ShellBear/epical/pkg/epical"
"github.com/spf13/cobra"
)

var listCmd = &cobra.Command{
Use: "list",
Short: "List all upcoming Epitech events",
Run: func(cmd *cobra.Command, args []string) {
epical.ListEvents(cmd.Flag("token").Value.String())
},
}
16 changes: 16 additions & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"fmt"
"github.com/ShellBear/epical/pkg/epical"
"github.com/spf13/cobra"
)

var syncCmd = &cobra.Command{
Use: "sync",
Short: "Sync all upcoming Epitech events to Google calendar",
Run: func(cmd *cobra.Command, args []string) {
epical.SyncCalendar(cmd.Flag("credentials").Value.String(), cmd.Flag("token").Value.String())
fmt.Println("Successfully synchronized Epitech events to Google calendar.")
},
}
15 changes: 15 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"fmt"
"github.com/ShellBear/epical/pkg/epical"
"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Epical",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Epical v%s\n", epical.VERSION)
},
}
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/ShellBear/epical

go 1.12

require (
cloud.google.com/go v0.45.0 // indirect
github.com/google/go-cmp v0.3.1 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/spf13/cobra v0.0.5
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sys v0.0.0-20190904005037-43c01164e931 // indirect
google.golang.org/api v0.9.0
google.golang.org/appengine v1.6.2 // indirect
google.golang.org/grpc v1.23.0 // indirect
)
173 changes: 173 additions & 0 deletions go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/ShellBear/epical/cmd"

func main() {
cmd.Execute()
}
97 changes: 97 additions & 0 deletions pkg/epical/epical.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package epical

import (
"fmt"
"log"
)

const (
CALENDAR_NAME = "EpiCal"
VERSION = "0.1"
)

func ListEvents(epitechToken string) {
data, err := GetRegisteredEvents(epitechToken)
if err != nil {
log.Fatal(err)
}

if len(data) == 0 {
fmt.Println("No upcoming events found.")
} else {
for _, evt := range data {
fmt.Printf("%v (%v-%v)\n", evt.ActiTitle, evt.Start, evt.End)
}
}
}

func ClearEvents(credentialsPath string) {
svc, err := GetGoogleCalendarService(credentialsPath)
if err != nil {
log.Fatal(err)
}

cal, err := GetGoogleCalendarByName(svc, CALENDAR_NAME)
if err != nil {
log.Fatal(err)
}

if cal != nil {
events, err := svc.Events.List(cal.Id).Do()
if err != nil {
log.Fatal(err)
}

for _, evt := range events.Items {
err = svc.Events.Delete(cal.Id, evt.Id).Do()
if err != nil {
log.Fatal(err)
}
}

err = svc.Calendars.Delete(cal.Id).Do()
if err != nil {
log.Fatal(err)
}
}
}

func SyncCalendar(credentialsPath, token string) {
data, err := GetRegisteredEvents(token)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Found %d events to synchronize.\n", len(data))

svc, err := GetGoogleCalendarService(credentialsPath)
if err != nil {
log.Fatal(err)
}

ClearEvents(credentialsPath)
fmt.Println("Cleared old calendar events.")

cal, err := GetOrCreateGoogleCalendar(svc, CALENDAR_NAME)
if err != nil {
log.Fatal(err)
}

if len(data) == 0 {
fmt.Println("There is no upcoming Epitech event.")
} else {
for _, c := range data {
newEvt, err := NewGoogleCalendarEvent(&c)
if err != nil {
log.Fatal(err)
}

evt, err := svc.Events.Insert(cal.Id, newEvt).Do()
if err != nil {
log.Fatal(err)
}

log.Println("Created event", evt.Summary)
}
}
}

0 comments on commit 9272b4c

Please sign in to comment.