-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
116 lines (97 loc) · 3.05 KB
/
config.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
package config
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"github.com/spf13/cobra"
)
// Config holds all configuration settings for the CLI
type Config struct {
// Project settings
ProjectJSON string
// Output settings
OutputDir string
OutputFileName string
OutputFormat string
// Processing settings
Resolution string
Orientation string
// Environment
OperatingSystem string
Environment string
}
// Global configuration pointer
var GlobalConfig *Config
// Initialize with default configuration
func init() {
GlobalConfig = DefaultConfig()
}
// DefaultConfig returns the default configuration
func DefaultConfig() *Config {
return &Config{
OutputDir: ".",
OutputFileName: fmt.Sprintf("CodeVideo-%s", time.Now().Format("2006-01-02-15-04-05")),
OutputFormat: "mp4",
Resolution: "1080p", // 1080p by default, could be 4K
Orientation: "landscape", // Default to landscape
OperatingSystem: runtime.GOOS,
Environment: "local",
}
}
// LoadFromFlags updates the configuration from command flags
func LoadFromFlags(cmd *cobra.Command) error {
// Read output path if provided
output, _ := cmd.Flags().GetString("output")
if output != "" {
GlobalConfig.OutputDir = filepath.Dir(output)
GlobalConfig.OutputFileName = filepath.Base(output)
// Remove extension if present
GlobalConfig.OutputFileName = GlobalConfig.OutputFileName[:len(GlobalConfig.OutputFileName)-len(filepath.Ext(GlobalConfig.OutputFileName))]
}
// Read resolution if provided
resolution, _ := cmd.Flags().GetString("resolution")
if resolution != "" {
GlobalConfig.Resolution = resolution
}
// Read orientation if provided
orientation, _ := cmd.Flags().GetString("orientation")
if orientation != "" {
GlobalConfig.Orientation = orientation
}
// Ensure output directories exist
return GlobalConfig.EnsureOutputDirs()
}
// EnsureOutputDirs ensures that all required output directories exist
func (c *Config) EnsureOutputDirs() error {
// Make sure output directory exists
if err := os.MkdirAll(c.OutputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
// Create temp directories for processing
tempDirs := []string{
filepath.Join(os.TempDir(), "codevideo", "new"),
filepath.Join(os.TempDir(), "codevideo", "error"),
filepath.Join(os.TempDir(), "codevideo", "success"),
filepath.Join(os.TempDir(), "codevideo", "video"),
}
for _, dir := range tempDirs {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create temp directory %s: %w", dir, err)
}
}
return nil
}
// GetTempDir returns the path to a specific temporary directory
func (c *Config) GetTempDir(dirType string) string {
return filepath.Join(os.TempDir(), "codevideo", dirType)
}
// GenerateOutputPath generates the full path for an output file
func (c *Config) GenerateOutputPath(suffix string) string {
filename := c.OutputFileName
if suffix != "" {
filename = fmt.Sprintf("%s-%s", filename, suffix)
}
return filepath.Join(c.OutputDir, fmt.Sprintf("%s.%s", filename, c.OutputFormat))
}