Skip to content

Commit

Permalink
Add validate-xml command for efactura-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
printesoi committed May 15, 2024
1 parent cc38ef3 commit 074d706
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/efactura-cli/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/spf13/cobra"
)

// apiCmd represents the auth command
// apiCmd represents the api command
var apiCmd = &cobra.Command{
Use: "api",
Short: "e-factura API calls",
Expand Down
14 changes: 14 additions & 0 deletions cmd/efactura-cli/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

"github.com/spf13/cobra"

efacturaclient "github.com/printesoi/e-factura-go/client"
"github.com/printesoi/e-factura-go/constants"
"github.com/printesoi/e-factura-go/efactura"
"github.com/printesoi/e-factura-go/oauth2"
)
Expand Down Expand Up @@ -74,3 +76,15 @@ func newEfacturaClient(ctx context.Context, cmd *cobra.Command) (client *efactur
}
return
}

func newEfacturaPublicClient(cmd *cobra.Command) (client *efactura.Client, err error) {
publicApiClient, err := efacturaclient.NewPublicApiClient(
efacturaclient.PublicApiClientBaseURL(constants.PublicApiBaseProd),
)
if err != nil {
return nil, err
}

client, err = efactura.NewClient(efactura.ClientPublicApiClient(publicApiClient))
return
}
29 changes: 29 additions & 0 deletions cmd/efactura-cli/cmd/public_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 Victor Dodon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

package cmd

import (
"github.com/spf13/cobra"
)

// publicApiCmd represents the public-api command
var publicApiCmd = &cobra.Command{
Use: "public-api",
Short: "e-factura Public API calls",
}

func init() {
rootCmd.AddCommand(publicApiCmd)
}
86 changes: 86 additions & 0 deletions cmd/efactura-cli/cmd/public_api_validate_xml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2024 Victor Dodon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

package cmd

import (
"context"
"fmt"
"os"

"github.com/printesoi/e-factura-go/efactura"
"github.com/spf13/cobra"
)

const (
flagNameValidateInFilePath = "xml-file"
flagNameValidateStandard = "standard"
)

// publicApiValidateCmd represents the `api download` command
var publicApiValidateCmd = &cobra.Command{
Use: "validate-xml",
Short: "Validate XML",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := newEfacturaPublicClient(cmd)
if err != nil {
cmd.SilenceUsage = true
return err
}

fvXmlFile, err := cmd.Flags().GetString(flagNameValidateInFilePath)
if err != nil {
return err
}
f, err := os.Open(fvXmlFile)
if err != nil {
cmd.SilenceUsage = true
return err
}
defer f.Close()

fvStandard, err := cmd.Flags().GetString(flagNameValidateStandard)
if err != nil {
return err
}
switch fvStandard {
case string(efactura.ValidateStandardFACT1), string(efactura.ValidateStandardFCN):
default:
return fmt.Errorf("invalid standard: `%s`", fvStandard)
}

validateRes, err := client.ValidateXML(context.Background(), f, efactura.ValidateStandard(fvStandard))
if err != nil {
cmd.SilenceUsage = true
return err
}
if !validateRes.IsOk() {
cmd.SilenceUsage = true
return fmt.Errorf("error validating XML: %s", validateRes.GetFirstMessage())
}
fmt.Printf("validate %s: OK\n", fvStandard)
return nil
},
}

func init() {
publicApiValidateCmd.Flags().StringP(flagNameValidateInFilePath, "f", "", "Path of the input XML file")
_ = publicApiValidateCmd.MarkFlagRequired(flagNameValidateInFilePath)

publicApiValidateCmd.Flags().StringP(flagNameValidateStandard, "s", "", fmt.Sprintf("Standard for validation (%s/%s)",
efactura.ValidateStandardFACT1, efactura.ValidateStandardFCN))
_ = publicApiValidateCmd.MarkFlagRequired(flagNameValidateStandard)

publicApiCmd.AddCommand(publicApiValidateCmd)
}
4 changes: 2 additions & 2 deletions cmd/efactura-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "client",
Short: "A CLI client for the ANAF e-factura API",
Short: "A CLI client for the ANAF e-factura APIs",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
Expand Down Expand Up @@ -78,7 +78,7 @@ func initConfig() {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".cobra" (without extension).
// Search config in home directory with name ".e-factura.yaml".
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".e-factura")
Expand Down
1 change: 0 additions & 1 deletion efactura/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type ClientConfig struct {
ApiClient *client.ApiClient
// the client to use for making requests to the ANAF public APIs.
PublicApiClient *client.PublicApiClient
Ctx context.Context
}

// ClientConfigOption allows gradually modifying a ClientConfig
Expand Down

0 comments on commit 074d706

Please sign in to comment.