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

mendersoftware / mender-cli / 1779499080

22 Apr 2025 10:11AM UTC coverage: 1.737% (-30.1%) from 31.802%
1779499080

push

gitlab-ci

web-flow
Merge pull request #277 from alfrunes/MEN-7794

MEN-7794: Add support for pagination when listing devices

28 of 82 new or added lines in 4 files covered. (34.15%)

770 existing lines in 17 files now uncovered.

45 of 2590 relevant lines covered (1.74%)

0.04 hits per line

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

0.0
/cmd/root.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package cmd
15

16
import (
17
        "errors"
18
        "fmt"
19
        "net/url"
20
        "os"
21

22
        "github.com/spf13/cobra"
23
        "github.com/spf13/pflag"
24
        "github.com/spf13/viper"
25

26
        "github.com/mendersoftware/mender-cli/log"
27
)
28

29
var Version string
30

31
const (
32
        argRootServer     = "server"
33
        argRootSkipVerify = "skip-verify"
34
        argRootToken      = "token"
35
        argRootTokenValue = "token-value"
36
        argRootVerbose    = "verbose"
37
        argRootGenerate   = "generate-autocomplete"
38
        argRootVersion    = "version"
39
)
40

UNCOV
41
func init() {
×
UNCOV
42
        viper.SetConfigName(".mender-clirc")
×
UNCOV
43
        viper.SetConfigType("json")
×
UNCOV
44
        viper.AddConfigPath("/etc/mender-cli/")
×
UNCOV
45
        viper.AddConfigPath("$HOME/")
×
UNCOV
46
        viper.AddConfigPath(".")
×
UNCOV
47
        if err := viper.ReadInConfig(); err != nil {
×
UNCOV
48
                if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
×
49
                        log.Info(fmt.Sprintf("Failed to read config: %s", err))
×
50
                        os.Exit(1)
×
UNCOV
51
                } else {
×
UNCOV
52
                        log.Info("Configuration file not found. Continuing.")
×
UNCOV
53
                }
×
UNCOV
54
        } else {
×
UNCOV
55
                fmt.Fprintf(os.Stderr, "Using configuration file: %s\n", viper.ConfigFileUsed())
×
UNCOV
56
        }
×
57
}
58

59
// rootCmd represents the base command when called without any subcommands
60
var rootCmd = &cobra.Command{
61
        Use:   "mender-cli",
62
        Short: "A general-purpose CLI for the Mender server.",
63

64
        //setup global stuff, will run regardless of (sub)command
UNCOV
65
        PersistentPreRun: func(cmd *cobra.Command, args []string) {
×
UNCOV
66
                verbose, err := cmd.Flags().GetBool(argRootVerbose)
×
UNCOV
67
                CheckErr(err)
×
UNCOV
68
                log.Setup(verbose)
×
UNCOV
69
                if verbose {
×
UNCOV
70
                        log.Verb("verbose output is ON")
×
UNCOV
71
                }
×
72
        },
73
        ValidArgs: []string{"artifacts", "help", "login"},
74
}
75

76
// Execute adds all child commands to the root command and sets flags appropriately.
77
// This is called by main.main(). It only needs to happen once to the rootCmd.
UNCOV
78
func Execute() {
×
UNCOV
79
        lflags := rootCmd.LocalFlags()
×
UNCOV
80
        lflags.ParseErrorsWhitelist.UnknownFlags = true
×
UNCOV
81
        err := lflags.Parse(os.Args)
×
UNCOV
82
        if err != nil && !errors.Is(err, pflag.ErrHelp) {
×
83
                log.Errf("Failed to parse flags: %s\n", err)
×
84
        }
×
UNCOV
85
        b, _ := rootCmd.Flags().GetBool(argRootGenerate)
×
UNCOV
86
        if b {
×
87
                err := rootCmd.GenBashCompletionFile("./autocomplete/autocomplete.sh")
×
88
                if err != nil {
×
89
                        log.Errf("Failed to generate the Bash autocompletion scripts: %s\n", err)
×
90
                }
×
91
                err = rootCmd.GenZshCompletionFile("./autocomplete/autocomplete.zsh")
×
92
                if err != nil {
×
93
                        log.Errf("Failed to generate the Zsh autocompletion scripts: %s\n", err)
×
94
                }
×
95
                return
×
96
        }
UNCOV
97
        version, err := rootCmd.Flags().GetBool(argRootVersion)
×
UNCOV
98
        CheckErr(err)
×
UNCOV
99
        if version {
×
100
                fmt.Printf("mender-cli version %s\n", Version)
×
101
                os.Exit(0)
×
102
        }
×
UNCOV
103
        CheckErr(rootCmd.Execute())
×
104
}
105

UNCOV
106
func validateConfiguration() {
×
UNCOV
107
        server := viper.GetString(argRootServer)
×
UNCOV
108
        u, _ := url.Parse(server)
×
UNCOV
109
        if u.Scheme == "" {
×
110
                viper.Set(argRootServer, "https://"+server)
×
111
                log.Info("Protocol is not specified, HTTPS is used by default.")
×
112
        }
×
113
}
114

UNCOV
115
func init() {
×
UNCOV
116
        // Here you will define your flags and configuration settings.
×
UNCOV
117
        // Cobra supports persistent flags, which, if defined here,
×
UNCOV
118
        // will be global for your application.
×
UNCOV
119
        rootCmd.PersistentFlags().
×
UNCOV
120
                StringP(
×
UNCOV
121
                        argRootServer,
×
UNCOV
122
                        "",
×
UNCOV
123
                        "https://hosted.mender.io",
×
UNCOV
124
                        "root server URL, e.g. 'https://hosted.mender.io'",
×
UNCOV
125
                )
×
UNCOV
126
        _ = viper.BindPFlag(argRootServer, rootCmd.PersistentFlags().Lookup(argRootServer))
×
UNCOV
127
        rootCmd.PersistentFlags().
×
UNCOV
128
                BoolP(argRootSkipVerify, "k", false, "skip SSL certificate verification")
×
UNCOV
129
        rootCmd.PersistentFlags().StringP(argRootToken, "", "", "JWT token file path")
×
UNCOV
130
        rootCmd.PersistentFlags().StringP(argRootTokenValue, "", "", "JWT token value (API key)")
×
UNCOV
131
        rootCmd.PersistentFlags().BoolP(argRootVerbose, "v", false, "print verbose output")
×
UNCOV
132
        rootCmd.Flags().Bool(argRootVersion, false, "print version")
×
UNCOV
133
        rootCmd.Flags().Bool(argRootGenerate, false, "generate shell completion script")
×
UNCOV
134
        _ = rootCmd.Flags().MarkHidden(argRootGenerate)
×
UNCOV
135
        rootCmd.AddCommand(loginCmd)
×
UNCOV
136
        rootCmd.AddCommand(artifactsCmd)
×
UNCOV
137
        rootCmd.AddCommand(devicesCmd)
×
UNCOV
138
        rootCmd.AddCommand(terminalCmd)
×
UNCOV
139
        rootCmd.AddCommand(portForwardCmd)
×
UNCOV
140
        rootCmd.AddCommand(fileTransferCmd)
×
UNCOV
141
        validateConfiguration()
×
UNCOV
142
}
×
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

© 2025 Coveralls, Inc