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

mendersoftware / mender-cli / 1807248277

08 May 2025 11:21AM UTC coverage: 1.701% (-0.04%) from 1.737%
1807248277

Pull #284

gitlab-ci

danielskinstad
chore: add input sanitization to devices list

Return error if page or per-page argument is less than 1

Signed-off-by: Daniel Skinstad Drabitzius <daniel.drabitzius@northern.tech>
Pull Request #284: feat: use paginated endpoint to list artifacts

0 of 64 new or added lines in 3 files covered. (0.0%)

4 existing lines in 2 files now uncovered.

45 of 2646 relevant lines covered (1.7%)

0.03 hits per line

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

0.0
/cmd/devices_list.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

19
        "github.com/spf13/cobra"
20
        "github.com/spf13/viper"
21

22
        "github.com/mendersoftware/mender-cli/client/devices"
23
)
24

25
var devicesListCmd = &cobra.Command{
26
        Use:   "list",
27
        Short: "Get a list of devices from the Mender server.",
28
        Run: func(c *cobra.Command, args []string) {
×
29
                cmd, err := NewDevicesListCmd(c, args)
×
30
                CheckErr(err)
×
31
                CheckErr(cmd.Run())
×
32
        },
×
33
}
34

35
const (
36
        argRawMode = "raw"
37
        argPerPage = "per-page"
38
        argPage    = "page"
39
)
40

41
func init() {
×
42
        devicesListCmd.Flags().IntP(argDetailLevel, "d", 0, "devices list detail level [0..3]")
×
43
        devicesListCmd.Flags().IntP(argPerPage, "N", 20, "Number of results to display")
×
44
        devicesListCmd.Flags().IntP(argPage, "P", 1, "Page number to return")
×
45
        devicesListCmd.Flags().BoolP(
×
46
                argRawMode,
×
47
                "r",
×
48
                false,
×
49
                "devices list raw mode (json from mender server)")
×
50
}
×
51

52
type DevicesListCmd struct {
53
        server        string
54
        skipVerify    bool
55
        token         string
56
        detailLevel   int
57
        rawMode       bool
58
        page, perPage int
59
}
60

61
func NewDevicesListCmd(cmd *cobra.Command, args []string) (*DevicesListCmd, error) {
×
62
        server := viper.GetString(argRootServer)
×
63
        if server == "" {
×
64
                return nil, errors.New("No server")
×
65
        }
×
66

67
        flags := cmd.Flags()
×
68

×
69
        skipVerify, err := flags.GetBool(argRootSkipVerify)
×
70
        if err != nil {
×
71
                return nil, err
×
72
        }
×
73

74
        detailLevel, err := flags.GetInt(argDetailLevel)
×
75
        if err != nil {
×
76
                return nil, err
×
77
        }
×
78

79
        rawMode, err := flags.GetBool(argRawMode)
×
80
        if err != nil {
×
81
                return nil, err
×
82
        }
×
83

84
        perPage, err := flags.GetInt(argPerPage)
×
85
        if err != nil {
×
86
                return nil, err
×
87
        }
×
88

89
        page, err := flags.GetInt(argPage)
×
90
        if err != nil {
×
91
                return nil, err
×
92
        }
×
93

NEW
94
        if page <= 0 || perPage <= 0 {
×
NEW
95
                return nil, errors.New("page and per-page arguments must be larger than 0")
×
NEW
96
        }
×
97

98
        token, err := getAuthToken(cmd)
×
99
        if err != nil {
×
100
                return nil, err
×
101
        }
×
102

103
        return &DevicesListCmd{
×
104
                server:      server,
×
105
                token:       token,
×
106
                skipVerify:  skipVerify,
×
107
                detailLevel: detailLevel,
×
108
                rawMode:     rawMode,
×
109
                perPage:     perPage,
×
110
                page:        page,
×
111
        }, nil
×
112
}
113

114
func (c *DevicesListCmd) Run() error {
×
115

×
116
        client := devices.NewClient(c.server, c.skipVerify)
×
117
        return client.ListDevices(c.token, c.detailLevel, c.perPage, c.page, c.rawMode)
×
118
}
×
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