File: services.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (124 lines) | stat: -rw-r--r-- 3,466 bytes parent folder | download | duplicates (3)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2018 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 (
	"context"
	"errors"
	"fmt"
	"sort"
	"text/tabwriter"

	"github.com/snapcore/snapd/client/clientutil"
	"github.com/snapcore/snapd/i18n"
	"github.com/snapcore/snapd/overlord/servicestate"
	"github.com/snapcore/snapd/progress"
	"github.com/snapcore/snapd/snap"
)

var (
	shortServicesHelp = i18n.G("Query the status of services")
	longServicesHelp  = i18n.G(`
The services command lists information about the services specified.
`)
)

func init() {
	addCommand("services", shortServicesHelp, longServicesHelp, func() command { return &servicesCommand{} })
}

type servicesCommand struct {
	baseCommand
	Positional struct {
		ServiceNames []string `positional-arg-name:"<service>"`
	} `positional-args:"yes"`
	Global bool `long:"global" short:"g" description:"Show the global enable status for user services instead of the status for the current user"`
	User   bool `long:"user" short:"u" description:"Show the current status of the user services instead of the global enable status"`
}

type byApp []*snap.AppInfo

func (a byApp) Len() int      { return len(a) }
func (a byApp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byApp) Less(i, j int) bool {
	return a[i].Name < a[j].Name
}

var newStatusDecorator = func(ctx context.Context, isGlobal bool, uid string) clientutil.StatusDecorator {
	if isGlobal {
		return servicestate.NewStatusDecorator(progress.Null)
	} else {
		return servicestate.NewStatusDecoratorForUid(progress.Null, ctx, uid)
	}
}

func (c *servicesCommand) showGlobalEnablement() bool {
	if c.uid == "0" && !c.User {
		return true
	} else if c.uid != "0" && c.Global {
		return true
	}
	return false
}

func (c *servicesCommand) validateArguments() error {
	// can't use --global and --user together
	if c.Global && c.User {
		return errors.New(i18n.G("cannot combine --global and --user switches."))
	}
	return nil
}

// The 'snapctl services' command is one of the few commands that can run as
// non-root through snapctl.
func (c *servicesCommand) Execute([]string) error {
	ctx, err := c.ensureContext()
	if err != nil {
		return err
	}

	if err := c.validateArguments(); err != nil {
		return err
	}

	st := ctx.State()
	svcInfos, err := getServiceInfos(st, ctx.InstanceName(), c.Positional.ServiceNames)
	if err != nil {
		return err
	}
	sort.Sort(byApp(svcInfos))

	isGlobal := c.showGlobalEnablement()
	sd := newStatusDecorator(context.TODO(), isGlobal, c.uid)
	services, err := clientutil.ClientAppInfosFromSnapAppInfos(svcInfos, sd)
	if err != nil || len(services) == 0 {
		return err
	}

	w := tabwriter.NewWriter(c.stdout, 5, 3, 2, ' ', 0)
	defer w.Flush()

	fmt.Fprintln(w, i18n.G("Service\tStartup\tCurrent\tNotes"))
	for _, svc := range services {
		fmt.Fprintln(w, clientutil.FmtServiceStatus(&svc, isGlobal))
	}

	return nil
}