github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/pod-spec-set.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/gnuflag"
    10  
    11  	jujucmd "github.com/juju/juju/cmd"
    12  )
    13  
    14  // PodSpecSetCommand implements the pod-spec-set command.
    15  type PodSpecSetCommand struct {
    16  	cmd.CommandBase
    17  	ctx Context
    18  
    19  	specFile cmd.FileVar
    20  }
    21  
    22  // NewPodSpecSetCommand makes a pod-spec-set command.
    23  func NewPodSpecSetCommand(ctx Context) (cmd.Command, error) {
    24  	return &PodSpecSetCommand{ctx: ctx}, nil
    25  }
    26  
    27  func (c *PodSpecSetCommand) Info() *cmd.Info {
    28  	doc := `
    29  Sets configuration data to use for a pod.
    30  The spec applies to all units for the application.
    31  `
    32  	return jujucmd.Info(&cmd.Info{
    33  		Name:    "pod-spec-set",
    34  		Args:    "--file <pod spec file>",
    35  		Purpose: "set pod spec information",
    36  		Doc:     doc,
    37  	})
    38  }
    39  
    40  func (c *PodSpecSetCommand) SetFlags(f *gnuflag.FlagSet) {
    41  	c.specFile.SetStdin()
    42  	c.specFile.Path = "-"
    43  	f.Var(&c.specFile, "file", "file containing pod spec")
    44  }
    45  
    46  func (c *PodSpecSetCommand) Init(args []string) error {
    47  	return cmd.CheckEmpty(args)
    48  }
    49  
    50  func (c *PodSpecSetCommand) Run(ctx *cmd.Context) error {
    51  	specData, err := c.handleSpecFile(ctx)
    52  	if err != nil {
    53  		return errors.Trace(err)
    54  	}
    55  	return c.ctx.SetPodSpec(specData)
    56  }
    57  
    58  func (c *PodSpecSetCommand) handleSpecFile(ctx *cmd.Context) (string, error) {
    59  	specData, err := c.specFile.Read(ctx)
    60  	if err != nil {
    61  		return "", errors.Trace(err)
    62  	}
    63  	if len(specData) == 0 {
    64  		return "", errors.New("no pod spec specified: pipe pod spec to command, or specify a file with --file")
    65  	}
    66  	return string(specData), nil
    67  }