Skip to content

Commit

Permalink
Add get-message-state command for efactura-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
printesoi committed May 4, 2024
1 parent ad16ad4 commit cc38ef3
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions cmd/efactura-cli/cmd/api_get_message_state.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit cc38ef3

Please sign in to comment.