Skip to content

Commit

Permalink
Merge pull request #34 from NikhilSharma03/update_config_path
Browse files Browse the repository at this point in the history
feat: update `credential` file path
  • Loading branch information
NikhilSharma03 authored Dec 27, 2023
2 parents f11c87e + 14ef585 commit 2c35fd9
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 400 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.env.dev
.env.prod
tmp
okane_cli
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ generate-proto:
clean-proto:
rm -rf pkg/{protobuf,google}

## CLI commands

.PHONY: build-cli
build-cli:
cd cli; go build -o okane_cli .; mv okane_cli ..

## Server commands

.PHONY: lint
lint:
golangci-lint run ./...

.PHONY: run-server
run-server:
go run cmd/main.go
Expand Down
78 changes: 59 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Okane

<div align="center">
<div align="center">
<br>

<img width=100% src="https://github.com/NikhilSharma03/Okane/blob/main/assets/demo.gif"></p>

</div>

<div align="center">
<div align="center">
<br>
[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=95)](https://github.com/NikhilSharma03/Okane)

[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=95)](https://github.com/NikhilSharma03/Okane)
[![Build by Nikhil](https://img.shields.io/badge/Built%20by-Nikhil-Green)](https://github.com/NikhilSharma03/Okane)

</div>
Expand All @@ -23,53 +23,93 @@

## Technology Stack

<div align="center">
<div align="center">

<img alt="Go" src="https://img.shields.io/badge/go%20-%231572B6.svg?&style=for-the-badge&logo=go&logoColor=white"/> <img alt="gRPC"
src="https://img.shields.io/badge/grpc%20-%231572B6.svg?&style=for-the-badge"/> <img alt="Redis"
<img alt="Go" src="https://img.shields.io/badge/go%20-%231572B6.svg?&style=for-the-badge&logo=go&logoColor=white"/> <img alt="gRPC"
src="https://img.shields.io/badge/grpc%20-%231572B6.svg?&style=for-the-badge"/> <img alt="Redis"
src="https://img.shields.io/badge/redis%20-%231572B6.svg?&color=red&style=for-the-badge&logo=redis&logoColor=white"/>

</div>

<br>

## Setup and Installation

Add `Environment Variables` by creating a new `.env` file in `root` folder and add the contents following `.env.example`
First install `Protocol compiler`

```
brew install protobuf
```

Once you have added correct credentials, run the server using
Now, install `gRPC Go` plugin

```
go install \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest \
google.golang.org/protobuf/cmd/protoc-gen-go@latest \
google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
```

Then, add GOPATH in `.bashrc` file

```
export PATH=$PATH:$HOME/go/bin
export PATH=$PATH:/usr/local/go/bin
```

Now, Add `Environment Variables` by creating a new `.env` file in `root` folder and add the contents following `.env.example`

Once you have added correct credentials, run the server using

```
make run-server
```

now the server is running at `localhost:8000`
If using `docker compose`, then first build the image

Now, Lets build the okane cli using
```
make compose-dev-build
```

Now start the dev server

```
make build-cli
make compose-dev-up
```

Now naviagate to `cli` folder and you will find the `okane` file which we can use as
To access logs

```
./okane
make compose-dev-logs
```

<br>
To shut down the server

```
make compose-dev-down
```

Now the server is running at `localhost:8000`


Now, Lets build the `okane cli` app

```
make build-cli
```

If you want to run the server using docker, just add `redis` before port in `REDIS_ADDRESS` in `.env` file and run
Now you can use the app

```
docker-compose up --build
./okane_cli
```

<br>

# License

<div align="center">
<div align="center">
<br>

<img width=35% src="https://media0.giphy.com/media/3ornjXbo3cjqh2BIyY/200.gif"></p>
Expand Down
26 changes: 19 additions & 7 deletions cli/cmd/expense_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var expNewCmd = &cobra.Command{
Short: "Create a new expense",
Long: `
The new command is used to create a new expense
Example:
okane expense new
okane expense new
`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(_ *cobra.Command, _ []string) {
// Check if user is logged in
userData, err := loginUserData.GetData()
if err != nil {
Expand All @@ -58,26 +58,38 @@ Example:
titlePrompt := &survey.Input{
Message: "Please type expense title :",
}
survey.AskOne(titlePrompt, &title)
err = survey.AskOne(titlePrompt, &title)
if err != nil {
log.Fatalf(err.Error())
}
// Getting expense description
var description string
descriptionPrompt := &survey.Input{
Message: "Please type expense description :",
}
survey.AskOne(descriptionPrompt, &description)
err = survey.AskOne(descriptionPrompt, &description)
if err != nil {
log.Fatalf(err.Error())
}
// Getting expense amount
var amount string
amountPrompt := &survey.Input{
Message: "Please type expense amount (USD) :",
}
survey.AskOne(amountPrompt, &amount)
err = survey.AskOne(amountPrompt, &amount)
if err != nil {
log.Fatalf(err.Error())
}
// Getting expense type
expType := ""
expTypePrompt := &survey.Select{
Message: "Choose expense type:",
Options: []string{"Credit", "Debit"},
}
survey.AskOne(expTypePrompt, &expType)
err = survey.AskOne(expTypePrompt, &expType)
if err != nil {
log.Fatalf(err.Error())
}
// Validate data
valTitle := strings.TrimSpace(title)
valDescription := strings.TrimSpace(description)
Expand Down
25 changes: 18 additions & 7 deletions cli/cmd/expense_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ var expUpdateCmd = &cobra.Command{
Short: "Update single transaction details",
Long: `
The update command is used to update single transaction details
Example:
okane expense update --id/-I {expenseID}
`,
Run: func(cmd *cobra.Command, args []string) {
Run: func(cmd *cobra.Command, _ []string) {
// Getting expense ID
expenseID, err := cmd.Flags().GetString("id")
if err != nil || expenseID == "" {
Expand Down Expand Up @@ -105,27 +105,38 @@ Example:
titlePrompt := &survey.Input{
Message: "Please type expense title (current : " + Title + ") :",
}
survey.AskOne(titlePrompt, &title)
err = survey.AskOne(titlePrompt, &title)
if err != nil {
log.Fatalf(err.Error())
}
// Getting expense description
var description string
descriptionPrompt := &survey.Input{
Message: "Please type expense description (current : " + Description + ") :",
}
survey.AskOne(descriptionPrompt, &description)
err = survey.AskOne(descriptionPrompt, &description)
if err != nil {
log.Fatalf(err.Error())
}
// Getting expense amount
var amount string
amountPrompt := &survey.Input{
Message: "Please type expense amount (USD) (current : " + Amount + ") :",
}
survey.AskOne(amountPrompt, &amount)
err = survey.AskOne(amountPrompt, &amount)
if err != nil {
log.Fatalf(err.Error())
}
// Getting expense type
expType := ""
expTypePrompt := &survey.Select{
Message: "Choose expense type (current : " + Type + ") :",
Options: []string{"Credit", "Debit"},
}
survey.AskOne(expTypePrompt, &expType)
// Validate data
err = survey.AskOne(expTypePrompt, &expType)
if err != nil {
log.Fatalf(err.Error())
}
valTitle := strings.TrimSpace(title)
valDescription := strings.TrimSpace(description)
valAmount := strings.TrimSpace(amount)
Expand Down
65 changes: 24 additions & 41 deletions cli/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -74,70 +75,51 @@ func (lu *LoginUserData) Login(token, name, id, email, password string) error {
return err
}

// generate path
var filePath string = "cli/cred.yaml"
path, err := os.Getwd()
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("something went wrong while login")
}
pathLen := len(path)
lastPath := string(path[pathLen-3]) + string(path[pathLen-2]) + string(path[pathLen-1])
if lastPath == "cli" {
filePath = "cred.yaml"
} else if lastPath == "ane" {
filePath = "cli/cred.yaml"
} else {
return fmt.Errorf("please execute cli app from root dir or cli dir")

configDir := filepath.Join(homeDir, ".config", "okane")
err = os.MkdirAll(configDir, 0755)
if err != nil {
return fmt.Errorf("something went wrong while login")
}

filePath := filepath.Join(configDir, "cred.yml")

return os.WriteFile(filePath, y, 0644)
}

func (l *LoginUserData) LogOut() error {
// generate path
var filePath string = "cli/cred.yaml"
path, err := os.Getwd()
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("something went wrong while login")
}
pathLen := len(path)
lastPath := string(path[pathLen-3]) + string(path[pathLen-2]) + string(path[pathLen-1])
if lastPath == "cli" {
filePath = "cred.yaml"
} else if lastPath == "ane" {
filePath = "cli/cred.yaml"
} else {
return fmt.Errorf("please execute cli app from root dir or cli dir")
return fmt.Errorf("something went wrong while logout")
}

filePath := filepath.Join(homeDir, ".config", "okane", "cred.yml")

return os.Remove(filePath)
}

func (l *LoginUserData) GetData() (*LoginUserData, error) {
// generate path
var filePath string = "cli/cred.yaml"
var confPath string = "./cli"
path, err := os.Getwd()
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("something went wrong while login")
}
pathLen := len(path)
lastPath := string(path[pathLen-3]) + string(path[pathLen-2]) + string(path[pathLen-1])
if lastPath == "cli" {
filePath = "cred.yaml"
confPath = "."
} else if lastPath == "ane" {
filePath = "cli/cred.yaml"
} else {
return nil, fmt.Errorf("please execute cli app from root dir or cli dir")
return nil, fmt.Errorf("failed to read cred file ")
}

configDir := filepath.Join(homeDir, ".config", "okane")
filePath := filepath.Join(configDir, "cred.yml")

// Check if cred.yaml exists
_, err = os.Stat(filePath)
if errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("please login")
}
// Get values from cred.yaml

viper.SetConfigName("cred")
viper.AddConfigPath(confPath)
viper.AddConfigPath(configDir)

err = viper.ReadInConfig()
if err != nil {
return nil, fmt.Errorf(
Expand All @@ -150,5 +132,6 @@ func (l *LoginUserData) GetData() (*LoginUserData, error) {
"failed to unmarshal cred file! please remove the cred file and login again",
)
}

return l, nil
}
5 changes: 4 additions & 1 deletion cli/cmd/user_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ Example:
passPrompt := &survey.Password{
Message: "Please type your password (for authentication) :",
}
survey.AskOne(passPrompt, &password)
err = survey.AskOne(passPrompt, &password)
if err != nil {
log.Fatalf(err.Error())
}
// Validate data
valPassword := strings.TrimSpace(password)
// Check for empty field
Expand Down
Loading

0 comments on commit 2c35fd9

Please sign in to comment.