github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/cmd/snap/cmd_prefer.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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  	"github.com/jessevdk/go-flags"
    24  
    25  	"github.com/snapcore/snapd/i18n"
    26  )
    27  
    28  type cmdPrefer struct {
    29  	waitMixin
    30  	Positionals struct {
    31  		Snap installedSnapName `required:"yes"`
    32  	} `positional-args:"true"`
    33  }
    34  
    35  var shortPreferHelp = i18n.G("Enable aliases from a snap, disabling any conflicting aliases")
    36  var longPreferHelp = i18n.G(`
    37  The prefer command enables all aliases of the given snap in preference
    38  to conflicting aliases of other snaps whose aliases will be disabled
    39  (or removed, for manual ones).
    40  `)
    41  
    42  func init() {
    43  	addCommand("prefer", shortPreferHelp, longPreferHelp, func() flags.Commander {
    44  		return &cmdPrefer{}
    45  	}, waitDescs, []argDesc{
    46  		{name: "<snap>"},
    47  	})
    48  }
    49  
    50  func (x *cmdPrefer) Execute(args []string) error {
    51  	if len(args) > 0 {
    52  		return ErrExtraArgs
    53  	}
    54  
    55  	id, err := x.client.Prefer(string(x.Positionals.Snap))
    56  	if err != nil {
    57  		return err
    58  	}
    59  	chg, err := x.wait(id)
    60  	if err != nil {
    61  		if err == noWait {
    62  			return nil
    63  		}
    64  		return err
    65  	}
    66  
    67  	return showAliasChanges(chg)
    68  }