github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/addrelation.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/juju/cmd"
    10  	"github.com/juju/juju/cmd/envcmd"
    11  	"github.com/juju/juju/juju"
    12  )
    13  
    14  // AddRelationCommand adds a relation between two service endpoints.
    15  type AddRelationCommand struct {
    16  	envcmd.EnvCommandBase
    17  	Endpoints []string
    18  }
    19  
    20  func (c *AddRelationCommand) Info() *cmd.Info {
    21  	return &cmd.Info{
    22  		Name:    "add-relation",
    23  		Args:    "<service1>[:<relation name1>] <service2>[:<relation name2>]",
    24  		Purpose: "add a relation between two services",
    25  	}
    26  }
    27  
    28  func (c *AddRelationCommand) Init(args []string) error {
    29  	if len(args) != 2 {
    30  		return fmt.Errorf("a relation must involve two services")
    31  	}
    32  	c.Endpoints = args
    33  	return nil
    34  }
    35  
    36  func (c *AddRelationCommand) Run(_ *cmd.Context) error {
    37  	client, err := juju.NewAPIClientFromName(c.EnvName)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	defer client.Close()
    42  	_, err = client.AddRelation(c.Endpoints...)
    43  	return err
    44  }