-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_utils.go
More file actions
107 lines (92 loc) · 2.53 KB
/
Copy pathcmd_utils.go
File metadata and controls
107 lines (92 loc) · 2.53 KB
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
package wordcounter
import (
"fmt"
"os"
)
// ExportConfig holds configuration for export operations
type ExportConfig struct {
Type string
Path string
}
// CounterExporter provides common export functionality for counters
type CounterExporter struct {
counter interface {
ExportCSV(filename ...string) (string, error)
ExportExcel(filename ...string) error
ExportTable() string
}
config ExportConfig
}
// NewCounterExporter creates a new CounterExporter
func NewCounterExporter(counter interface {
ExportCSV(filename ...string) (string, error)
ExportExcel(filename ...string) error
ExportTable() string
}, config ExportConfig) *CounterExporter {
return &CounterExporter{
counter: counter,
config: config,
}
}
// Export performs the export operation based on configuration
func (ce *CounterExporter) Export() error {
switch ce.config.Type {
case ExportTypeCSV:
return ce.exportCSV()
case ExportTypeExcel:
return ce.exportExcel()
case ExportTypeTable:
return ce.exportTable()
default:
return NewInvalidInputError(fmt.Sprintf("unsupported export type: %s", ce.config.Type))
}
}
func (ce *CounterExporter) exportCSV() error {
csvData, err := ce.counter.ExportCSV(ce.config.Path)
if err != nil {
return NewExportError("CSV export", err)
}
fmt.Println(csvData)
return nil
}
func (ce *CounterExporter) exportExcel() error {
if err := ce.counter.ExportExcel(ce.config.Path); err != nil {
return NewExportError("Excel export", err)
}
fmt.Printf("Excel file exported to: %s\n", ce.config.Path)
return nil
}
func (ce *CounterExporter) exportTable() error {
fmt.Println(ce.counter.ExportTable())
return nil
}
// ValidatePath validates if a path exists
func ValidatePath(path string) error {
if path == "" {
return NewInvalidInputError("path cannot be empty")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return NewFileNotFoundError(path, err)
}
return nil
}
// ValidateExportType validates if an export type is supported
func ValidateExportType(exportType string) error {
switch exportType {
case ExportTypeTable, ExportTypeCSV, ExportTypeExcel:
return nil
default:
return NewInvalidInputError(fmt.Sprintf("unsupported export type: %s, supported types: %s, %s, %s",
exportType, ExportTypeTable, ExportTypeCSV, ExportTypeExcel))
}
}
// ValidateMode validates if a mode is supported
func ValidateMode(mode string) error {
switch mode {
case ModeDir, ModeFile:
return nil
default:
return NewInvalidInputError(fmt.Sprintf("unsupported mode: %s, supported modes: %s, %s",
mode, ModeDir, ModeFile))
}
}