open-code-review/cmd/opencodereview/root.go
超級の新人 756203c31a
Some checks are pending
CI / test (push) Waiting to run
CI / windows (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
fix(cmd): suppress ANSI color when stdout is not a TTY (#927)
* fix(cmd): suppress ANSI color when stdout is not a TTY

Text output hardcoded ANSI escape sequences with no terminal detection, so
piping or redirecting a review leaked raw escapes into the consumer. Piping
into `gh issue comment` produced a comment full of literal `^[[2m` sequences.

Color is now resolved once per run, in this order: `--no-color`/`--color=never`
turn it off, `--color=always` forces it on even through a pipe, `NO_COLOR`
(any non-empty value, per no-color.org) and `TERM=dumb` turn it off, and
otherwise it follows whether stdout is a terminal. Explicit flags outrank the
environment because a flag is a per-invocation decision while the variable is a
standing preference.

Both text paths — review findings and `--preview` — route every escape through
colorize(), so plain mode keeps all the information the color carried: the
diff gutter still shows +/-/space and the status badges still read [A]/[M]/...
Preview counts are padded before colorizing so the columns align in either
mode.

The flags are persistent on the root command, so `ocr --no-color review` and
`ocr review --no-color` are equivalent, and an invalid `--color` value is
rejected rather than silently treated as auto.

Fixes #682

* refactor(cmd): drop --no-color flag and NO_COLOR env var support

--no-color was just an alias for --color=never with no added
capability, and NO_COLOR isn't a universal enough convention to
bake in speculatively. --color <auto|always|never> alone already
covers every case. TERM=dumb stays, since that closes a real gap
in TTY detection rather than adding another way to configure the
same toggle.

---------

Co-authored-by: kite <lizhengfeng.lzf@alibaba-inc.com>
2026-08-19 21:51:38 +08:00

69 lines
1.8 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package main
import (
"fmt"
"runtime"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "ocr",
Short: "OpenCodeReview - AI-Powered Code Review CLI",
Long: `OpenCodeReview - AI-Powered Code Review CLI
An AI-powered code review tool that reads git diffs, sends them to a
configurable LLM service, and generates review comments.`,
SilenceUsage: true,
SilenceErrors: true,
// Runs for every subcommand, always before any RunE: validate --color once
// flags are parsed, then resolve the color decision from them.
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := validateColorMode(colorMode); err != nil {
return err
}
colorEnabled = resolveColor()
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
v, _ := cmd.Flags().GetBool("version")
if v {
printVersion()
return nil
}
return cmd.Help()
},
}
func init() {
rootCmd.SetFlagErrorFunc(flagErrorWithSuggestion)
rootCmd.Flags().BoolP("version", "V", false, "version for ocr")
addColorFlags(rootCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(reviewCmd)
rootCmd.AddCommand(scanCmd)
rootCmd.AddCommand(delegateCmd)
rootCmd.AddCommand(sessionCmd)
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(llmCmd)
rootCmd.AddCommand(rulesCmd)
rootCmd.AddCommand(viewerCmd)
rootCmd.AddCommand(completionCmd)
}
func versionString() string {
s := fmt.Sprintf("open-code-review %s", Version)
if GitCommit != "" {
s += fmt.Sprintf(" (%s)", GitCommit)
}
s += fmt.Sprintf(" %s/%s\n", runtime.GOOS, runtime.GOARCH)
if BuildDate != "" {
s += fmt.Sprintf("built at: %s\n", BuildDate)
}
s += "https://github.com/alibaba/open-code-review\n"
return s
}