github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/commands/push.go (about)

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/fnproject/cli/common"
     8  	"github.com/urfave/cli"
     9  )
    10  
    11  // PushCommand returns push cli.command
    12  func PushCommand() cli.Command {
    13  	cmd := pushcmd{}
    14  	var flags []cli.Flag
    15  	flags = append(flags, cmd.flags()...)
    16  	return cli.Command{
    17  		Name:        "push",
    18  		Usage:       "\tPush function to docker registry",
    19  		Aliases:     []string{"p"},
    20  		Category:    "DEVELOPMENT COMMANDS",
    21  		Description: "This command pushes the created image to the Docker registry.",
    22  		Flags:       flags,
    23  		Action:      cmd.push,
    24  	}
    25  }
    26  
    27  type pushcmd struct {
    28  	verbose  bool
    29  	registry string
    30  }
    31  
    32  func (p *pushcmd) flags() []cli.Flag {
    33  	return []cli.Flag{
    34  		cli.BoolFlag{
    35  			Name:        "v",
    36  			Usage:       "Verbose mode",
    37  			Destination: &p.verbose,
    38  		},
    39  		cli.StringFlag{
    40  			Name:        "registry",
    41  			Usage:       "Set the Docker owner for images and optionally the registry. This will be prefixed to your function name for pushing to Docker registries.\n eg: `--registry username` will set your Docker Hub owner. `--registry registry.hub.docker.com/username` will set the registry and owner.",
    42  			Destination: &p.registry,
    43  		},
    44  	}
    45  }
    46  
    47  // push will take the found function and check for the presence of a
    48  // Dockerfile, and run a three step process: parse functions file,
    49  // push the container, and finally it will update the function. Optionally,
    50  // the function can be overriden inside the functions file.
    51  func (p *pushcmd) push(c *cli.Context) error {
    52  	ffV, err := common.ReadInFuncFile()
    53  	version := common.GetFuncYamlVersion(ffV)
    54  	if version == common.LatestYamlVersion {
    55  		_, ff, err := common.LoadFuncFileV20180708(".")
    56  		if err != nil {
    57  			if _, ok := err.(*common.NotFoundError); ok {
    58  				return errors.New("Image name is missing or no function file found")
    59  			}
    60  			return err
    61  		}
    62  
    63  		fmt.Println("pushing", ff.ImageNameV20180708())
    64  
    65  		if err := common.DockerPushV20180708(ff); err != nil {
    66  			return err
    67  		}
    68  
    69  		fmt.Printf("Function %v pushed successfully to Docker Hub.\n", ff.ImageNameV20180708())
    70  		return nil
    71  	}
    72  
    73  	_, ff, err := common.LoadFuncfile(".")
    74  
    75  	if err != nil {
    76  		if _, ok := err.(*common.NotFoundError); ok {
    77  			return errors.New("Image name is missing or no function file found")
    78  		}
    79  		return err
    80  	}
    81  
    82  	fmt.Println("pushing", ff.ImageName())
    83  
    84  	if err := common.DockerPush(ff); err != nil {
    85  		return err
    86  	}
    87  
    88  	fmt.Printf("Function %v pushed successfully to Docker Hub.\n", ff.ImageName())
    89  	return nil
    90  }