Skip to content

Commit 1aaa603

Browse files
committed
add config generate command
1 parent 5b8b202 commit 1aaa603

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

cmd/config.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/fdddf/xcstrings-translator/internal/config"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// configCmd represents the config command
12+
var configCmd = &cobra.Command{
13+
Use: "config",
14+
Short: "Configuration management commands",
15+
Long: `Commands for managing configuration files.`,
16+
}
17+
18+
// generateCmd represents the generate subcommand
19+
var generateCmd = &cobra.Command{
20+
Use: "generate",
21+
Short: "Generate a default config.yaml file",
22+
Long: `Generate a default config.yaml file with example configuration values.`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
generateConfig()
25+
},
26+
// This prevents the normal config file loading for this command
27+
// by bypassing the PersistentPreRun that would normally run
28+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
29+
// Do nothing - this prevents the root's PersistentPreRun from executing
30+
},
31+
}
32+
33+
func init() {
34+
configCmd.AddCommand(generateCmd)
35+
36+
// Add the config command to the root command
37+
rootCmd.AddCommand(configCmd)
38+
}
39+
40+
func generateConfig() {
41+
cfg := config.DefaultConfig()
42+
43+
// Create the default configuration with example values
44+
yamlContent := `# Global configuration settings
45+
global:
46+
input_file: "` + cfg.Global.InputFile + `"
47+
output_file: "` + cfg.Global.OutputFile + `"
48+
source_language: "` + cfg.Global.SourceLanguage + `"
49+
target_languages:
50+
- "` + cfg.Global.TargetLanguages[0] + `"
51+
- "ja"
52+
- "ko"
53+
concurrency: ` + fmt.Sprintf("%d", cfg.Global.Concurrency) + `
54+
verbose: ` + fmt.Sprintf("%t", cfg.Global.Verbose) + `
55+
56+
# Google Translate API configuration
57+
google:
58+
api_key: "your-google-api-key-here"
59+
model: "` + cfg.Google.Model + `"
60+
glossary: ""
61+
62+
# DeepL API configuration
63+
deepl:
64+
api_key: "your-deepl-api-key-here"
65+
is_free: ` + fmt.Sprintf("%t", cfg.DeepL.IsFree) + `
66+
formality: "` + cfg.DeepL.Formality + `"
67+
68+
# Baidu Translate API configuration
69+
baidu:
70+
app_id: "your-baidu-app-id-here"
71+
app_secret: "your-baidu-app-secret-here"
72+
73+
# OpenAI compatible API configuration
74+
openai:
75+
api_key: "your-openai-api-key-here"
76+
api_base_url: "` + cfg.OpenAI.APIBaseURL + `"
77+
model: "` + cfg.OpenAI.Model + `"
78+
temperature: ` + fmt.Sprintf("%.1f", cfg.OpenAI.Temperature) + `
79+
max_tokens: ` + fmt.Sprintf("%d", cfg.OpenAI.MaxTokens) + `
80+
`
81+
82+
// Write the config file
83+
err := os.WriteFile("config.yaml", []byte(yamlContent), 0644)
84+
if err != nil {
85+
fmt.Fprintf(os.Stderr, "Error writing config file: %v\n", err)
86+
os.Exit(1)
87+
}
88+
89+
fmt.Println("Generated config.yaml with default values.")
90+
fmt.Println("Please edit the file with your API keys and settings.")
91+
}

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
var (
13-
Version = "0.0.1"
14-
Build = "2025-11-05"
13+
Version = "0.1.3"
14+
Build = "2025-12-08"
1515
Commit = "0.0.1"
1616
)
1717

0 commit comments

Comments
 (0)