|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + "github.com/mattn/go-isatty" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + |
| 16 | + "github.com/arrase/code-reducer/internal/config" |
| 17 | + "github.com/arrase/code-reducer/internal/engine" |
| 18 | + "github.com/arrase/code-reducer/internal/security" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + modelIdFlag string |
| 23 | + numCtxFlag string |
| 24 | + printFlag bool |
| 25 | + initFlag bool |
| 26 | + updateFlag bool |
| 27 | + dryRunFlag bool |
| 28 | +) |
| 29 | + |
| 30 | +var RootCmd = &cobra.Command{ |
| 31 | + Use: "code-reducer [message]", |
| 32 | + Short: "Code-Reducer is a documentation agent that writes and maintains a project wiki.", |
| 33 | + Long: `A pure Go port of Code-Reducer CLI, optimized for performance and local LLM execution.`, |
| 34 | + Args: cobra.MaximumNArgs(1), |
| 35 | + Run: func(cmd *cobra.Command, args []string) { |
| 36 | + // Load existing environment variables |
| 37 | + _, _ = config.LoadEnv() |
| 38 | + |
| 39 | + userMessage := "" |
| 40 | + if len(args) > 0 { |
| 41 | + userMessage = args[0] |
| 42 | + } |
| 43 | + |
| 44 | + // Determine command mode |
| 45 | + commandMode := "update" |
| 46 | + if initFlag { |
| 47 | + commandMode = "init" |
| 48 | + } |
| 49 | + |
| 50 | + executeCommand(commandMode, userMessage) |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +func init() { |
| 55 | + RootCmd.PersistentFlags().StringVar(&modelIdFlag, "model-id", "", "Specify LLM model ID") |
| 56 | + RootCmd.PersistentFlags().StringVar(&modelIdFlag, "modelId", "", "Specify LLM model ID (alias)") |
| 57 | + RootCmd.PersistentFlags().StringVar(&numCtxFlag, "num-ctx", "", "Specify Ollama context window size") |
| 58 | + RootCmd.PersistentFlags().BoolVarP(&printFlag, "print", "p", false, "Run once and print the final assistant output (non-interactive)") |
| 59 | + RootCmd.PersistentFlags().BoolVar(&initFlag, "init", false, "Initialize project documentation") |
| 60 | + RootCmd.PersistentFlags().BoolVar(&updateFlag, "update", false, "Update existing project documentation") |
| 61 | + RootCmd.PersistentFlags().BoolVar(&dryRunFlag, "dry-run", false, "Show plan without invoking agent") |
| 62 | +} |
| 63 | + |
| 64 | +func executeCommand(command string, userMessage string) { |
| 65 | + repoRoot, err := os.Getwd() |
| 66 | + if err != nil { |
| 67 | + fmt.Printf("Error getting current working directory: %v\n", err) |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + |
| 71 | + // Apply flag overrides to environment variables |
| 72 | + if modelIdFlag != "" { |
| 73 | + _ = os.Setenv(config.CodeReducerModelIdEnvKey, modelIdFlag) |
| 74 | + } |
| 75 | + if numCtxFlag != "" { |
| 76 | + _ = os.Setenv(config.OllamaNumCtxEnvKey, numCtxFlag) |
| 77 | + } |
| 78 | + |
| 79 | + needsSetup := NeedsCredentialSetup() |
| 80 | + isTTY := isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) |
| 81 | + |
| 82 | + if needsSetup { |
| 83 | + if !isTTY { |
| 84 | + fmt.Println("Error: Credentials/Model ID not configured. Please set the model ID using --model-id or the CODE_REDUCER_MODEL_ID environment variable.") |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + |
| 88 | + // Interactive CLI onboarding |
| 89 | + reader := bufio.NewReader(os.Stdin) |
| 90 | + fmt.Println("Welcome to Code-Reducer CLI Setup") |
| 91 | + fmt.Println("---------------------------------") |
| 92 | + |
| 93 | + fmt.Print("Enter LLM Model ID (e.g. gemma4:26b-a4b-it-qat): ") |
| 94 | + modelInput, _ := reader.ReadString('\n') |
| 95 | + modelInput = strings.TrimSpace(modelInput) |
| 96 | + if modelInput == "" { |
| 97 | + modelInput = "gemma4:26b-a4b-it-qat" |
| 98 | + } |
| 99 | + |
| 100 | + fmt.Printf("Enter Ollama Base URL [%s]: ", config.OllamaDefaultBaseURL) |
| 101 | + urlInput, _ := reader.ReadString('\n') |
| 102 | + urlInput = strings.TrimSpace(urlInput) |
| 103 | + if urlInput == "" { |
| 104 | + urlInput = config.OllamaDefaultBaseURL |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Printf("Enter Ollama Context Size [%d]: ", config.OllamaDefaultNumCtx) |
| 108 | + ctxInput, _ := reader.ReadString('\n') |
| 109 | + ctxInput = strings.TrimSpace(ctxInput) |
| 110 | + if ctxInput == "" { |
| 111 | + ctxInput = strconv.Itoa(config.OllamaDefaultNumCtx) |
| 112 | + } |
| 113 | + |
| 114 | + updates := map[string]string{ |
| 115 | + config.CodeReducerModelIdEnvKey: modelInput, |
| 116 | + config.OllamaBaseUrlEnvKey: urlInput, |
| 117 | + config.OllamaNumCtxEnvKey: ctxInput, |
| 118 | + } |
| 119 | + |
| 120 | + err = config.SaveEnv(updates) |
| 121 | + if err != nil { |
| 122 | + fmt.Printf("Error saving configuration: %v\n", err) |
| 123 | + os.Exit(1) |
| 124 | + } |
| 125 | + fmt.Println("Configuration successfully saved to local .env file.") |
| 126 | + fmt.Println() |
| 127 | + } |
| 128 | + |
| 129 | + if dryRunFlag { |
| 130 | + fmt.Println("Dry-run mode: configuration is verified, but agent was not executed.") |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + // Acquire exclusive repository lock |
| 135 | + lock, err := security.AcquireLock(repoRoot, true) |
| 136 | + if err != nil { |
| 137 | + fmt.Printf("Error acquiring repository lock: %v\n", err) |
| 138 | + os.Exit(1) |
| 139 | + } |
| 140 | + defer lock.Unlock() |
| 141 | + |
| 142 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) |
| 143 | + defer stop() |
| 144 | + |
| 145 | + modelId := os.Getenv(config.CodeReducerModelIdEnvKey) |
| 146 | + baseURL := os.Getenv(config.OllamaBaseUrlEnvKey) |
| 147 | + numCtx := config.GetOllamaContextSize(numCtxFlag) |
| 148 | + |
| 149 | + client := engine.NewLLMClient(modelId, baseURL, numCtx) |
| 150 | + |
| 151 | + err = client.RunInitOrUpdate(ctx, command, repoRoot, userMessage, func(ev engine.Event) { |
| 152 | + if ev.Type == "status" { |
| 153 | + fmt.Println(ev.Message) |
| 154 | + } else if ev.Type == "error" { |
| 155 | + fmt.Fprintf(os.Stderr, "Error: %s\n", ev.Message) |
| 156 | + } else { |
| 157 | + fmt.Println(ev.Message) |
| 158 | + } |
| 159 | + }) |
| 160 | + |
| 161 | + if err != nil { |
| 162 | + fmt.Printf("Documentation Run Failed: %v\n", err) |
| 163 | + os.Exit(1) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// NeedsCredentialSetup checks if critical configuration is missing |
| 168 | +func NeedsCredentialSetup() bool { |
| 169 | + return os.Getenv(config.CodeReducerModelIdEnvKey) == "" |
| 170 | +} |
| 171 | + |
0 commit comments