Skip to content

Commit

Permalink
cmds: usage string based on binary name (#3937)
Browse files Browse the repository at this point in the history
Renaming binaries is sometimes preferrable or even necessary when
creating distribution packages. In order to reduce confusion for the
users, it's nice when the built in help text of the applications refers
to the actually used name of the binary.

By adapting the `Use` string, cobra also generates correct completion
scripts when using a changed name for the binary.
  • Loading branch information
matzf authored Nov 26, 2020
1 parent ae63a60 commit 6abe062
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 71 deletions.
6 changes: 4 additions & 2 deletions go/cs/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -74,10 +75,11 @@ func main() {
var flags struct {
config string
}
executable := filepath.Base(os.Args[0])
cmd := &cobra.Command{
Use: "cs",
Use: executable,
Short: "SCION Control Service instance",
Example: " cs --config cs.toml",
Example: " " + executable + " --config cs.toml",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.NoArgs,
Expand Down
6 changes: 4 additions & 2 deletions go/posix-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/syndtr/gocapability/capability"
Expand All @@ -46,10 +47,11 @@ func main() {
var flags struct {
config string
}
executable := filepath.Base(os.Args[0])
cmd := &cobra.Command{
Use: "posix-gateway",
Use: executable,
Short: "SCION IP gateway",
Example: " posix-gateway --config gateway.toml",
Example: " " + executable + " --config gateway.toml",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.NoArgs,
Expand Down
5 changes: 4 additions & 1 deletion go/posix-router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"sync"

"github.com/spf13/cobra"
Expand All @@ -40,9 +41,11 @@ func main() {
config string
}
metrics := router.NewMetrics()
executable := filepath.Base(os.Args[0])
cmd := &cobra.Command{
Use: "posix-router",
Use: executable,
Short: "SCION router",
Example: " " + executable + " --config br.toml",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.NoArgs,
Expand Down
2 changes: 0 additions & 2 deletions go/scion-pki/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ scion_go_binary(
go_library(
name = "go_default_library",
srcs = [
"completion.go",
"main.go",
"version.go",
],
importpath = "github.com/scionproto/scion/go/scion-pki",
visibility = ["//visibility:private"],
deps = [
"//go/lib/env:go_default_library",
"//go/lib/serrors:go_default_library",
"//go/pkg/command:go_default_library",
"//go/scion-pki/certs:go_default_library",
"//go/scion-pki/testcrypto:go_default_library",
Expand Down
60 changes: 0 additions & 60 deletions go/scion-pki/completion.go

This file was deleted.

4 changes: 3 additions & 1 deletion go/scion-pki/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

Expand All @@ -27,8 +28,9 @@ import (
)

func main() {
executable := filepath.Base(os.Args[0])
cmd := &cobra.Command{
Use: "scion-pki",
Use: executable,
Short: "SCION Control Plane PKI Management Tool",
Args: cobra.NoArgs,
// Silence the errors, since we print them in main. Otherwise, cobra
Expand Down
4 changes: 3 additions & 1 deletion go/scion/scion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -70,8 +71,9 @@ type CommandPather interface {
}

func main() {
executable := filepath.Base(os.Args[0])
cmd := &cobra.Command{
Use: "scion",
Use: executable,
Short: "A clean-slate Internet architecture",
Args: cobra.NoArgs,
// Silence the errors, since we print them in main. Otherwise, cobra
Expand Down
6 changes: 4 additions & 2 deletions go/sciond/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"time"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -57,10 +58,11 @@ func main() {
var flags struct {
config string
}
executable := filepath.Base(os.Args[0])
cmd := &cobra.Command{
Use: "sciond",
Use: executable,
Short: "SCION Daemon",
Example: " sciond --config sd.toml",
Example: " " + executable + " --config sd.toml",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.NoArgs,
Expand Down

0 comments on commit 6abe062

Please sign in to comment.