|
| 1 | +// -*- Mode: Go; indent-tabs-mode: t -*- |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (C) 2026 Canonical Ltd |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License version 3 as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +package ctlcmd |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + |
| 25 | + "github.com/snapcore/snapd/i18n" |
| 26 | + "github.com/snapcore/snapd/overlord/state" |
| 27 | +) |
| 28 | + |
| 29 | +type tasksCommand struct { |
| 30 | + baseCommand |
| 31 | + json bool |
| 32 | +} |
| 33 | + |
| 34 | +var shortTasksHelp = i18n.G(`Return a list of information associated with all change-ids.`) |
| 35 | +var longTasksHelp = i18n.G(` |
| 36 | +The tasks command is used to query the status of all change ids associated with |
| 37 | +snapctl commands running in asynchronous mode. |
| 38 | +
|
| 39 | +$ snapctl tasks [--json] |
| 40 | + 0: successfully reported change information, regardless of state of change |
| 41 | + 1: any error (invalid change ID, permissions error) |
| 42 | +stdout: table of tasks, mirroring "snap tasks <change-id>" output |
| 43 | +stderr: empty for exit code 0. Contains relevant errors for exit code 1. |
| 44 | +`) |
| 45 | + |
| 46 | +func init() { |
| 47 | + addCommand("tasks", shortTasksHelp, longTasksHelp, func() command { |
| 48 | + return &tasksCommand{} |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func (c *tasksCommand) Execute(args []string) error { |
| 53 | + ctx, err := c.ensureContext() |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + if len(args) != 1 { |
| 59 | + return fmt.Errorf("invalid number of arguments: expected 1, got %d", len(args)) |
| 60 | + } |
| 61 | + |
| 62 | + c.json = c.flagSet.Lookup("json").Value.String() == "true" |
| 63 | + |
| 64 | + ready, err := isReady(ctx, c.changeID) |
| 65 | + |
| 66 | + if err != nil { |
| 67 | + fmt.Fprintf(c.stderr, err.Error()) |
| 68 | + return &UnsuccessfulError{ExitCode: otherErrorExitCode} |
| 69 | + } |
| 70 | + |
| 71 | + if !ready.Ready() { |
| 72 | + return &UnsuccessfulError{ExitCode: changeNotReadyExitCode} |
| 73 | + } |
| 74 | + |
| 75 | + if ready != state.DoneStatus { |
| 76 | + return &UnsuccessfulError{ExitCode: changeUnsuccessfulExitCode} |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
0 commit comments