Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
feat: support cli dry run
wulien336490fa2ee0
diff --git a/cmd/cli/main.go b/cmd/cli/main.goindex ea98b17c..3e8de737 100644--- a/cmd/cli/main.go+++ b/cmd/cli/main.go@@ -17,6 +17,15 @@ func main() {log.InitLogger()defer log.GetLogger().Sync()+ cmd, err := cli.Parse(os.Args[1:])+ if err != nil {+ writeAndExit(errorResponse(err, pipeline.ErrorKindUsage))+ }+ if cmd.DryRun {+ writeAndExit(cli.Execute(context.Background(), nil, cmd))+ return+ }+if !config.LoadConfig() {writeAndExit(pipeline.Response{OK: false,@@ -33,10 +42,6 @@ func main() {if err := deps.CheckDependency(); err != nil {writeAndExit(errorResponse(err, pipeline.ErrorKindDependency))}- cmd, err := cli.Parse(os.Args[1:])- if err != nil {- writeAndExit(errorResponse(err, pipeline.ErrorKindUsage))- }svc := service.NewService()adapter := pipeline.NewServiceAdapter(svc)writeAndExit(cli.Execute(context.Background(), adapter, cmd))diff --git a/internal/cli/commands.go b/internal/cli/commands.goindex df2d0769..efa5b616 100644--- a/internal/cli/commands.go+++ b/internal/cli/commands.go@@ -7,11 +7,13 @@ import ("fmt""io""krillin-ai/internal/pipeline"+ "os""strings")type Command struct {Name string+ DryRun boolSubtitle pipeline.SubtitleRequestTTS pipeline.TTSRequestRender pipeline.RenderRequest@@ -42,6 +44,9 @@ func Parse(args []string) (Command, error) {}func Execute(ctx context.Context, svc pipeline.StageService, cmd Command) pipeline.Response {+ if cmd.DryRun {+ return dryRun(cmd)+ }switch cmd.Name {case "subtitle":resp, err := pipeline.GenerateSubtitles(ctx, svc, cmd.Subtitle)@@ -74,6 +79,7 @@ func parseSubtitle(name string, args []string) (Command, error) {captionSource := fs.String("caption-source", string(pipeline.CaptionSourceAny), "caption source")bilingualTop := fs.Bool("bilingual-top", false, "put target subtitle on top")maxWordOneLine := fs.Int("max-word-one-line", 0, "max words per line")+ dryRun := fs.Bool("dry-run", false, "validate command without running external services")input := ""parseArgs := argsif len(args) > 0 && !strings.HasPrefix(args[0], "-") {@@ -90,7 +96,8 @@ func parseSubtitle(name string, args []string) (Command, error) {return Command{}, errors.New("subtitle requires input")}return Command{- Name: name,+ Name: name,+ DryRun: *dryRun,Subtitle: pipeline.SubtitleRequest{Input: input,Workdir: *workdir,@@ -114,6 +121,7 @@ func parseTTS(name string, args []string) (Command, error) {video := fs.String("video", "", "input video")voice := fs.String("voice", "", "voice")voiceCloneSource := fs.String("voice-clone-source", "", "voice clone source")+ dryRun := fs.Bool("dry-run", false, "validate command without running external services")if err := fs.Parse(args); err != nil {return Command{}, err}@@ -121,7 +129,8 @@ func parseTTS(name string, args []string) (Command, error) {return Command{}, errors.New("tts requires --input-srt")}return Command{- Name: name,+ Name: name,+ DryRun: *dryRun,TTS: pipeline.TTSRequest{Workdir: *workdir,TaskID: *taskID,@@ -144,11 +153,13 @@ func parseRender(name string, args []string, horizontal bool) (Command, error) {dubbed := fs.Bool("dubbed", false, "render dubbed video")majorTitle := fs.String("major-title", "", "vertical major title")minorTitle := fs.String("minor-title", "", "vertical minor title")+ dryRun := fs.Bool("dry-run", false, "validate command without running external services")if err := fs.Parse(args); err != nil {return Command{}, err}return Command{- Name: name,+ Name: name,+ DryRun: *dryRun,Render: pipeline.RenderRequest{Workdir: *workdir,TaskID: *taskID,@@ -167,6 +178,7 @@ func parsePipeline(name string, args []string) (Command, error) {fs := newFlagSet(name)outputs := fs.String("outputs", "subtitle", "outputs")async := fs.Bool("async", false, "run async")+ dryRun := fs.Bool("dry-run", false, "validate command without running external services")if err := fs.Parse(args); err != nil {return Command{}, err}@@ -174,7 +186,8 @@ func parsePipeline(name string, args []string) (Command, error) {return Command{}, err}return Command{- Name: name,+ Name: name,+ DryRun: *dryRun,Pipeline: pipeline.PipelineRequest{Outputs: *outputs,Async: *async,@@ -182,6 +195,73 @@ func parsePipeline(name string, args []string) (Command, error) {}, nil}+func dryRun(cmd Command) pipeline.Response {+ switch cmd.Name {+ case "subtitle":+ return dryRunManifest(cmd.Subtitle.Workdir, cmd.Subtitle.TaskID, pipeline.StageSubtitle, func(m *pipeline.Manifest) {+ m.InputURL = cmd.Subtitle.Input+ m.OriginLanguage = cmd.Subtitle.OriginLang+ m.TargetLanguage = cmd.Subtitle.TargetLang+ m.CaptionSource = string(cmd.Subtitle.CaptionSource)+ })+ case "tts":+ return dryRunManifest(cmd.TTS.Workdir, cmd.TTS.TaskID, pipeline.StageTTS, nil)+ case "render-horizontal":+ return dryRunManifest(cmd.Render.Workdir, cmd.Render.TaskID, pipeline.StageRenderHorizontal, nil)+ case "render-vertical":+ return dryRunManifest(cmd.Render.Workdir, cmd.Render.TaskID, pipeline.StageRenderVertical, nil)+ case "pipeline":+ return pipeline.Response{OK: true, Stage: pipeline.StagePipeline}+ default:+ return pipeline.Response{+ OK: false,+ Error: &pipeline.Error{+ Kind: pipeline.ErrorKindUsage,+ Code: "unsupported_dry_run",+ Message: fmt.Sprintf("unsupported dry-run command: %s", cmd.Name),+ },+ }+ }+}++func dryRunManifest(workdir, taskID string, stage pipeline.Stage, update func(*pipeline.Manifest)) pipeline.Response {+ if workdir == "" {+ workdir = "."+ }+ manifest := pipeline.NewManifest(taskID, workdir)+ if update != nil {+ update(manifest)+ }+ if err := manifest.ApplyDefaultOutputs(); err != nil {+ return dryRunError(stage, workdir, taskID, "apply_outputs_failed", err)+ }+ manifest.MarkStage(stage, true, "dry-run")+ if err := manifest.Save(); err != nil && !errors.Is(err, os.ErrExist) {+ return dryRunError(stage, workdir, taskID, "save_manifest_failed", err)+ }+ return pipeline.Response{+ OK: true,+ Stage: stage,+ Workdir: manifest.Workdir,+ TaskID: manifest.TaskID,+ Outputs: manifest.Outputs,+ }+}++func dryRunError(stage pipeline.Stage, workdir, taskID, code string, err error) pipeline.Response {+ return pipeline.Response{+ OK: false,+ Stage: stage,+ Workdir: workdir,+ TaskID: taskID,+ Error: &pipeline.Error{+ Kind: pipeline.ErrorKindInternal,+ Code: code,+ Message: err.Error(),+ },+ }+}+func newFlagSet(name string) *flag.FlagSet {fs := flag.NewFlagSet(name, flag.ContinueOnError)fs.SetOutput(io.Discard)diff --git a/internal/cli/commands_test.go b/internal/cli/commands_test.goindex 3f01bc6b..717092da 100644--- a/internal/cli/commands_test.go+++ b/internal/cli/commands_test.go@@ -1,6 +1,10 @@package cli-import "testing"+import (+ "context"+ "krillin-ai/internal/pipeline"+ "testing"+)func TestParseSubtitleCommand(t *testing.T) {cmd, err := Parse([]string{@@ -31,3 +35,24 @@ func TestParseTTSCommandRequiresInputSRT(t *testing.T) {t.Fatalf("Parse() error = nil, want error")}}++func TestExecuteDryRunSubtitleReturnsJSONReadyResponse(t *testing.T) {+ cmd, err := Parse([]string{+ "subtitle",+ "local:demo.mp4",+ "--origin-lang", "en",+ "--target-lang", "zh_cn",+ "--workdir", t.TempDir(),+ "--dry-run",+ })+ if err != nil {+ t.Fatalf("Parse() error = %v", err)+ }+ resp := Execute(context.Background(), nil, cmd)+ if !resp.OK {+ t.Fatalf("OK = false, error = %#v", resp.Error)+ }+ if resp.Stage != pipeline.StageSubtitle {+ t.Fatalf("Stage = %s", resp.Stage)+ }+}