github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_sandbox_features.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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 main
    21  
    22  import (
    23  	"fmt"
    24  	"sort"
    25  	"strings"
    26  
    27  	"github.com/jessevdk/go-flags"
    28  
    29  	"github.com/snapcore/snapd/i18n"
    30  )
    31  
    32  var shortSandboxFeaturesHelp = i18n.G("Print sandbox features available on the system")
    33  var longSandboxFeaturesHelp = i18n.G(`
    34  The sandbox command prints tags describing features of individual sandbox
    35  components used by snapd on a given system.
    36  `)
    37  
    38  type cmdSandboxFeatures struct {
    39  	clientMixin
    40  	Required []string `long:"required" arg-name:"<backend feature>"`
    41  }
    42  
    43  func init() {
    44  	addDebugCommand("sandbox-features", shortSandboxFeaturesHelp, longSandboxFeaturesHelp, func() flags.Commander {
    45  		return &cmdSandboxFeatures{}
    46  	}, map[string]string{
    47  		"required": i18n.G("Ensure that given backend:feature is available"),
    48  	}, nil)
    49  }
    50  
    51  func (cmd cmdSandboxFeatures) Execute(args []string) error {
    52  	if len(args) > 0 {
    53  		return ErrExtraArgs
    54  	}
    55  
    56  	sysInfo, err := cmd.client.SysInfo()
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	sandboxFeatures := sysInfo.SandboxFeatures
    62  
    63  	if len(cmd.Required) > 0 {
    64  		avail := make(map[string]bool)
    65  		for backend := range sandboxFeatures {
    66  			for _, feature := range sandboxFeatures[backend] {
    67  				avail[fmt.Sprintf("%s:%s", backend, feature)] = true
    68  			}
    69  		}
    70  		for _, required := range cmd.Required {
    71  			if !avail[required] {
    72  				return fmt.Errorf("sandbox feature not available: %q", required)
    73  			}
    74  		}
    75  	} else {
    76  		backends := make([]string, 0, len(sandboxFeatures))
    77  		for backend := range sandboxFeatures {
    78  			backends = append(backends, backend)
    79  		}
    80  		sort.Strings(backends)
    81  		w := tabWriter()
    82  		defer w.Flush()
    83  		for _, backend := range backends {
    84  			fmt.Fprintf(w, "%s:\t%s\n", backend, strings.Join(sandboxFeatures[backend], " "))
    85  		}
    86  	}
    87  	return nil
    88  }