Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# codacy-cli-v2

The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing code using tools like ESLint and uploading the results in SARIF format to Codacy. It provides two main commands: `analyze` and `upload`.
The Codacy CLI (version 2) is a command-line tool that supports analyzing code using tools like ESLint and uploading the results in SARIF format to Codacy.
The tool is invoked using the `codacy-cli` command, and provides two main commands: analyze and upload.

### Commands

Expand Down Expand Up @@ -32,13 +33,13 @@ The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing co
tools:
- [email protected]

- **`codacy-cli-v2 install`**: Command to install the specified node and eslint versions before running analysis.
- **`codacy-cli install`**: Command to install the specified node and eslint versions before running analysis.

## Download

### MacOS (brew)

To install `codacy-cli-v2` using Homebrew:
To install `codacy-cli` using Homebrew:

```bash
brew install codacy/codacy-cli-v2/codacy-cli-v2
Expand Down
65 changes: 62 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "codacy-cli",
Short: "Codacy CLI - A command line interface for Codacy",
Long: ``,
Use: "codacy-cli",
Short: "Codacy CLI - A command line interface for Codacy",
Long: "",
Example: getExampleText(),
Run: func(cmd *cobra.Command, args []string) {
// Check if .codacy directory exists
if _, err := os.Stat(".codacy"); os.IsNotExist(err) {
Expand Down Expand Up @@ -52,4 +54,61 @@ func showWelcomeMessage() {
fmt.Println()
fmt.Println("Or run without a token to use local configuration:")
fmt.Println(" codacy-cli init")
fmt.Println()
fmt.Println("For more information about available commands, run:")
fmt.Println(" codacy-cli --help")
}

func getExampleText() string {
return color.New(color.FgCyan).Sprint("Initialize a project:") + "\n" +
color.New(color.FgGreen).Sprint(" codacy-cli init") + "\n\n" +
color.New(color.FgCyan).Sprint("Install required tools:") + "\n" +
color.New(color.FgGreen).Sprint(" codacy-cli install") + "\n\n" +
color.New(color.FgCyan).Sprint("Run analysis with ESLint:") + "\n" +
color.New(color.FgGreen).Sprint(" codacy-cli analyze --tool eslint") + "\n\n" +
color.New(color.FgCyan).Sprint("Run analysis and output in SARIF format:") + "\n" +
color.New(color.FgGreen).Sprint(" codacy-cli analyze --tool eslint --format sarif") + "\n\n" +
color.New(color.FgCyan).Sprint("Upload results to Codacy:") + "\n" +
color.New(color.FgGreen).Sprint(" codacy-cli upload -s results.sarif -c <commit-uuid> -t <project-token>")
}

func init() {
// Add global flags here
rootCmd.PersistentFlags().StringP("config", "c", filepath.Join(".codacy", "codacy.yaml"), "config file")

// Customize help template
rootCmd.SetUsageTemplate(`
` + color.New(color.FgCyan).Sprint("Usage:") + `
{{.UseLine}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

` + color.New(color.FgCyan).Sprint("Aliases:") + `
{{.NameAndAliases}}{{end}}{{if .HasExample}}

` + color.New(color.FgCyan).Sprint("Examples:") + `
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}

` + color.New(color.FgCyan).Sprint("Available Commands:") + `{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
` + "{{$cmd := .Name}}" + color.New(color.FgGreen).Sprintf("{{rpad .Name .NamePadding}}") + ` {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}

` + color.New(color.FgCyan).Sprint("Flags:") + `
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}

` + color.New(color.FgCyan).Sprint("Global Flags:") + `
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}

` + color.New(color.FgCyan).Sprint("Additional help topics:") + `{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}

Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}

` + color.New(color.FgCyan).Sprint("Configuration Example") + ` (.codacy/codacy.yaml):
runtimes:
- [email protected]
tools:
- [email protected]

` + color.New(color.FgCyan).Sprint("For more information and examples, visit:") + `
https://github.com/codacy/codacy-cli-v2
`)
}