6
6
"flag"
7
7
"fmt"
8
8
"strings"
9
+ "text/tabwriter"
9
10
10
11
"github.com/djdv/go-filesystem-utils/internal/command"
11
12
"github.com/djdv/p9/p9"
@@ -70,7 +71,9 @@ func (so *shutdownOptions) BindFlags(flagSet *flag.FlagSet) {
70
71
const shutdownName = "level"
71
72
shutdownUsage := fmt .Sprintf (
72
73
"sets the `disposition` for shutdown" +
73
- "\n one of {%s}" , shutdownLevelsText (),
74
+ "\n one of:" +
75
+ "\n %s" ,
76
+ shutdownLevelsTable (),
74
77
)
75
78
flagSetFunc (flagSet , shutdownName , shutdownUsage , so ,
76
79
func (value shutdownDisposition , settings * shutdownSettings ) error {
@@ -91,14 +94,51 @@ func (so shutdownOptions) make() (shutdownSettings, error) {
91
94
return settings , settings .clientSettings .fillDefaults ()
92
95
}
93
96
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 ,
99
111
)
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 )
100
140
}
101
- return strings . Join ( levels , ", " )
141
+ return levelsBuffer . String ( )
102
142
}
103
143
104
144
func shutdownExecute (ctx context.Context , options ... shutdownOption ) error {
0 commit comments