Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): implement personality for application name-agnostic help manuals #238

Merged
merged 16 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func MockVersion(version string) (restore func()) {
return func() { Version = old }
}

// ProgramName represents the name of the application binary (i.e. pebble)
// ProgramName represents the name of the application binary.
var ProgramName string = "pebble"

// DisplayName represents the user-facing name of the application (i.e. Pebble)
// DisplayName represents the user-facing name of the application.
var DisplayName string = "Pebble"
2 changes: 1 addition & 1 deletion internals/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func errorToMessage(e error) (normalMessage string, err error) {
}
case client.ErrorKindSystemRestart:
isError = false
msg = fmt.Sprintf("%s is about to reboot the system", cmd.ProgramName)
msg = fmt.Sprintf("%s is about to reboot the system", cmd.DisplayName)
case client.ErrorKindNoDefaultServices:
msg = "no default services"
default:
Expand Down
3 changes: 1 addition & 2 deletions internals/cli/cmd_enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/canonical/go-flags"

"github.com/canonical/pebble/client"
cmdpkg "github.com/canonical/pebble/cmd"
"github.com/canonical/pebble/internals/logger"
)

Expand Down Expand Up @@ -179,7 +178,7 @@ func (cmd *cmdEnter) Execute(args []string) error {
case runStop = <-runReadyCh:
case runPanic := <-runResultCh:
if runPanic == nil {
panic(fmt.Sprintf("internal error: %s daemon stopped early", cmdpkg.ProgramName))
panic("internal error: daemon stopped early")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of multiple services wouldn't the program name be useful in a panic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine -- I'd be happy to reinstate it as {{.DisplayName}}.

}
panic(runPanic)
}
Expand Down
2 changes: 1 addition & 1 deletion internals/cli/cmd_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type HelpCategory struct {
// HelpCategories helps us by grouping commands
var HelpCategories = []HelpCategory{{
Label: "Run",
Description: "run {{.DisplayName}}",
Description: "run the service manager",
Commands: []string{"run", "help", "version"},
}, {
Label: "Plan",
Expand Down
4 changes: 2 additions & 2 deletions internals/cli/cmd_okay.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
const cmdOkaySummary = "Acknowledge notices and warnings"
const cmdOkayDescription = `
The okay command acknowledges warnings and notices that have been previously
listed using 'pebble warnings' or 'pebble notices', so that they are omitted
listed using '{{.ProgramName}} warnings' or '{{.ProgramName}} notices', so that they are omitted
from future runs of either command. When a notice or warning is repeated, it
will again show up until the next 'pebble okay'.
will again show up until the next '{{.ProgramName}} okay'.
`

type cmdOkay struct {
Expand Down
4 changes: 2 additions & 2 deletions internals/cli/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/canonical/pebble/internals/systemd"
)

const cmdRunSummary = "Run the {{.DisplayName}} environment"
const cmdRunSummary = "Run the service manager environment"
const cmdRunDescription = `
The run command starts pebble and runs the configured environment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to override this instance too? Probably with {{.DisplayName}}.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this comment was resolved but not done? The cmdRunDescription still says "The run command starts pebble..."


Expand Down Expand Up @@ -98,7 +98,7 @@ func (rcmd *cmdRun) run(ready chan<- func()) {
// This exit code must be in system'd SuccessExitStatus.
panic(&exitStatus{42})
}
fmt.Fprintf(os.Stderr, "cannot run %s: %v\n", cmd.ProgramName, err)
fmt.Fprintf(os.Stderr, "cannot run daemon: %v\n", err)
panic(&exitStatus{1})
}
}
Expand Down
14 changes: 1 addition & 13 deletions internals/cli/cmd_warnings.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ type cmdWarnings struct {
Verbose bool `long:"verbose"`
}

const cmdOkaySummary = "Acknowledge warnings"
const cmdOkayDescription = `
The okay command acknowledges the warnings listed with '{{.ProgramName}} warnings'.

Once acknowledged, a warning won't appear again unless it reoccurs and
sufficient time has passed.
`

type cmdOkay struct {
client *client.Client
}

func init() {
AddCommand(&CmdInfo{
Name: "warnings",
Expand Down Expand Up @@ -209,7 +197,7 @@ func lastWarningTimestamp() (time.Time, error) {
f, err := os.Open(warnFilename(user.HomeDir))
if err != nil {
if os.IsNotExist(err) {
return time.Time{}, fmt.Errorf("you must have looked at the warnings before acknowledging them. Try '%s warnings'.", cmd.ProgramName)
return time.Time{}, fmt.Errorf("you must have looked at the warnings before acknowledging them. Try 'pebble warnings'.")
anpep marked this conversation as resolved.
Show resolved Hide resolved
}
return time.Time{}, fmt.Errorf("cannot open timestamp file: %v", err)
}
Expand Down
183 changes: 183 additions & 0 deletions run-checks
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#!/bin/bash
anpep marked this conversation as resolved.
Show resolved Hide resolved

#----------------------------------------------------#
# PRINTING ROUTINES #
#----------------------------------------------------#

P_RESET="\e[0m"
P_RED="\e[31m"
P_GREEN="\e[32m"

print_newline() {
echo -e "$P_RESET"
}

print_normal() {
echo -e "$P_RESET$*"
}

print_red() {
echo -e "$P_RED$*$P_RESET"
}

print_green() {
echo -e "$P_GREEN$*$P_RESET"
}

print_special() {
echo -ne "$P_RESET$*"
}

#----------------------------------------------------#
# SETUP / TEST VERBOSITY CONTROL #
#----------------------------------------------------#

# If started with:
# VERBOSE=1 ./run-checks
# Verbose print everything, not only failures
VERBOSE=${VERBOSE:-0}

# Number of tests that failed
FAILED=0

# Print the final test results and exit wit non-zero
# return code if anything failed.
#
print_results() {
if [ $FAILED -eq 0 ]; then
print_newline
print_green "Good news everyone ... all checks passed!"
print_newline
exit 0
else
print_newline
print_red "Bad news everyone ... $FAILED checks failed!"
print_newline
exit 1
fi
}

# Print the pass message, and if VERBOSE=1 then
# add the command output.
#
print_test_pass() {
print_green "Pass"
if [ "$VERBOSE" = "1" ]; then
print_newline
print_normal "Command: ${CMD#*--}"
print_newline
cat "$OUTPUT"
print_newline
fi
}

# Print the fail message which includes the command
# output.
#
print_test_fail() {
print_red "Fail"
print_newline
print_red "Command: ${CMD#*--}"
print_newline
cat "$OUTPUT"
print_newline
}

# Run a test and fail based on the check type
#
# Verbose print the failure case unless VERBOSE=1, in
# which case verbose print everything (useful for CI
# logs).
#
# Argument $1: Check Type ("zero-exit", "zero-output")
# Argument $2: Test string description
# Argument $3..$n: Test command line
#
test_check() {
print_special "=> $2"
OUTPUT=$(mktemp)
CMD=$(exec 2>&1 && set -x && set -- "${@:3}")

set +e
"${@:3}" > "$OUTPUT" 2>&1
RET=$?
set -e

# Check type
if [ "zero-output" = "$1" ]; then

CHECK="$(wc -l < "$OUTPUT")"
else
CHECK="$RET"
fi

# Print result
if [ "$CHECK" -ne 0 ]; then
print_test_fail
FAILED=$((FAILED + 1))
else
print_test_pass
fi

rm -f "$OUTPUT"
}

#----------------------------------------------------#
# TEST DEPENDENCIES #
#----------------------------------------------------#

# Allow commands to be normally quiet while output
# will be enabled if VERBOSE=1 is set.
#
run() {
if [ "$VERBOSE" = "1" ]; then
CMD=$(exec 2>&1 && set -x && set -- "$@")
print_newline
print_normal "Command: ${CMD#*--}"
print_newline
"$@"
print_newline
else
"$@" >/dev/null 2>&1
fi
}

# Install staticcheck from the correct source so that it
# will compile and install for the Go version on the host.
#
# shellcheck disable=SC2317
# Disable "command appears to be unreachable" warnings. This function
# is indirectly called through run(), which confuses shellcheck.
#
install_staticcheck() {
# We build from source to make this architecture independent.
# However, staticcheck releases are dependent on specific Go
# versions, so we cannot simply build the latest.
PKG="honnef.co/go/tools/cmd/staticcheck"
GO_VERSION="$(go version | cut -d' ' -f3 | cut -d'.' -f1-2 | sed 's/go//' | sed 's/\.//')"

# This list will be updated as x-go moves to later Go versions.
if [ "$GO_VERSION" -ge "120" ]; then
# Go v1.20
go install "${PKG}@2023.1.3"
else
# Not supported
print_red "Staticcheck v2023.1.3 requires Go version v1.20 or later."
fi
}

GOBIN=$(go env GOPATH)/bin
export PATH=$PATH:$GOBIN
#run install_staticcheck

#----------------------------------------------------#
# TESTS #
#----------------------------------------------------#

test_check "zero-exit" "Build ... " go build ./...
test_check "zero-exit" "Vet ... " go vet ./...
test_check "zero-exit" "Unit Tests ... " go test ./...
#test_check "zero-exit" "Static-check ... " staticcheck ./...
test_check "zero-output" "Formatting ... " gofmt -d .

print_results
Loading