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

mendersoftware / mender-cli / 1787764283

26 Apr 2025 10:04AM UTC coverage: 1.737%. Remained the same
1787764283

Pull #249

gitlab-ci

bitcloud
Merge remote-tracking branch 'upstream/master' into change-config-path-priority
Pull Request #249: change priority of rc config discovery

0 of 2 new or added lines in 1 file covered. (0.0%)

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

41
func init() {
×
42
        viper.SetConfigName(".mender-clirc")
×
43
        viper.SetConfigType("json")
×
44
        viper.AddConfigPath(".")
×
NEW
45
        viper.AddConfigPath("$HOME/")
×
NEW
46
        viper.AddConfigPath("/etc/mender-cli/")
×
47
        if err := viper.ReadInConfig(); err != nil {
×
48
                if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
×
49
                        log.Info(fmt.Sprintf("Failed to read config: %s", err))
×
50
                        os.Exit(1)
×
51
                } else {
×
52
                        log.Info("Configuration file not found. Continuing.")
×
53
                }
×
54
        } else {
×
55
                fmt.Fprintf(os.Stderr, "Using configuration file: %s\n", viper.ConfigFileUsed())
×
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
65
        PersistentPreRun: func(cmd *cobra.Command, args []string) {
×
66
                verbose, err := cmd.Flags().GetBool(argRootVerbose)
×
67
                CheckErr(err)
×
68
                log.Setup(verbose)
×
69
                if verbose {
×
70
                        log.Verb("verbose output is ON")
×
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.
78
func Execute() {
×
79
        lflags := rootCmd.LocalFlags()
×
80
        lflags.ParseErrorsWhitelist.UnknownFlags = true
×
81
        err := lflags.Parse(os.Args)
×
82
        if err != nil && !errors.Is(err, pflag.ErrHelp) {
×
83
                log.Errf("Failed to parse flags: %s\n", err)
×
84
        }
×
85
        b, _ := rootCmd.Flags().GetBool(argRootGenerate)
×
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
        }
97
        version, err := rootCmd.Flags().GetBool(argRootVersion)
×
98
        CheckErr(err)
×
99
        if version {
×
100
                fmt.Printf("mender-cli version %s\n", Version)
×
101
                os.Exit(0)
×
102
        }
×
103
        CheckErr(rootCmd.Execute())
×
104
}
105

106
func validateConfiguration() {
×
107
        server := viper.GetString(argRootServer)
×
108
        u, _ := url.Parse(server)
×
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

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