github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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  	Positionals struct {
    34  		Offer disconnectSlotOrPlugSpec `required:"true"`
    35  		Use   disconnectSlotSpec
    36  	} `positional-args:"true"`
    37  }
    38  
    39  var shortDisconnectHelp = i18n.G("Disconnect a plug from a slot")
    40  var longDisconnectHelp = i18n.G(`
    41  The disconnect command disconnects a plug from a slot.
    42  It may be called in the following ways:
    43  
    44  $ snap disconnect <snap>:<plug> <snap>:<slot>
    45  
    46  Disconnects the specific plug from the specific slot.
    47  
    48  $ snap disconnect <snap>:<slot or plug>
    49  
    50  Disconnects everything from the provided plug or slot.
    51  The snap name may be omitted for the core snap.
    52  `)
    53  
    54  func init() {
    55  	addCommand("disconnect", shortDisconnectHelp, longDisconnectHelp, func() flags.Commander {
    56  		return &cmdDisconnect{}
    57  	}, waitDescs, []argDesc{
    58  		// TRANSLATORS: This needs to begin with < and end with >
    59  		{name: i18n.G("<snap>:<plug>")},
    60  		// TRANSLATORS: This needs to begin with < and end with >
    61  		{name: i18n.G("<snap>:<slot>")},
    62  	})
    63  }
    64  
    65  func (x *cmdDisconnect) Execute(args []string) error {
    66  	if len(args) > 0 {
    67  		return ErrExtraArgs
    68  	}
    69  
    70  	offer := x.Positionals.Offer.SnapAndName
    71  	use := x.Positionals.Use.SnapAndName
    72  
    73  	// snap disconnect <snap>:<slot>
    74  	// snap disconnect <snap>
    75  	if use.Snap == "" && use.Name == "" {
    76  		// Swap Offer and Use around
    77  		offer, use = use, offer
    78  	}
    79  	if use.Name == "" {
    80  		return fmt.Errorf("please provide the plug or slot name to disconnect from snap %q", use.Snap)
    81  	}
    82  
    83  	id, err := x.client.Disconnect(offer.Snap, offer.Name, use.Snap, use.Name)
    84  	if err != nil {
    85  		if client.IsInterfacesUnchangedError(err) {
    86  			fmt.Fprintf(Stdout, i18n.G("No connections to disconnect"))
    87  			fmt.Fprintf(Stdout, "\n")
    88  			return nil
    89  		}
    90  		return err
    91  	}
    92  
    93  	if _, err := x.wait(id); err != nil {
    94  		if err == noWait {
    95  			return nil
    96  		}
    97  		return err
    98  	}
    99  
   100  	return nil
   101  }