Skip to content

Commit

Permalink
[NEW] add pdf export (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
HappyTobi committed Apr 24, 2023
1 parent a7ac4c2 commit ac97a51
Show file tree
Hide file tree
Showing 15 changed files with 390 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*.dll
*.so
*.dylib
.DS_Store
*.pdf

# Test binary, built with `go test -c`
*.test
Expand Down
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ The cli tool brings the http api to the terminal.
| --- | --- |
| `warp info` | Get information about the warp charger |
| `warp info version` | Get the version of the warp charger |
| `warp info update` | Check if an warp charger update is available |
| `warp info version update` | Check if an warp charger update is available |
| `warp info name` | Get the name of the warp charger |
| `warp infor display-name` | Get the display name of the warp charger |
| `warp info modules` | Get the modules of the warp charger |
| `warp info features` | Get the features of the warp charger |
| `warp meater values` | Get the meater values of the warp charger |
| `warp meter values` | Get the meter values of the warp charger |
| `warp users list` | Get the users of the warp charger |
| `warp charge-tracker` | Get information about the charge tracker |
| `warp charge-tracker log` | Get the charge tracker log (csv) |
| `warp charge-tracker log` | Get the charge tracker log (csv or pdf) |
| `warp version` | Get the version of the warp cli |

Each command has a help page, which can be accessed with the `-h` or `--help` flag.
Expand All @@ -31,11 +31,21 @@ The configuration file is located at `~/.warp.yaml`.
csv:
comma: ; # separator for csv
header: true # add header to csv
date_time:
time_format: 15:04:05 02-01-2006 # date time format
time_zone: Europe/Berlin # time zone to print the date time
power:
price: "0.35" # price per kWh
pdf:
image_path: /root/.config/warp/logo.png # path to the logo image for the pdf
print_header: false # print header in pdf
settings:
date_time:
time_format: 15:04:05 02-01-2006 #date time format
time_zone: Europe/Berlin # time zone to print the date time
power:
price: "35.55" # price per kWh
user: # user information that will be added to the pdf if print_header is true
city: internet
firstname: happy
lastname: tobi
postcode: "0000"
street: githubroad
```
#### Build warp cli
Expand Down Expand Up @@ -80,6 +90,20 @@ $ warp charge-tracker log -c "http://mywarp.ip -u "username" -p "password"
]
```

### Charge tracker csv export
```console
$ warp charge-tracker log -c "http://mywarp.ip -u "username" -p "password" -o csv -f ~/Desktop/my-charge-document.csv
```

### Charge tracker pdf export
```console
$ warp charge-tracker log -c "http://mywarp.ip -u "username" -p "password" -o pdf -f ~/Desktop/my-charge-document.pdf
```

Example pdf document:
![Example pdf document](doc/charge-pdf-example.png)


## Warp Charger information

[Product Page](https://www.warp-charger.com)
Expand Down
6 changes: 5 additions & 1 deletion cmd/chargeTracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ func validateCustomOutputFormat(cmd *cobra.Command, args []string) error {
output, _ := cmd.Flags().GetString("output")
file, _ := cmd.Flags().GetString("file")

if output != "json" && output != "yaml" && output != "csv" {
if output != "json" && output != "yaml" && output != "csv" && output != "pdf" {
return fmt.Errorf("invalid output format: %s", output)
}

if output == "csv" && file == "" {
return fmt.Errorf("output format csv requires a file to be set")
}

if output == "pdf" && file == "" {
return fmt.Errorf("output format pdf requires a file to be set")
}

return nil
}
50 changes: 43 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/HappyTobi/warp/pkg/cmd/settings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -33,24 +35,58 @@ func init() {
cobra.OnInitialize(initConfig)
}

// TODO move to settings package
func initConfig() {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

viper.AddConfigPath(home)
configPath := filepath.Join(home, ".config", "warp")

migrated := false
if _, err := os.Stat(filepath.Join(home, ".warp.yaml")); !os.IsNotExist(err) {
fmt.Println("Configurations file will me moved and updated to new format")
_ = os.MkdirAll(configPath, os.ModePerm)
if err := os.Rename(filepath.Join(home, "warp.yaml"), filepath.Join(configPath, "warp.yaml")); err != nil {
fmt.Println("Error while moving config file")
os.Exit(1)
}
os.Remove(filepath.Join(home, ".warp.yaml"))
migrated = true
}

viper.AddConfigPath(configPath)
viper.SetConfigType("yaml")
viper.SetConfigName(".warp")
viper.SetConfigName("warp")

if err := viper.ReadInConfig(); err == nil {
if err := viper.ReadInConfig(); err == nil && !migrated {
return
}

viper.SetDefault("date_time.time_zone", "Europe/Berlin")
viper.SetDefault("date_time.time_format", "15:04:05 02-01-2006")
viper.SetDefault("power.price", "0.35")
viper.SetDefault("settings.user.firstname", "happy")
viper.SetDefault("settings.user.lastname", "tobi")
viper.SetDefault("settings.user.street", "githubroad")
viper.SetDefault("settings.user.postcode", "0000")
viper.SetDefault("settings.user.city", "internet")

viper.SetDefault("settings.date_time.time_zone", "Europe/Berlin")
viper.SetDefault("settings.date_time.time_format", "15:04:05 02-01-2006")
viper.SetDefault("settings.power.price", "0.35")

//csv settings
viper.SetDefault("csv.comma", ";")
viper.SetDefault("csv.header", true)
if err := viper.SafeWriteConfig(); err != nil {

//pdf settings
viper.SetDefault("pdf.print_header", false)
imagePath := filepath.Join(configPath, "logo.png")
viper.SetDefault("pdf.image_path", imagePath)

if err := settings.StoreImage(imagePath); err != nil {
fmt.Print("Error while storing image")
os.Exit(1)
}

if err := viper.WriteConfig(); err != nil {
fmt.Print(err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func ValidateOutputformat(cmd *cobra.Command, args []string) error {
return err
}

if output != "json" && output != "yaml" {
if output != "json" && output != "yaml" && output != "yml" {
return fmt.Errorf("invalid output format: %s", output)
}

Expand Down
Binary file added doc/charge-pdf-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ go 1.19
require github.com/spf13/cobra v1.6.1

require (
github.com/boombuler/barcode v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/johnfercher/maroto v0.40.0 // indirect
github.com/jung-kurt/gofpdf v1.16.2 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -111,6 +114,8 @@ github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
Expand All @@ -122,8 +127,13 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/johnfercher/maroto v0.40.0 h1:jBPC9JQ030SSbusWwIbfbsgdcyk6OZtBe428EOXopI0=
github.com/johnfercher/maroto v0.40.0/go.mod h1:qeujdhKT+677jMjGWlIa5OCgR04GgIHvByJ6pSC+hOw=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc=
github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -135,12 +145,19 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.7/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245 h1:K1Xf3bKttbF+koVGaX5xngRIZ5bVjbmPnaxE/dR08uY=
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
Expand All @@ -154,9 +171,11 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU=
github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -194,6 +213,7 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
Loading

0 comments on commit ac97a51

Please sign in to comment.