-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathextend.go
More file actions
48 lines (40 loc) · 978 Bytes
/
extend.go
File metadata and controls
48 lines (40 loc) · 978 Bytes
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
46
47
48
// Copyright 2025 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package command
import (
"context"
"github.com/spf13/cobra"
)
func Sequence(items ...func(cmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
for i := range items {
if err := items[i](cmd, args); err != nil {
return err
}
}
return nil
}
}
func Visit(cmd *cobra.Command, f func(c *cobra.Command) error) error {
err := f(cmd)
if err != nil {
return err
}
for _, c := range cmd.Commands() {
err := Visit(c, f)
if err != nil {
return err
}
}
return nil
}
type commandKey struct{}
func WithCommand(ctx context.Context, cmd *cobra.Command) context.Context {
return context.WithValue(ctx, commandKey{}, cmd)
}
func CommandFromContext(ctx context.Context) *cobra.Command {
if cmd, ok := ctx.Value(commandKey{}).(*cobra.Command); ok {
return cmd
}
return nil
}