-
Notifications
You must be signed in to change notification settings - Fork 101
/
examples.go
45 lines (36 loc) · 893 Bytes
/
examples.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package core
import (
"fmt"
"strings"
)
type PackageCommandTreeOpts struct {
BinaryName string
PositionalArgs bool
Color bool
JSON bool
AlwaysAllowTogglingWait bool
}
type Example struct {
Description string
Args []string
}
func (e Example) asString(nameFlag string, opts PackageCommandTreeOpts) string {
command := opts.BinaryName
for _, arg := range e.Args {
if opts.PositionalArgs && arg == nameFlag {
continue
}
command += " " + arg
}
return fmt.Sprintf("# %s \n%s", e.Description, command)
}
type Examples []Example
func (es Examples) Description(nameFlag string, opts PackageCommandTreeOpts) string {
var description string
for _, example := range es {
description += example.asString(nameFlag, opts) + "\n\n"
}
return strings.TrimSuffix(description, "\n\n")
}