• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

matzefriedrich / cobra-extensions / 17961987532

23 Sep 2025 11:48PM UTC coverage: 95.385% (+1.8%) from 93.603%
17961987532

push

github

matzefriedrich
Bumps Go version in GitHub Actions workflow to 1.25

434 of 455 relevant lines covered (95.38%)

10.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/internal/reflection/command_descriptor.go
1
package reflection
2

3
import (
4
        "reflect"
5

6
        "github.com/matzefriedrich/cobra-extensions/pkg/types"
7
        "github.com/spf13/cobra"
8
)
9

10
// CommandDescriptor represents the metadata and configuration for a command, including its use, descriptions, flags, and arguments.
11
type commandDescriptor struct {
12
        use       string
13
        short     string
14
        long      string
15
        flags     []FlagDescriptor
16
        arguments types.ArgumentsDescriptor
17
}
18

19
var _ types.CommandDescriptor = (*commandDescriptor)(nil)
20

21
// NewCommandDescriptor creates a new CommandDescriptor with specified use, short and long descriptions, flags, and arguments.
22
func NewCommandDescriptor(use string, short string, long string, flags []FlagDescriptor, arguments types.ArgumentsDescriptor) types.CommandDescriptor {
12✔
23
        return &commandDescriptor{
12✔
24
                use:       use,
12✔
25
                short:     short,
12✔
26
                long:      long,
12✔
27
                flags:     flags,
12✔
28
                arguments: arguments,
12✔
29
        }
12✔
30
}
12✔
31

32
// BindFlags Binds the reflected flags configuration to the given *cobra.Command object.
33
func (d *commandDescriptor) BindFlags(target *cobra.Command) {
12✔
34
        if target == nil {
12✔
35
                return
×
36
        }
×
37
        for _, f := range d.flags {
26✔
38
                targetFlags := target.Flags()
14✔
39
                name := f.name
14✔
40
                usage := f.usage
14✔
41
                switch f.kind {
14✔
42
                case reflect.String:
8✔
43
                        targetFlags.String(name, f.AsString(), usage)
8✔
44
                case reflect.Int, reflect.Int64:
4✔
45
                        targetFlags.Int64(name, f.AsInt64(), usage)
4✔
46
                case reflect.Bool:
2✔
47
                        targetFlags.Bool(name, f.AsBool(), usage)
2✔
48
                }
49
        }
50
}
51

52
// BindArguments Binds the reflected arguments configuration to the given *cobra.Command object.
53
func (d *commandDescriptor) BindArguments(target *cobra.Command) {
12✔
54
        if target == nil {
12✔
55
                return
×
56
        }
×
57

58
        target.Use = d.use
12✔
59
        target.Short = d.short
12✔
60
        target.Long = d.long
12✔
61

12✔
62
        d.arguments.BindArguments(target)
12✔
63
}
64

65
// UnmarshalArgumentValues deserializes the command argument values from a list of strings and binds them to the corresponding fields.
66
func (d *commandDescriptor) UnmarshalArgumentValues(args ...string) {
7✔
67
        d.arguments.BindArgumentValues(args...)
7✔
68
}
7✔
69

70
// UnmarshalFlagValues populates the CommandDescriptor's flags from the provided *cobra.Command object.
71
func (d *commandDescriptor) UnmarshalFlagValues(target *cobra.Command) {
7✔
72
        flags := target.Flags()
7✔
73
        for _, f := range d.flags {
18✔
74
                flagName := f.name
11✔
75
                switch f.kind {
11✔
76
                case reflect.String:
5✔
77
                        value, _ := flags.GetString(flagName)
5✔
78
                        _ = f.SetValue(value)
5✔
79
                case reflect.Int, reflect.Int64:
4✔
80
                        value, _ := flags.GetInt64(flagName)
4✔
81
                        _ = f.SetValue(value)
4✔
82
                case reflect.Bool:
2✔
83
                        value, _ := flags.GetBool(flagName)
2✔
84
                        _ = f.SetValue(value)
2✔
85
                }
86
        }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc