-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
166 lines (147 loc) · 5.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (c) Ronan LE MEILLAT 2024
* Licensed under the AGPLv3 License
* https://www.gnu.org/licenses/agpl-3.0.html
*/
// Package main is the entry point for the AzurePDFTranslator application.
// It provides a command-line interface for translating PDF documents using Azure Translator and storing the translated documents in Azure Blob Storage.
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"translator/internal/translator"
"github.com/sirupsen/logrus"
)
// Constants for environment variable names
const (
envTranslatorEndpoint = "TRANSLATOR_ENDPOINT"
envTranslatorKey = "TRANSLATOR_KEY"
envTranslatorRegion = "TRANSLATOR_REGION"
envBlobAccount = "BLOB_STORAGE_ACCOUNT_NAME"
envBlobAccountKey = "BLOB_STORAGE_ACCOUNT_KEY"
envBlobContainer = "BLOB_STORAGE_CONTAINER_NAME"
)
var verbose bool
// main is the entry point of the application.
// It parses command-line arguments, loads configuration from file if specified, validates inputs, and performs the translation.
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
// run is the main logic of the application.
// It sets up the translator configuration, validates inputs, and performs the translation.
// It returns an error if any step fails.
func run() error {
// Set up logging
log := logrus.New()
log.SetOutput(os.Stdout)
// Parse command-line arguments
endpoint := flag.String("endpoint", os.Getenv(envTranslatorEndpoint), "Azure Translator API endpoint")
key := flag.String("key", os.Getenv(envTranslatorKey), "Azure Translator API key")
region := flag.String("region", os.Getenv(envTranslatorRegion), "Azure region")
in := flag.String("in", "", "Input file path")
from := flag.String("from", "", "Source language")
to := flag.String("to", "", "Target language")
out := flag.String("out", "", "Destination file path")
timeout := flag.Int("timeout", 30, "Timeout in seconds")
blobAccount := flag.String("blobAccount", os.Getenv(envBlobAccount), "Azure Blob Storage account name")
blobAccountKey := flag.String("blobAccountKey", os.Getenv(envBlobAccountKey), "Azure Blob Storage account key")
blobContainer := flag.String("blobContainer", os.Getenv(envBlobContainer), "Azure Blob Storage container name")
configFile := flag.String("config", "", "Configuration file path")
flag.BoolVar(&verbose, "v", false, "enable verbose logging")
flag.Parse()
// Load configuration from file if specified
if *configFile != "" {
err := loadConfigFromFile(*configFile, endpoint, key, region, blobAccount, blobAccountKey, blobContainer, &verbose)
if err != nil {
return err
}
}
// Validate inputs
if err := validateInputs(*endpoint, *key, *region, *in, *out, *to, *blobAccount, *blobAccountKey, *blobContainer); err != nil {
return err
}
// Set up translator configuration
config := translator.TranslatorConfig{
TranslatorEndpoint: *endpoint,
TranslatorKey: *key,
TranslatorRegion: *region,
BlobAccountName: *blobAccount,
BlobAccountKey: *blobAccountKey,
BlobContainerName: *blobContainer,
Timeout: *timeout,
Verbose: verbose,
Logger: log,
}
// Set log level based on verbose flag
if config.Verbose {
log.SetLevel(logrus.DebugLevel)
} else {
log.SetLevel(logrus.InfoLevel)
}
// Perform the translation
log.Info("Starting document translation")
return translator.TranslateDocument(*in, *out, *from, *to, config)
}
// loadConfigFromFile loads the translator configuration from a JSON file.
// It updates the endpoint, key, region, blobAccount, blobAccountKey, blobContainer, and verbose variables with the values from the file.
func loadConfigFromFile(configFile string, endpoint, key, region, blobAccount, blobAccountKey, blobContainer *string, verbose *bool) error {
file, err := os.Open(configFile)
if err != nil {
return err
}
defer file.Close()
var config translator.TranslatorConfig
err = json.NewDecoder(file).Decode(&config)
if err != nil {
return err
}
*endpoint = config.TranslatorEndpoint
*key = config.TranslatorKey
*region = config.TranslatorRegion
*blobAccount = config.BlobAccountName
*blobAccountKey = config.BlobAccountKey
*blobContainer = config.BlobContainerName
*verbose = config.Verbose
return nil
}
// validateInputs checks if all the required inputs are provided.
// It returns an error if any required input is missing.
func validateInputs(endpoint, key, region, in, out, to, blobAccount, blobAccountKey, blobContainer string) error {
missingArgs := []string{}
if endpoint == "" {
missingArgs = append(missingArgs, "endpoint")
}
if key == "" {
missingArgs = append(missingArgs, "key")
}
if region == "" {
missingArgs = append(missingArgs, "region")
}
if in == "" {
missingArgs = append(missingArgs, "in")
}
if out == "" {
missingArgs = append(missingArgs, "out")
}
if to == "" {
missingArgs = append(missingArgs, "to")
}
if blobAccount == "" {
missingArgs = append(missingArgs, "blobAccount")
}
if blobAccountKey == "" {
missingArgs = append(missingArgs, "blobAccountKey")
}
if blobContainer == "" {
missingArgs = append(missingArgs, "blobContainer")
}
if len(missingArgs) > 0 {
return fmt.Errorf("missing required arguments: %s", strings.Join(missingArgs, ", "))
}
return nil
}