Skip to content

Commit

Permalink
Accept tilda (~) based paths (#150)
Browse files Browse the repository at this point in the history
* Accept tilda based paths

* Update credential file path in config
  • Loading branch information
goyalmunish authored Dec 25, 2022
1 parent bfa4e3f commit f3983ab
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 33 deletions.
3 changes: 2 additions & 1 deletion cmd/reminder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/uuid"
"github.com/goyalmunish/reminder/internal/model"
"github.com/goyalmunish/reminder/internal/settings"
"github.com/goyalmunish/reminder/pkg/calendar"
"github.com/goyalmunish/reminder/pkg/logger"
"github.com/goyalmunish/reminder/pkg/utils"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func Flow() {
// read and parse the existing data
reminderData := *model.ReadDataFile(ctx, config.AppInfo.DataFile)
// initialize Google Calendar
model.FetchCalendar(ctx)
calendar.FetchCalendar(ctx)
// print data stats
fmt.Println(reminderData.Stats())
// try automatic backup
Expand Down
4 changes: 2 additions & 2 deletions config/default.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
appinfo:
# datafile: /Users/goyalmunish/reminder/data.json
data_file: ~/reminder/data.json
log:
level: 5
lookup_fields:
- app
- run_id
calendar:
credentialfile: ~/credentials.json
credential_file: ~/calendar_credentials.json
13 changes: 2 additions & 11 deletions internal/appinfo/options.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package appinfo

import (
"os"
"path"
)

type Options struct {
DataFile string
DataFile string `json:"data_file" yaml:"data_file" mapstructure:"data_file"`
}

func DefaultOptions() *Options {
homePath := os.Getenv("HOME")
if homePath == "" {
homePath = "~"
}
dataFilePath := path.Join(homePath, "reminder", "data.json")
dataFilePath := "~/reminder/data.json"
return &Options{
DataFile: dataFilePath,
}
Expand Down
1 change: 1 addition & 0 deletions internal/model/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (comment *Comment) String() string {
return strings.Join(parts, " | ")
}

// SetContext sets given context to the receiver.
func (comment *Comment) SetContext(ctx context.Context) {
comment.context = ctx
}
3 changes: 2 additions & 1 deletion internal/model/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ func BlankReminder(askUserInput bool, dataFilePath string) *ReminderData {
func ReadDataFile(ctx context.Context, dataFilePath string) *ReminderData {
var reminderData ReminderData
// read byte data from file
byteValue, err := os.ReadFile(dataFilePath)
byteValue, err := os.ReadFile(utils.TryConvertTildaBasedPath(dataFilePath))
utils.LogError(ctx, err)
// parse json data
err = json.Unmarshal(byteValue, &reminderData)
logger.Info(ctx, fmt.Sprintf("Read contents of %q into ReminderData", dataFilePath))
utils.LogError(ctx, err)
// set context
reminderData.SetContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func skipCI(t *testing.T) {

func TestDataFile(t *testing.T) {
defaultDataFilePath := settings.DefaultSettings().AppInfo.DataFile
utils.AssertEqual(t, strings.HasPrefix(defaultDataFilePath, "/"), true)
utils.AssertEqual(t, strings.HasPrefix(defaultDataFilePath, "/") || strings.HasPrefix(defaultDataFilePath, "~/"), true)
utils.AssertEqual(t, strings.HasSuffix(defaultDataFilePath, ".json"), true)
}

Expand Down
1 change: 1 addition & 0 deletions internal/model/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (note *Note) Type() string {
return "incidental"
}

// SetContext sets given context to the receiver.
func (note *Note) SetContext(ctx context.Context) {
note.context = ctx
}
Expand Down
1 change: 1 addition & 0 deletions internal/model/reminder_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ReminderData struct {
BaseStruct
}

// SetContext sets given context to the receiver.
func (rd *ReminderData) SetContext(ctx context.Context) {
rd.context = ctx
}
Expand Down
1 change: 1 addition & 0 deletions internal/model/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (t Tag) String() string {
return fmt.Sprintf("%v#%v#%v", t.Group, t.Slug, t.Id)
}

// SetContext sets given context to the receiver.
func (tag *Tag) SetContext(ctx context.Context) {
tag.context = ctx
}
1 change: 1 addition & 0 deletions internal/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (u User) String() string {
return fmt.Sprintf("{Name: %v, EmailId: %v}", u.Name, u.EmailId)
}

// SetContext sets given context to the receiver.
func (u *User) SetContext(ctx context.Context) {
u.context = ctx
}
11 changes: 5 additions & 6 deletions internal/model/calendar.go → pkg/calendar/calendar.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package calendar

import (
"context"
Expand All @@ -10,22 +10,21 @@ import (
"time"

"github.com/goyalmunish/reminder/pkg/logger"
"github.com/goyalmunish/reminder/pkg/utils"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)

var HomeDir string = os.Getenv("HOME")

const EnableCalendar bool = false

// Retrieve a token, saves the token, then returns the generated client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
// The file calendar_token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first time.
tokenFile := fmt.Sprintf("%s/%s", HomeDir, "calendar_token.json")
tokenFile := fmt.Sprintf("%s/%s", utils.HomeDir(), "calendar_token.json")
tok, err := tokenFromFile(ctx, tokenFile)
if err != nil {
tok = getTokenFromWeb(ctx, config)
Expand Down Expand Up @@ -79,8 +78,8 @@ func saveToken(ctx context.Context, path string, token *oauth2.Token) {

// Get Calendar Service.
func getCalendarService(ctx context.Context) (*calendar.Service, error) {
credFile := fmt.Sprintf("%s/%s", HomeDir, "calendar_credentials.json")
b, err := os.ReadFile(credFile)
credFile := "~/calendar_credentials.json"
b, err := os.ReadFile(utils.TryConvertTildaBasedPath(credFile))
if err != nil {
logger.Fatal(ctx, fmt.Sprintf("Unable to read client secret file: %v", err))
}
Expand Down
13 changes: 2 additions & 11 deletions pkg/calendar/options.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package calendar

import (
"os"
"path"
)

type Options struct {
CredentialFile string
CredentialFile string `json:"credential_file" yaml:"credential_file" mapstructure:"credential_file"`
}

func DefaultOptions() *Options {
homePath := os.Getenv("HOME")
if homePath != "" {
homePath = "~"
}
return &Options{
CredentialFile: path.Join(homePath, "credentials.json"),
CredentialFile: "~/calendar_credentials.json",
}
}
23 changes: 23 additions & 0 deletions pkg/utils/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"html/template"
"os"
"os/exec"
"os/user"
"path/filepath"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -446,3 +448,24 @@ func GenerateNoteSearchSelect(items []string, searchFunc func(filter string, val
err := survey.AskOne(prompt, &selectedIndex)
return selectedIndex, err
}

// HomeDir return the home directory path for current user.
func HomeDir() string {
usr, _ := user.Current()
dir := usr.HomeDir
return dir
}

// TryConvertTildaBasedPath converts tilda (~) based path to complete path.
// For a non-tilda based path, return as it is.
func TryConvertTildaBasedPath(path string) string {
homeDir := HomeDir()
if path == "~" {
path = homeDir
} else if strings.HasPrefix(path, "~/") {
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
path = filepath.Join(homeDir, path[2:])
}
return path
}
7 changes: 7 additions & 0 deletions pkg/utils/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,10 @@ func TestPerformShellOperation(t *testing.T) {
_, err = utils.PerformShellOperation("command_do_not_exist", "arg1", "arg2")
utils.AssertEqual(t, err, errors.New("exec: no command"))
}

func TestHomeDir(t *testing.T) {
got := utils.HomeDir()
if len(got) == 0 {
t.Errorf("HomeDir function returns blank path")
}
}
Empty file added temp_file
Empty file.

0 comments on commit f3983ab

Please sign in to comment.