github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/commands/push.go (about)

     1  /*
     2   * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package commands
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  
    23  	"github.com/fnproject/cli/common"
    24  	"github.com/urfave/cli"
    25  )
    26  
    27  // PushCommand returns push cli.command
    28  func PushCommand() cli.Command {
    29  	cmd := pushcmd{}
    30  	var flags []cli.Flag
    31  	flags = append(flags, cmd.flags()...)
    32  	return cli.Command{
    33  		Name:        "push",
    34  		Usage:       "\tPush function to docker registry",
    35  		Aliases:     []string{"p"},
    36  		Category:    "DEVELOPMENT COMMANDS",
    37  		Description: "This command pushes the created image to the Docker registry.",
    38  		Flags:       flags,
    39  		Action:      cmd.push,
    40  	}
    41  }
    42  
    43  type pushcmd struct {
    44  	registry string
    45  }
    46  
    47  func (p *pushcmd) flags() []cli.Flag {
    48  	return []cli.Flag{
    49  		cli.BoolFlag{
    50  			Name:        "verbose, v",
    51  			Usage:       "Verbose mode",
    52  			Destination: &common.CommandVerbose,
    53  		},
    54  		cli.StringFlag{
    55  			Name:        "registry",
    56  			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 only the owner prefix. `--registry registry.hub.docker.com/username` will set the registry and owner.",
    57  			Destination: &p.registry,
    58  		},
    59  	}
    60  }
    61  
    62  // push will take the found function and check for the presence of a
    63  // Dockerfile, and run a three step process: parse functions file,
    64  // push the container, and finally it will update the function. Optionally,
    65  // the function can be overriden inside the functions file.
    66  func (p *pushcmd) push(c *cli.Context) error {
    67  	ffV, err := common.ReadInFuncFile()
    68  	version := common.GetFuncYamlVersion(ffV)
    69  	if version == common.LatestYamlVersion {
    70  		_, ff, err := common.LoadFuncFileV20180708(".")
    71  		if err != nil {
    72  			if _, ok := err.(*common.NotFoundError); ok {
    73  				return errors.New("Image name is missing or no function file found")
    74  			}
    75  			return err
    76  		}
    77  
    78  		fmt.Println("pushing", ff.ImageNameV20180708())
    79  
    80  		if err := common.PushV20180708(ff); err != nil {
    81  			return err
    82  		}
    83  
    84  		fmt.Printf("Function %v pushed successfully to the registry.\n", ff.ImageNameV20180708())
    85  		return nil
    86  	}
    87  
    88  	_, ff, err := common.LoadFuncfile(".")
    89  
    90  	if err != nil {
    91  		if _, ok := err.(*common.NotFoundError); ok {
    92  			return errors.New("Image name is missing or no function file found")
    93  		}
    94  		return err
    95  	}
    96  
    97  	fmt.Println("pushing", ff.ImageName())
    98  
    99  	if err := common.Push(ff); err != nil {
   100  		return err
   101  	}
   102  
   103  	fmt.Printf("Function %v pushed successfully to the registry.\n", ff.ImageName())
   104  	return nil
   105  }