-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
100 lines (86 loc) · 2.8 KB
/
cli.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
package cli
import (
"context"
"fmt"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/codevideo/codevideo-cli/cli/config"
"github.com/codevideo/codevideo-cli/cli/detector"
"github.com/codevideo/codevideo-cli/cli/generator"
"github.com/codevideo/codevideo-cli/server"
"github.com/spf13/cobra"
)
// Execute runs the CLI workflow with the provided project data
func Execute(cmd *cobra.Command) error {
// Load configuration from flags
if err := config.LoadFromFlags(cmd); err != nil {
return fmt.Errorf("failed to load configuration: %w", err)
}
// Get project JSON data from flags
projectJSON, _ := cmd.Flags().GetString("project")
if projectJSON == "" {
cmd.Help()
return nil
}
// Store project JSON in config
config.GlobalConfig.ProjectJSON = projectJSON
// Create a context with cancellation
ctx, cancel := context.WithCancel(context.Background())
setupCancellation(ctx, cancel)
log.Printf("Analyzing project JSON: %s", projectJSON)
course, lesson, actions, err := detector.DetectProjectType(projectJSON)
if err != nil {
return fmt.Errorf("failed to detect project type: %w", err)
}
generator := generator.NewGenerator()
if course != nil {
log.Printf("Starting Course workflow processing")
fmt.Println("Detected project type: Course")
fmt.Println("/> CodeVideo generation in progress...")
manifests := generator.GenerateFromCourse(*course)
// for each manifest, get its absolute path and call server.ProcessJob
for _, manifest := range manifests {
manifestPath, err := generator.SaveManifest(manifest)
if err != nil {
return fmt.Errorf("failed to save manifest: %w", err)
}
server.ProcessJob(manifestPath, "cli")
}
}
if lesson != nil {
log.Printf("Starting Lesson workflow processing")
fmt.Println("Detected project type: Lesson")
fmt.Println("/> CodeVideo generation in progress...")
manifest := generator.GenerateFromLesson(*lesson)
manifestPath, err := generator.SaveManifest(manifest)
if err != nil {
return fmt.Errorf("failed to save manifest: %w", err)
}
server.ProcessJob(manifestPath, "cli")
}
if actions != nil {
log.Printf("Starting Actions workflow processing")
fmt.Println("Detected project type: Actions")
fmt.Println("/> CodeVideo generation in progress...")
manifest := generator.GenerateFromActions(*actions)
manifestPath, err := generator.SaveManifest(manifest)
if err != nil {
return fmt.Errorf("failed to save manifest: %w", err)
}
server.ProcessJob(manifestPath, "cli")
}
return nil
}
// setupCancellation configures graceful shutdown on interrupt
func setupCancellation(ctx context.Context, cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
go func() {
<-c
fmt.Println("\nCancelling operations...")
cancel()
// Give a little time for cleanup
time.Sleep(1 * time.Second)
os.Exit(0)
}()
}