github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_connect.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/snapcore/snapd/i18n"
    24  
    25  	"github.com/jessevdk/go-flags"
    26  )
    27  
    28  type cmdConnect struct {
    29  	waitMixin
    30  	Positionals struct {
    31  		PlugSpec connectPlugSpec `required:"yes"`
    32  		SlotSpec connectSlotSpec
    33  	} `positional-args:"true"`
    34  }
    35  
    36  var shortConnectHelp = i18n.G("Connect a plug to a slot")
    37  var longConnectHelp = i18n.G(`
    38  The connect command connects a plug to a slot.
    39  It may be called in the following ways:
    40  
    41  $ snap connect <snap>:<plug> <snap>:<slot>
    42  
    43  Connects the provided plug to the given slot.
    44  
    45  $ snap connect <snap>:<plug> <snap>
    46  
    47  Connects the specific plug to the only slot in the provided snap that matches
    48  the connected interface. If more than one potential slot exists, the command
    49  fails.
    50  
    51  $ snap connect <snap>:<plug>
    52  
    53  Connects the provided plug to the slot in the core snap with a name matching
    54  the plug name.
    55  `)
    56  
    57  func init() {
    58  	addCommand("connect", shortConnectHelp, longConnectHelp, func() flags.Commander {
    59  		return &cmdConnect{}
    60  	}, waitDescs, []argDesc{
    61  		// TRANSLATORS: This needs to begin with < and end with >
    62  		{name: i18n.G("<snap>:<plug>")},
    63  		// TRANSLATORS: This needs to begin with < and end with >
    64  		{name: i18n.G("<snap>:<slot>")},
    65  	})
    66  }
    67  
    68  func (x *cmdConnect) Execute(args []string) error {
    69  	if len(args) > 0 {
    70  		return ErrExtraArgs
    71  	}
    72  
    73  	// snap connect <plug> <snap>[:<slot>]
    74  	if x.Positionals.PlugSpec.Snap != "" && x.Positionals.PlugSpec.Name == "" {
    75  		// Move the value of .Snap to .Name and keep .Snap empty
    76  		x.Positionals.PlugSpec.Name = x.Positionals.PlugSpec.Snap
    77  		x.Positionals.PlugSpec.Snap = ""
    78  	}
    79  
    80  	id, err := x.client.Connect(x.Positionals.PlugSpec.Snap, x.Positionals.PlugSpec.Name, x.Positionals.SlotSpec.Snap, x.Positionals.SlotSpec.Name)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	if _, err := x.wait(id); err != nil {
    86  		if err == noWait {
    87  			return nil
    88  		}
    89  		return err
    90  	}
    91  
    92  	return nil
    93  }