Skip to content

Commit 1ecbe5a

Browse files
committed
commands: shutdown - change helptext format
Formerly a grouping, now a table. Unfortunately not a list.
1 parent 689a7a3 commit 1ecbe5a

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

internal/commands/shutdown.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"flag"
77
"fmt"
88
"strings"
9+
"text/tabwriter"
910

1011
"github.com/djdv/go-filesystem-utils/internal/command"
1112
"github.com/djdv/p9/p9"
@@ -70,7 +71,9 @@ func (so *shutdownOptions) BindFlags(flagSet *flag.FlagSet) {
7071
const shutdownName = "level"
7172
shutdownUsage := fmt.Sprintf(
7273
"sets the `disposition` for shutdown"+
73-
"\none of {%s}", shutdownLevelsText(),
74+
"\none of:"+
75+
"\n%s",
76+
shutdownLevelsTable(),
7477
)
7578
flagSetFunc(flagSet, shutdownName, shutdownUsage, so,
7679
func(value shutdownDisposition, settings *shutdownSettings) error {
@@ -91,14 +94,51 @@ func (so shutdownOptions) make() (shutdownSettings, error) {
9194
return settings, settings.clientSettings.fillDefaults()
9295
}
9396

94-
func shutdownLevelsText() string {
95-
levels := make([]string, maximumShutdown)
96-
for i, sl := 0, minimumShutdown; sl <= maximumShutdown; i, sl = i+1, sl+1 {
97-
levels[i] = fmt.Sprintf(
98-
`"%s"`, strings.ToLower(sl.String()),
97+
func shutdownLevelsTable() string {
98+
// [upstream] glamour prepends a newline to lists
99+
// which can not be disabled. So we don't use them here. :^/
100+
const (
101+
minWidth = 0
102+
tabWidth = 0
103+
padding = 0
104+
padChar = ' '
105+
flags = 0
106+
)
107+
var (
108+
levelsBuffer strings.Builder
109+
tabWriter = tabwriter.NewWriter(
110+
&levelsBuffer, minWidth, tabWidth, padding, padChar, flags,
99111
)
112+
)
113+
for _, pair := range []struct {
114+
description string
115+
level shutdownDisposition
116+
}{
117+
{
118+
level: patientShutdown,
119+
description: "waits for connections to become idle before closing",
120+
},
121+
{
122+
level: shortShutdown,
123+
description: "forcibly closes connections after a short delay",
124+
},
125+
{
126+
level: immediateShutdown,
127+
description: "forcibly closes connections immediately",
128+
},
129+
} {
130+
if _, err := fmt.Fprintf(
131+
tabWriter,
132+
"`%s`\t - %s\n",
133+
pair.level, pair.description,
134+
); err != nil {
135+
panic(err)
136+
}
137+
}
138+
if err := tabWriter.Flush(); err != nil {
139+
panic(err)
100140
}
101-
return strings.Join(levels, ", ")
141+
return levelsBuffer.String()
102142
}
103143

104144
func shutdownExecute(ctx context.Context, options ...shutdownOption) error {

0 commit comments

Comments
 (0)