From cc38ef399ab19d29d213d386b194fc6fe4509811 Mon Sep 17 00:00:00 2001 From: Victor Dodon Date: Sat, 4 May 2024 20:51:33 +0300 Subject: [PATCH] Add get-message-state command for efactura-cli --- cmd/efactura-cli/cmd/api_get_message_state.go | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 cmd/efactura-cli/cmd/api_get_message_state.go diff --git a/cmd/efactura-cli/cmd/api_get_message_state.go b/cmd/efactura-cli/cmd/api_get_message_state.go new file mode 100644 index 0000000..0c3d937 --- /dev/null +++ b/cmd/efactura-cli/cmd/api_get_message_state.go @@ -0,0 +1,68 @@ +// 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" + "github.com/spf13/cobra" +) + +const ( + flagNameGetMessageStateUploadIndex = "upload-index" +) + +// apiGetMessageStateCmd represents the `api download` command +var apiGetMessageStateCmd = &cobra.Command{ + Use: "get-message-state", + Short: "Get message state for an upload index", + RunE: func(cmd *cobra.Command, args []string) error { + ctx := context.Background() + client, err := newEfacturaClient(ctx, cmd) + if err != nil { + cmd.SilenceUsage = true + return err + } + + fvUploadIndex, err := cmd.Flags().GetInt64(flagNameGetMessageStateUploadIndex) + if err != nil { + return err + } + + res, err := client.GetMessageState(ctx, fvUploadIndex) + if err != nil { + cmd.SilenceUsage = true + return fmt.Errorf("get message state failed: %w", err) + } + if res.IsOk() { + fmt.Printf("Message for upload index %d: ok, download_id=%d", fvUploadIndex, res.GetDownloadID()) + } else if res.IsProcessing() { + fmt.Printf("Message for upload index %d: processing", fvUploadIndex) + } else if res.IsInvalidXML() { + fmt.Printf("Message for upload index %d: invalid XML, download_id=%d", fvUploadIndex, res.GetDownloadID()) + } else if res.IsNok() { + fmt.Printf("Message for upload index %d: nok, download_id=%d", fvUploadIndex, res.GetDownloadID()) + } + + return nil + }, +} + +func init() { + apiGetMessageStateCmd.Flags().Int64(flagNameGetMessageStateUploadIndex, 0, "Upload index") + _ = apiGetMessageStateCmd.MarkFlagRequired(flagNameGetMessageStateUploadIndex) + + apiCmd.AddCommand(apiGetMessageStateCmd) +}