forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_ready.go
More file actions
88 lines (71 loc) · 2.21 KB
/
Copy pathis_ready.go
File metadata and controls
88 lines (71 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2026 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ctlcmd
import (
"fmt"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/overlord/state"
)
type isReadyCommand struct {
baseCommand
}
const (
changeReadyExitCode = iota
fatalErrorExitCode
changeUnsuccessfulExitCode
changeNotReadyExitCode
)
var shortIsReadyHelp = i18n.G(`Return the status of the associated change id.`)
var longIsReadyHelp = i18n.G(`
The is-ready command is used to query the status of change ids that are returned
by asynchronous snapctl commands.
$ snapctl is-ready <change-id>
0: change completed successfully (Done)
1: fatal errors (invalid change id, permissions error)
2: change is ready but did not complete successfully (Undone, Error, Hold)
3: change is not ready
stdout: empty, exit code conveys change readiness
stderr: empty for exit codes 0 and 3. Contains relevant errors for exit codes 1 and 2.
`)
func init() {
addCommand("is-ready", shortIsReadyHelp, longIsReadyHelp, func() command {
return &isReadyCommand{}
})
}
func (c *isReadyCommand) Execute(args []string) error {
ctx, err := c.ensureContext()
if err != nil {
return err
}
if len(args) != 1 {
return fmt.Errorf("invalid number of arguments: expected 1, got %d", len(args))
}
changeID := args[0]
ready, err := isReady(ctx, changeID)
if err != nil {
return err
}
if !ready.Ready() {
return &UnsuccessfulError{ExitCode: changeNotReadyExitCode}
}
if ready != state.DoneStatus {
fmt.Fprintf(c.stderr, "change finished with status %s", ready)
return &UnsuccessfulError{ExitCode: changeUnsuccessfulExitCode}
}
return nil
}