-
-
Notifications
You must be signed in to change notification settings - Fork 89
refactor: reorganization of modules #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dvrd
wants to merge
2
commits into
maaslalani:main
Choose a base branch
from
dvrd:refactor/code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
*.json | ||
*.yaml | ||
*.yml | ||
invoice | ||
!.github/workflows/*.yml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +0,0 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Maas Lalani | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package assets | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
//go:embed "fonts/Inter Variable/Inter.ttf" | ||
var InterFont []byte | ||
|
||
//go:embed "fonts/Inter Hinted for Windows/Desktop/Inter-Bold.ttf" | ||
var InterBoldFont []byte |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package cmd | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/maaslalani/invoice/assets" | ||
"github.com/maaslalani/invoice/utils" | ||
"github.com/signintech/gopdf" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
importPath string | ||
output string | ||
file = utils.Invoice{} | ||
defaultInvoice = utils.DefaultInvoice() | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(generateCmd) | ||
viper.AutomaticEnv() | ||
|
||
generateCmd.Flags().StringVar(&importPath, "import", "", "Imported file (.json/.yaml)") | ||
generateCmd.Flags().StringVar(&file.Id, "id", time.Now().Format("20060102"), "ID") | ||
generateCmd.Flags().StringVar(&file.Title, "title", "INVOICE", "Title") | ||
|
||
generateCmd.Flags().Float64SliceVarP(&file.Rates, "rate", "r", defaultInvoice.Rates, "Rates") | ||
generateCmd.Flags().IntSliceVarP(&file.Quantities, "quantity", "q", defaultInvoice.Quantities, "Quantities") | ||
generateCmd.Flags().StringSliceVarP(&file.Items, "item", "i", defaultInvoice.Items, "Items") | ||
|
||
generateCmd.Flags().StringVarP(&file.Logo, "logo", "l", defaultInvoice.Logo, "Company logo") | ||
generateCmd.Flags().StringVarP(&file.From, "from", "f", defaultInvoice.From, "Issuing company") | ||
generateCmd.Flags().StringVarP(&file.To, "to", "t", defaultInvoice.To, "Recipient company") | ||
generateCmd.Flags().StringVar(&file.Date, "date", defaultInvoice.Date, "Date") | ||
generateCmd.Flags().StringVar(&file.Due, "due", defaultInvoice.Due, "Payment due date") | ||
|
||
generateCmd.Flags().Float64Var(&file.Tax, "tax", defaultInvoice.Tax, "Tax") | ||
generateCmd.Flags().Float64VarP(&file.Discount, "discount", "d", defaultInvoice.Discount, "Discount") | ||
generateCmd.Flags().StringVarP(&file.Currency, "currency", "c", defaultInvoice.Currency, "Currency") | ||
|
||
generateCmd.Flags().StringVarP(&file.Note, "note", "n", "", "Note") | ||
generateCmd.Flags().StringVarP(&output, "output", "o", "invoice.pdf", "Output file (.pdf)") | ||
|
||
flag.Parse() | ||
} | ||
|
||
var generateCmd = &cobra.Command{ | ||
Use: "generate", | ||
Short: "Generate an invoice", | ||
Long: `Generate an invoice`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
if importPath != "" { | ||
err := utils.ImportData(importPath, &file, cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
pdf := gopdf.GoPdf{} | ||
pdf.Start(gopdf.Config{ | ||
PageSize: *gopdf.PageSizeA4, | ||
}) | ||
pdf.SetMargins(40, 40, 40, 40) | ||
pdf.AddPage() | ||
err := pdf.AddTTFFontData("Inter", assets.InterFont) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = pdf.AddTTFFontData("Inter-Bold", assets.InterBoldFont) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
utils.WriteLogo(&pdf, file.Logo, file.From) | ||
utils.WriteTitle(&pdf, file.Title, file.Id, file.Date) | ||
utils.WriteBillTo(&pdf, file.To) | ||
utils.WriteHeaderRow(&pdf) | ||
subtotal := 0.0 | ||
for i := range file.Items { | ||
q := 1 | ||
if len(file.Quantities) > i { | ||
q = file.Quantities[i] | ||
} | ||
|
||
r := 0.0 | ||
if len(file.Rates) > i { | ||
r = file.Rates[i] | ||
} | ||
|
||
utils.WriteRow(&pdf, file.Items[i], q, r, file.Currency) | ||
subtotal += float64(q) * r | ||
} | ||
if file.Note != "" { | ||
utils.WriteNotes(&pdf, file.Note) | ||
} | ||
utils.WriteTotals(&pdf, subtotal, subtotal*file.Tax, subtotal*file.Discount, file.Currency) | ||
if file.Due != "" { | ||
utils.WriteDueDate(&pdf, file.Due) | ||
} | ||
utils.WriteFooter(&pdf, file.Id) | ||
output = strings.TrimSuffix(output, ".pdf") + ".pdf" | ||
err = pdf.WritePdf(output) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Generated %s\n", output) | ||
|
||
return nil | ||
}, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "invoice", | ||
Short: "Invoice generates invoices from the command line.", | ||
Long: `Invoice generates invoices from the command line.`, | ||
} | ||
|
||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,174 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
_ "embed" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/signintech/gopdf" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
//go:embed "Inter/Inter Variable/Inter.ttf" | ||
var interFont []byte | ||
|
||
//go:embed "Inter/Inter Hinted for Windows/Desktop/Inter-Bold.ttf" | ||
var interBoldFont []byte | ||
|
||
type Invoice struct { | ||
Id string `json:"id" yaml:"id"` | ||
Title string `json:"title" yaml:"title"` | ||
|
||
Logo string `json:"logo" yaml:"logo"` | ||
From string `json:"from" yaml:"from"` | ||
To string `json:"to" yaml:"to"` | ||
Date string `json:"date" yaml:"date"` | ||
Due string `json:"due" yaml:"due"` | ||
|
||
Items []string `json:"items" yaml:"items"` | ||
Quantities []int `json:"quantities" yaml:"quantities"` | ||
Rates []float64 `json:"rates" yaml:"rates"` | ||
|
||
Tax float64 `json:"tax" yaml:"tax"` | ||
Discount float64 `json:"discount" yaml:"discount"` | ||
Currency string `json:"currency" yaml:"currency"` | ||
|
||
Note string `json:"note" yaml:"note"` | ||
} | ||
|
||
func DefaultInvoice() Invoice { | ||
return Invoice{ | ||
Id: time.Now().Format("20060102"), | ||
Title: "INVOICE", | ||
Rates: []float64{25}, | ||
Quantities: []int{2}, | ||
Items: []string{"Paper Cranes"}, | ||
From: "Project Folded, Inc.", | ||
To: "Untitled Corporation, Inc.", | ||
Date: time.Now().Format("Jan 02, 2006"), | ||
Due: time.Now().AddDate(0, 0, 14).Format("Jan 02, 2006"), | ||
Tax: 0, | ||
Discount: 0, | ||
Currency: "USD", | ||
} | ||
} | ||
|
||
var ( | ||
importPath string | ||
output string | ||
file = Invoice{} | ||
defaultInvoice = DefaultInvoice() | ||
) | ||
|
||
func init() { | ||
viper.AutomaticEnv() | ||
|
||
generateCmd.Flags().StringVar(&importPath, "import", "", "Imported file (.json/.yaml)") | ||
generateCmd.Flags().StringVar(&file.Id, "id", time.Now().Format("20060102"), "ID") | ||
generateCmd.Flags().StringVar(&file.Title, "title", "INVOICE", "Title") | ||
|
||
generateCmd.Flags().Float64SliceVarP(&file.Rates, "rate", "r", defaultInvoice.Rates, "Rates") | ||
generateCmd.Flags().IntSliceVarP(&file.Quantities, "quantity", "q", defaultInvoice.Quantities, "Quantities") | ||
generateCmd.Flags().StringSliceVarP(&file.Items, "item", "i", defaultInvoice.Items, "Items") | ||
|
||
generateCmd.Flags().StringVarP(&file.Logo, "logo", "l", defaultInvoice.Logo, "Company logo") | ||
generateCmd.Flags().StringVarP(&file.From, "from", "f", defaultInvoice.From, "Issuing company") | ||
generateCmd.Flags().StringVarP(&file.To, "to", "t", defaultInvoice.To, "Recipient company") | ||
generateCmd.Flags().StringVar(&file.Date, "date", defaultInvoice.Date, "Date") | ||
generateCmd.Flags().StringVar(&file.Due, "due", defaultInvoice.Due, "Payment due date") | ||
|
||
generateCmd.Flags().Float64Var(&file.Tax, "tax", defaultInvoice.Tax, "Tax") | ||
generateCmd.Flags().Float64VarP(&file.Discount, "discount", "d", defaultInvoice.Discount, "Discount") | ||
generateCmd.Flags().StringVarP(&file.Currency, "currency", "c", defaultInvoice.Currency, "Currency") | ||
|
||
generateCmd.Flags().StringVarP(&file.Note, "note", "n", "", "Note") | ||
generateCmd.Flags().StringVarP(&output, "output", "o", "invoice.pdf", "Output file (.pdf)") | ||
|
||
flag.Parse() | ||
} | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "invoice", | ||
Short: "Invoice generates invoices from the command line.", | ||
Long: `Invoice generates invoices from the command line.`, | ||
} | ||
|
||
var generateCmd = &cobra.Command{ | ||
Use: "generate", | ||
Short: "Generate an invoice", | ||
Long: `Generate an invoice`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
if importPath != "" { | ||
err := importData(importPath, &file, cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
pdf := gopdf.GoPdf{} | ||
pdf.Start(gopdf.Config{ | ||
PageSize: *gopdf.PageSizeA4, | ||
}) | ||
pdf.SetMargins(40, 40, 40, 40) | ||
pdf.AddPage() | ||
err := pdf.AddTTFFontData("Inter", interFont) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = pdf.AddTTFFontData("Inter-Bold", interBoldFont) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
writeLogo(&pdf, file.Logo, file.From) | ||
writeTitle(&pdf, file.Title, file.Id, file.Date) | ||
writeBillTo(&pdf, file.To) | ||
writeHeaderRow(&pdf) | ||
subtotal := 0.0 | ||
for i := range file.Items { | ||
q := 1 | ||
if len(file.Quantities) > i { | ||
q = file.Quantities[i] | ||
} | ||
|
||
r := 0.0 | ||
if len(file.Rates) > i { | ||
r = file.Rates[i] | ||
} | ||
|
||
writeRow(&pdf, file.Items[i], q, r) | ||
subtotal += float64(q) * r | ||
} | ||
if file.Note != "" { | ||
writeNotes(&pdf, file.Note) | ||
} | ||
writeTotals(&pdf, subtotal, subtotal*file.Tax, subtotal*file.Discount) | ||
if file.Due != "" { | ||
writeDueDate(&pdf, file.Due) | ||
} | ||
writeFooter(&pdf, file.Id) | ||
output = strings.TrimSuffix(output, ".pdf") + ".pdf" | ||
err = pdf.WritePdf(output) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Generated %s\n", output) | ||
|
||
return nil | ||
}, | ||
} | ||
import "github.com/maaslalani/invoice/cmd" | ||
|
||
func main() { | ||
rootCmd.AddCommand(generateCmd) | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cmd.Execute() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package utils | ||
|
||
var currencySymbols = map[string]string{ | ||
"USD": "$", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is switching away from camelCase necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go modules export by case. Uppercase functions get exported, lowercase are private to the module