Skip to content

Commit

Permalink
Add transmit command
Browse files Browse the repository at this point in the history
This command is intended to demonstrate sending an invoice to SdI.

Added CA cert and verbose flags for better control from the command line.
The verbose flag provides more detailed debugging information about
the package's execution.
The ca-cert flag accepts a path to the CA Certificate.

Example usage of the command:

```console
$ go run ./cmd/gobl.fatturapa transmit --ca-cert ./ca-all.pem --cert ./SDI-B85905495.pem --key ./key_client.key --verbose invoice.xml
```

Additionally, separated responsibilities and added flexibility
in certificate storage.
The CLI handles the loading of the certificate path,
while the SdI package uses the loaded key.

Co-authored-by: Alex Malaszkiewicz <[email protected]>
Co-authored-by: Grzeg Lisowski <[email protected]>
  • Loading branch information
torrocus and noplisu committed Jun 3, 2024
1 parent 20a9abf commit a9d1e55
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/gobl.fatturapa/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (o *rootOpts) cmd() *cobra.Command {

cmd.AddCommand(versionCmd())
cmd.AddCommand(convert(o).cmd())
cmd.AddCommand(transmit(o).cmd())

return cmd
}
Expand Down
126 changes: 126 additions & 0 deletions cmd/gobl.fatturapa/transmit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"fmt"
"io"
"os"
"path/filepath"

sdi "github.com/invopop/gobl.fatturapa/sdi"
"github.com/spf13/cobra"
)

type transmitOpts struct {
*rootOpts
config *sdi.Config
}

func transmit(o *rootOpts) *transmitOpts {
return &transmitOpts{rootOpts: o}
}

func (c *transmitOpts) cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "transmit [file] [environment]",
Short: "Transmit a FatturaPA XML file to SdI in selected environment",
RunE: c.runE,
}

f := cmd.Flags()
f.Bool("verbose", false, "Logs all requests into the console")
f.String("ca-cert", "", "Path to a file containing the CA certificate")
f.String("cert", "", "Path to a file containing the SDI PEM certificate")
f.String("key", "", "Path to a file containing the sender PEM RSA private key")
f.String("env", "test", "Environment for running command")
_ = cmd.MarkFlagRequired("ca-cert")
_ = cmd.MarkFlagRequired("cert")
_ = cmd.MarkFlagRequired("key")

return cmd
}

func (c *transmitOpts) runE(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
verbose, err := cmd.Flags().GetBool("verbose")
if err != nil {
return err
}

config, err := getConfigByEnv(cmd)
if err != nil {
return err
}

caCertPool, err := loadDataFromFlag(cmd, "ca-cert")
if err != nil {
return err
}

certPEM, err := loadDataFromFlag(cmd, "cert")
if err != nil {
return err
}

keyPEM, err := loadDataFromFlag(cmd, "key")
if err != nil {
return err
}

client := sdi.NewClient(
sdi.WithDebugMode(verbose),
sdi.WithCACertPool(caCertPool),
sdi.WithClientCertPair(certPEM, keyPEM),
)

input, err := openInput(cmd, args)
if err != nil {
return err
}
defer input.Close() // nolint:errcheck

c.config = config

data, err := io.ReadAll(input)
if err != nil {
return fmt.Errorf("reading input: %w", err)
}
fileName := filepath.Base(args[0])

invOpts := sdi.InvoiceOpts{FileName: fileName, FileBody: data}

resp, err := sdi.SendInvoice(ctx, invOpts, client, *c.config)
if err != nil {
return fmt.Errorf("sending error: %w", err)
}

fmt.Printf("Invoice sent to SdI.\nHere is the response: %v\n", resp.Body.Response)

return nil
}

func getConfigByEnv(cmd *cobra.Command) (*sdi.Config, error) {
env, _ := cmd.Flags().GetString("env")
configs := map[string]*sdi.Config{
"dev": &sdi.DevelopmentSdIConfig,
"test": &sdi.TestSdIConfig,
"prod": &sdi.ProductionSdIConfig,
}
config, exists := configs[env]
if !exists {
return nil, fmt.Errorf("invalid environment: %s. Allowed values are 'dev', 'test', 'prod'", env)
}

return config, nil
}

func loadDataFromFlag(cmd *cobra.Command, flagName string) ([]byte, error) {
filePath, err := cmd.Flags().GetString(flagName)
if err != nil {
return nil, err
}
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return data, nil
}

0 comments on commit a9d1e55

Please sign in to comment.