Skip to content

Commit

Permalink
[sheets] make the sheets directory configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Feb 11, 2024
1 parent c7b377d commit c1c0f38
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ journal_path: /home/john/Documents/paisa/main.ledger
# REQUIRED
db_path: /home/john/Documents/paisa/paisa.db

# Path to your sheets directory. It can be absolute or relative to the
# configuration file. The sheets directory will be created if it does not exist.
# By default it will be created in the same directory as the journal file.
# OPTIONAL, DEFAULT: same directory as journal file.
sheets_directory: sheets

# The ledger client to use
# OPTIONAL, DEFAULT: ledger, ENUM: ledger, hledger, beancount
ledger_cli: ledger
Expand Down
17 changes: 16 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type CreditCard struct {
type Config struct {
JournalPath string `json:"journal_path" yaml:"journal_path"`
DBPath string `json:"db_path" yaml:"db_path"`
SheetsDirectory string `json:"sheets_directory" yaml:"sheets_directory"`
Readonly bool `json:"readonly" yaml:"readonly"`
LedgerCli string `json:"ledger_cli" yaml:"ledger_cli"`
DefaultCurrency string `json:"default_currency" yaml:"default_currency"`
Expand Down Expand Up @@ -349,7 +350,21 @@ func GetJournalPath() string {
}

func GetSheetDir() string {
return filepath.Dir(GetJournalPath())
if config.SheetsDirectory == "" {
return filepath.Dir(GetJournalPath())
}

dir := config.SheetsDirectory
if !filepath.IsAbs(config.SheetsDirectory) {
dir = filepath.Join(GetConfigDir(), config.SheetsDirectory)
}

err := os.MkdirAll(dir, 0750)
if err != nil {
log.Fatal("Failed to create sheets directory", err)
}

return dir
}

func GetDBPath() string {
Expand Down
4 changes: 4 additions & 0 deletions internal/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"type": "string",
"description": "Path to your database file. It can be absolute or relative to the configuration file. The database file will be created if it does not exist."
},
"sheets_directory": {
"type": "string",
"description": "Path to your sheets directory. It can be absolute or relative to the configuration file. The sheets directory will be created if it does not exist. By default it will be created in the same directory as the journal file."
},
"readonly": {
"type": "boolean",
"description": "Run in readonly mode.",
Expand Down
5 changes: 5 additions & 0 deletions tests/fixture/eur-hledger/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"config": {
"journal_path": "main.ledger",
"db_path": "paisa.db",
"sheets_directory": "",
"readonly": false,
"ledger_cli": "hledger",
"default_currency": "EUR",
Expand Down Expand Up @@ -604,6 +605,10 @@
],
"type": "array"
},
"sheets_directory": {
"description": "Path to your sheets directory. It can be absolute or relative to the configuration file. The sheets directory will be created if it does not exist. By default it will be created in the same directory as the journal file.",
"type": "string"
},
"strict": {
"description": "When strict mode is enabled, all the accounts and commodities should be defined before use.",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions tests/fixture/eur/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"config": {
"journal_path": "main.ledger",
"db_path": "paisa.db",
"sheets_directory": "",
"readonly": false,
"ledger_cli": "ledger",
"default_currency": "EUR",
Expand Down Expand Up @@ -604,6 +605,10 @@
],
"type": "array"
},
"sheets_directory": {
"description": "Path to your sheets directory. It can be absolute or relative to the configuration file. The sheets directory will be created if it does not exist. By default it will be created in the same directory as the journal file.",
"type": "string"
},
"strict": {
"description": "When strict mode is enabled, all the accounts and commodities should be defined before use.",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions tests/fixture/inr-beancount/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"config": {
"journal_path": "main.beancount",
"db_path": "paisa.db",
"sheets_directory": "",
"readonly": false,
"ledger_cli": "beancount",
"default_currency": "INR",
Expand Down Expand Up @@ -613,6 +614,10 @@
],
"type": "array"
},
"sheets_directory": {
"description": "Path to your sheets directory. It can be absolute or relative to the configuration file. The sheets directory will be created if it does not exist. By default it will be created in the same directory as the journal file.",
"type": "string"
},
"strict": {
"description": "When strict mode is enabled, all the accounts and commodities should be defined before use.",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions tests/fixture/inr-hledger/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"config": {
"journal_path": "main.ledger",
"db_path": "paisa.db",
"sheets_directory": "",
"readonly": false,
"ledger_cli": "hledger",
"default_currency": "INR",
Expand Down Expand Up @@ -611,6 +612,10 @@
],
"type": "array"
},
"sheets_directory": {
"description": "Path to your sheets directory. It can be absolute or relative to the configuration file. The sheets directory will be created if it does not exist. By default it will be created in the same directory as the journal file.",
"type": "string"
},
"strict": {
"description": "When strict mode is enabled, all the accounts and commodities should be defined before use.",
"enum": [
Expand Down
5 changes: 5 additions & 0 deletions tests/fixture/inr/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"config": {
"journal_path": "main.ledger",
"db_path": "paisa.db",
"sheets_directory": "",
"readonly": false,
"ledger_cli": "ledger",
"default_currency": "INR",
Expand Down Expand Up @@ -611,6 +612,10 @@
],
"type": "array"
},
"sheets_directory": {
"description": "Path to your sheets directory. It can be absolute or relative to the configuration file. The sheets directory will be created if it does not exist. By default it will be created in the same directory as the journal file.",
"type": "string"
},
"strict": {
"description": "When strict mode is enabled, all the accounts and commodities should be defined before use.",
"enum": [
Expand Down

0 comments on commit c1c0f38

Please sign in to comment.