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

NVIDIA / skyhook / 20072025419

09 Dec 2025 05:02PM UTC coverage: 77.041% (+2.1%) from 74.963%
20072025419

Pull #124

github

t0mmylam
feat(cli): Add node management commands and ignore label support
Pull Request #124: feat(cli): Add node management commands and ignore label support

1059 of 1277 new or added lines in 13 files covered. (82.93%)

56 existing lines in 3 files now uncovered.

5701 of 7400 relevant lines covered (77.04%)

0.88 hits per line

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

97.18
/operator/internal/cli/context/context.go
1
/*
2
 * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
 * SPDX-License-Identifier: Apache-2.0
4
 *
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18

19
package context
20

21
import (
22
        "fmt"
23
        "io"
24
        "os"
25
        "strings"
26

27
        "github.com/spf13/pflag"
28
        "k8s.io/cli-runtime/pkg/genericclioptions"
29
)
30

31
const defaultNamespace = "skyhook"
32

33
// GlobalFlags holds persistent CLI flags that every command uses (kubeconfig, namespace, output, etc.).
34
type GlobalFlags struct {
35
        ConfigFlags  *genericclioptions.ConfigFlags
36
        OutputFormat string
37
        Verbose      bool
38
        DryRun       bool
39
}
40

41
// NewGlobalFlags creates a new GlobalFlags with default values.
42
func NewGlobalFlags() *GlobalFlags {
1✔
43
        flags := genericclioptions.NewConfigFlags(true)
1✔
44
        if flags.Namespace == nil {
1✔
45
                flags.Namespace = new(string)
×
UNCOV
46
        }
×
47
        if *flags.Namespace == "" {
2✔
48
                *flags.Namespace = defaultNamespace
1✔
49
        }
1✔
50

51
        return &GlobalFlags{
1✔
52
                ConfigFlags:  flags,
1✔
53
                OutputFormat: "table",
1✔
54
        }
1✔
55
}
56

57
// AddFlags adds the global flags to the provided FlagSet.
58
func (f *GlobalFlags) AddFlags(flagset *pflag.FlagSet) {
1✔
59
        f.ConfigFlags.AddFlags(flagset)
1✔
60
        flagset.StringVarP(&f.OutputFormat, "output", "o", f.OutputFormat, "Output format. One of: table|json|yaml|wide")
1✔
61
        flagset.BoolVarP(&f.Verbose, "verbose", "v", false, "Enable verbose output")
1✔
62
        flagset.BoolVar(&f.DryRun, "dry-run", false, "Preview changes without applying them")
1✔
63
}
1✔
64

65
// Validate validates the global flags.
66
func (f *GlobalFlags) Validate() error {
1✔
67
        validFormats := map[string]struct{}{
1✔
68
                "table": {},
1✔
69
                "json":  {},
1✔
70
                "yaml":  {},
1✔
71
                "wide":  {},
1✔
72
        }
1✔
73
        f.OutputFormat = strings.ToLower(f.OutputFormat)
1✔
74
        if _, ok := validFormats[f.OutputFormat]; !ok {
2✔
75
                return fmt.Errorf("invalid output format %q", f.OutputFormat)
1✔
76
        }
1✔
77
        // No validation needed for boolean Verbose flag
78
        return nil
1✔
79
}
80

81
// Namespace returns the namespace selected via kubeconfig or flag (default "skyhook").
82
func (f *GlobalFlags) Namespace() string {
1✔
83
        if f.ConfigFlags == nil || f.ConfigFlags.Namespace == nil {
2✔
84
                return defaultNamespace
1✔
85
        }
1✔
86
        ns := strings.TrimSpace(*f.ConfigFlags.Namespace)
1✔
87
        if ns == "" {
2✔
88
                return defaultNamespace
1✔
89
        }
1✔
90
        return ns
1✔
91
}
92

93
// CLIContext holds the context that is passed around to every command.
94
// It contains global flags and can be extended with additional context in the future.
95
type CLIContext struct {
96
        GlobalFlags *GlobalFlags
97
        config      *CLIConfig
98
}
99

100
// CLIConfig holds the configuration for the CLI execution.
101
type CLIConfig struct {
102
        OutputWriter io.Writer
103
        ErrorWriter  io.Writer
104
}
105

106
// CLIConfigOption is a function that modifies a CLIConfig.
107
type CLIConfigOption func(*CLIConfig)
108

109
// WithOutputWriter sets the output writer for the CLI.
110
func WithOutputWriter(w io.Writer) CLIConfigOption {
1✔
111
        return func(c *CLIConfig) {
2✔
112
                c.OutputWriter = w
1✔
113
        }
1✔
114
}
115

116
// WithErrorWriter sets the error writer for the CLI.
117
func WithErrorWriter(w io.Writer) CLIConfigOption {
1✔
118
        return func(c *CLIConfig) {
2✔
119
                c.ErrorWriter = w
1✔
120
        }
1✔
121
}
122

123
// NewCLIConfig creates a new CLIConfig with the given options.
124
func NewCLIConfig(opts ...CLIConfigOption) *CLIConfig {
1✔
125
        config := &CLIConfig{
1✔
126
                OutputWriter: os.Stdout,
1✔
127
                ErrorWriter:  os.Stderr,
1✔
128
        }
1✔
129

1✔
130
        for _, opt := range opts {
2✔
131
                opt(config)
1✔
132
        }
1✔
133

134
        return config
1✔
135
}
136

137
// NewCLIContext creates a new CLIContext with default values.
138
// If config is nil, a default configuration is created.
139
func NewCLIContext(config *CLIConfig) *CLIContext {
1✔
140
        if config == nil {
2✔
141
                config = NewCLIConfig()
1✔
142
        }
1✔
143

144
        return &CLIContext{
1✔
145
                GlobalFlags: NewGlobalFlags(),
1✔
146
                config:      config,
1✔
147
        }
1✔
148
}
149

150
// Config returns the CLI configuration.
151
func (c *CLIContext) Config() *CLIConfig {
1✔
152
        return c.config
1✔
153
}
1✔
154

155
func (c *CLIContext) GetGlobalFlags() *GlobalFlags {
1✔
156
        return c.GlobalFlags
1✔
157
}
1✔
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