• 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/util.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
        "fmt"
18
        "os"
19
        "os/user"
20
        "path"
21
        "path/filepath"
22
        "strings"
23

24
        "github.com/pkg/errors"
25
        "github.com/spf13/cobra"
26
)
27

UNCOV
28
func CheckErr(e error) {
×
UNCOV
29
        if e != nil {
×
30
                fmt.Fprintf(os.Stderr, "FAILURE: %s\n", e.Error())
×
31
                os.Exit(1)
×
32
        }
×
33
}
34

UNCOV
35
func migrateAuthToken(oldtoken string, token string) {
×
UNCOV
36
        // if needed, migrate token from old to new location
×
UNCOV
37
        if _, err := os.Stat(token); !os.IsNotExist(err) {
×
UNCOV
38
                // new token exists, no migration
×
UNCOV
39
                return
×
UNCOV
40
        }
×
41

UNCOV
42
        if _, err := os.Stat(oldtoken); err != nil {
×
UNCOV
43
                // old token doesn't exist, no migration
×
UNCOV
44
                return
×
UNCOV
45
        }
×
46

47
        // Attempt migration, ignore errors (but log them?)
48
        if err := os.MkdirAll(filepath.Dir(token), 0700); err == nil {
×
49
                // log that token was moved?
×
50
                _ = os.Rename(oldtoken, token)
×
51
        }
×
52

53
        // Cleanup old token directory if empty
54
        _ = os.Remove(filepath.Dir(oldtoken)) // err on non-empty, ignore.
×
55
}
56

UNCOV
57
func getDefaultAuthTokenPath() (string, error) {
×
UNCOV
58
        cachedir := ""
×
UNCOV
59
        userhomedir := ""
×
UNCOV
60

×
UNCOV
61
        if homeenv := os.Getenv("HOME"); homeenv != "" {
×
UNCOV
62
                userhomedir = homeenv
×
UNCOV
63
        } else if user, err := user.Current(); err == nil {
×
64
                userhomedir = user.HomeDir
×
65
        } else {
×
66
                return "", errors.New("Not able to determine users cache dir")
×
67
        }
×
68

UNCOV
69
        if cachehomeenv := os.Getenv("XDG_CACHE_HOME"); cachehomeenv != "" {
×
70
                cachedir = cachehomeenv
×
UNCOV
71
        } else {
×
UNCOV
72
                cachedir = path.Join(userhomedir, ".cache")
×
UNCOV
73
        }
×
74

UNCOV
75
        oldtoken := filepath.Join(userhomedir, ".mender", "authtoken")
×
UNCOV
76
        token := filepath.Join(cachedir, "mender", "authtoken")
×
UNCOV
77

×
UNCOV
78
        migrateAuthToken(oldtoken, token)
×
UNCOV
79

×
UNCOV
80
        return token, nil
×
81
}
82

UNCOV
83
func getAuthToken(cmd *cobra.Command) (string, error) {
×
UNCOV
84
        tokenValue, err := cmd.Flags().GetString(argRootTokenValue)
×
UNCOV
85
        if err != nil {
×
86
                return "", err
×
87
        }
×
UNCOV
88
        tokenPath, err := cmd.Flags().GetString(argRootToken)
×
UNCOV
89
        if err != nil {
×
90
                return "", err
×
91
        }
×
92

UNCOV
93
        if tokenValue != "" && tokenPath != "" {
×
94
                return "", fmt.Errorf("cannot specify both --%s and --%s",
×
95
                        argRootTokenValue, argRootToken)
×
96
        }
×
97

UNCOV
98
        if tokenValue != "" {
×
99
                return tokenValue, nil
×
100
        }
×
101

UNCOV
102
        if tokenPath == "" {
×
UNCOV
103
                tokenPath, err = getDefaultAuthTokenPath()
×
UNCOV
104
                if err != nil {
×
105
                        return "", err
×
106
                }
×
107
        }
108

UNCOV
109
        token, err := os.ReadFile(tokenPath)
×
UNCOV
110
        if err != nil {
×
111
                return "", errors.Wrap(err, "Please Login first")
×
112
        }
×
UNCOV
113
        tokenValue = strings.TrimSpace(string(token))
×
UNCOV
114
        return tokenValue, nil
×
115
}
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