- комментарии, тексты ошибок, вывод CLI и сообщения тестов теперь на английском - по-русски остались только литералы словаря ru и содержимое фикстур: это данные под проверкой, а не текст инструмента - согласование числительных в итоге упростилось до английского plural
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
// Package cli lays out the commands of the tool.
|
|
//
|
|
// The depth of a command reflects how often it runs and whom it addresses:
|
|
// project commands run in every repository and often, tending a suite runs in
|
|
// one repository and rarely. That is why `check` stays at the top level and
|
|
// whatever makes no sense in a project goes under `suite`. There are no
|
|
// synonyms: `convy suite pull` is not introduced next to `convy pull`.
|
|
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// ExitCode is the exit status of the process.
|
|
type ExitCode int
|
|
|
|
const (
|
|
// OK means the check passed and the work is done.
|
|
OK ExitCode = 0
|
|
// Failed means the check found errors.
|
|
Failed ExitCode = 1
|
|
// Usage means the command was typed wrong or run in the wrong context.
|
|
Usage ExitCode = 2
|
|
)
|
|
|
|
// Env is the environment of a run. It is pulled out so that commands can be
|
|
// tested without a process.
|
|
type Env struct {
|
|
Dir string
|
|
Out io.Writer
|
|
Err io.Writer
|
|
NoTTY bool
|
|
Colors bool
|
|
}
|
|
|
|
// Run parses the arguments and executes the command.
|
|
func Run(env Env, args []string) ExitCode {
|
|
if len(args) == 0 {
|
|
usage(env.Out)
|
|
return Usage
|
|
}
|
|
|
|
switch args[0] {
|
|
case "suite":
|
|
return runSuite(env, args[1:])
|
|
case "add", "pull", "list", "check":
|
|
fmt.Fprintf(env.Err, "the %q command is not implemented yet\n", args[0])
|
|
return Usage
|
|
case "help", "-h", "--help":
|
|
usage(env.Out)
|
|
return OK
|
|
default:
|
|
fmt.Fprintf(env.Err, "unknown command %q\n\n", args[0])
|
|
usage(env.Err)
|
|
return Usage
|
|
}
|
|
}
|
|
|
|
func runSuite(env Env, args []string) ExitCode {
|
|
if len(args) == 0 {
|
|
fmt.Fprintln(env.Err, "convy suite: a subcommand is required — check")
|
|
return Usage
|
|
}
|
|
switch args[0] {
|
|
case "check":
|
|
return runSuiteCheck(env, args[1:])
|
|
case "new":
|
|
fmt.Fprintln(env.Err, "the \"suite new\" command is not implemented yet")
|
|
return Usage
|
|
default:
|
|
fmt.Fprintf(env.Err, "unknown subcommand %q for convy suite\n", args[0])
|
|
return Usage
|
|
}
|
|
}
|
|
|
|
// usage prints the help grouped under headings: a flat list hides the levels.
|
|
func usage(w io.Writer) {
|
|
fmt.Fprint(w, `convy — tending development conventions.
|
|
|
|
In a project:
|
|
convy add <topic> subscribe and assemble (not implemented)
|
|
convy pull reassemble what is subscribed (not implemented)
|
|
convy list what is wired up and available (not implemented)
|
|
convy check check the form of what is here (not implemented)
|
|
|
|
In a suite:
|
|
convy suite check suite integrity: prefixes, topics, axes, links, form
|
|
convy suite new a new topic (not implemented)
|
|
|
|
`)
|
|
}
|
|
|
|
// Main is the entry point of the process.
|
|
func Main() int {
|
|
dir, err := os.Getwd()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "cannot determine the current directory:", err)
|
|
return int(Usage)
|
|
}
|
|
env := Env{Dir: dir, Out: os.Stdout, Err: os.Stderr}
|
|
return int(Run(env, os.Args[1:]))
|
|
}
|