github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_disconnect.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  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/client"
    26  	"github.com/snapcore/snapd/i18n"
    27  
    28  	"github.com/jessevdk/go-flags"
    29  )
    30  
    31  type cmdDisconnect struct {
    32  	waitMixin
    33  	Forget      bool `long:"forget"`
    34  	Positionals struct {
    35  		Offer disconnectSlotOrPlugSpec `required:"true"`
    36  		Use   disconnectSlotSpec
    37  	} `positional-args:"true"`
    38  }
    39  
    40  var shortDisconnectHelp = i18n.G("Disconnect a plug from a slot")
    41  var longDisconnectHelp = i18n.G(`
    42  The disconnect command disconnects a plug from a slot.
    43  It may be called in the following ways:
    44  
    45  $ snap disconnect <snap>:<plug> <snap>:<slot>
    46  
    47  Disconnects the specific plug from the specific slot.
    48  
    49  $ snap disconnect <snap>:<slot or plug>
    50  
    51  Disconnects everything from the provided plug or slot.
    52  The snap name may be omitted for the core snap.
    53  
    54  When an automatic connection is manually disconnected, its disconnected state
    55  is retained after a snap refresh. The --forget flag can be added to the
    56  disconnect command to reset this behaviour, and consequently re-enable
    57  an automatic reconnection after a snap refresh.
    58  `)
    59  
    60  func init() {
    61  	addCommand("disconnect", shortDisconnectHelp, longDisconnectHelp, func() flags.Commander {
    62  		return &cmdDisconnect{}
    63  	}, waitDescs.also(map[string]string{"forget": "Forget remembered state about the given connection."}), []argDesc{
    64  		// TRANSLATORS: This needs to begin with < and end with >
    65  		{name: i18n.G("<snap>:<plug>")},
    66  		// TRANSLATORS: This needs to begin with < and end with >
    67  		{name: i18n.G("<snap>:<slot>")},
    68  	})
    69  }
    70  
    71  func (x *cmdDisconnect) Execute(args []string) error {
    72  	if len(args) > 0 {
    73  		return ErrExtraArgs
    74  	}
    75  
    76  	offer := x.Positionals.Offer.SnapAndName
    77  	use := x.Positionals.Use.SnapAndName
    78  
    79  	// snap disconnect <snap>:<slot>
    80  	// snap disconnect <snap>
    81  	if use.Snap == "" && use.Name == "" {
    82  		// Swap Offer and Use around
    83  		offer, use = use, offer
    84  	}
    85  	if use.Name == "" {
    86  		return fmt.Errorf("please provide the plug or slot name to disconnect from snap %q", use.Snap)
    87  	}
    88  
    89  	opts := &client.DisconnectOptions{Forget: x.Forget}
    90  	id, err := x.client.Disconnect(offer.Snap, offer.Name, use.Snap, use.Name, opts)
    91  	if err != nil {
    92  		if client.IsInterfacesUnchangedError(err) {
    93  			fmt.Fprintf(Stdout, i18n.G("No connections to disconnect"))
    94  			fmt.Fprintf(Stdout, "\n")
    95  			return nil
    96  		}
    97  		return err
    98  	}
    99  
   100  	if _, err := x.wait(id); err != nil {
   101  		if err == noWait {
   102  			return nil
   103  		}
   104  		return err
   105  	}
   106  
   107  	return nil
   108  }