github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/deploy/deploy.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package deploy provides an implementation of "gactions deploy" command.
    16  package deploy
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"github.com/actions-on-google/gactions/api/sdk"
    23  	"github.com/actions-on-google/gactions/project"
    24  	"github.com/actions-on-google/gactions/project/studio"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func setProjectID(project *project.Project) error {
    29  	studioProj, ok := (*project).(studio.Studio)
    30  	if !ok {
    31  		return fmt.Errorf("can not convert %T to %T", project, studio.Studio{})
    32  	}
    33  	if err := (&studioProj).SetProjectID(""); err != nil {
    34  		return err
    35  	}
    36  	*project = studioProj
    37  	return nil
    38  }
    39  
    40  // AddCommand adds the deploy sub-command to the passed in root command.
    41  func AddCommand(ctx context.Context, root *cobra.Command, project project.Project) {
    42  	deploy := &cobra.Command{
    43  		Use:   "deploy",
    44  		Short: "Deploy an Action to the specified channel.",
    45  		Long:  "This command deploys an Action to the specified channel.",
    46  		Args:  cobra.MinimumNArgs(1),
    47  	}
    48  	preview := &cobra.Command{
    49  		Use:   "preview",
    50  		Short: "Deploy for preview.",
    51  		Long:  "This command deploys an Action to preview, so you can test your Action in the simulator.",
    52  		RunE: func(cmd *cobra.Command, args []string) error {
    53  			sandbox, _ := cmd.Flags().GetBool("sandbox")
    54  			if err := setProjectID(&project); err != nil {
    55  				return err
    56  			}
    57  			return sdk.WritePreviewJSON(ctx, project, sandbox)
    58  		},
    59  	}
    60  	preview.Flags().Bool("sandbox", true,
    61  		"Indicates whether or not to run certain operations, such as transactions, in sandbox mode. The default value is set to true")
    62  	alpha := &cobra.Command{
    63  		Use:   "alpha",
    64  		Short: "Deploy to alpha channel.",
    65  		Long:  "This command deploys to alpha channel.",
    66  		RunE: func(cmd *cobra.Command, args []string) error {
    67  			if err := setProjectID(&project); err != nil {
    68  				return err
    69  			}
    70  			return sdk.CreateVersionJSON(ctx, project, sdk.AlphaChannel)
    71  		},
    72  	}
    73  	beta := &cobra.Command{
    74  		Use:   "beta",
    75  		Short: "Deploy to beta channel.",
    76  		Long:  "This command deploys to beta channel.",
    77  		RunE: func(cmd *cobra.Command, args []string) error {
    78  			if err := setProjectID(&project); err != nil {
    79  				return err
    80  			}
    81  			return sdk.CreateVersionJSON(ctx, project, sdk.BetaChannel)
    82  		},
    83  	}
    84  	prod := &cobra.Command{
    85  		Use:   "prod",
    86  		Short: "Deploy to production channel.",
    87  		Long:  "This command deploys to production channel.",
    88  		RunE: func(cmd *cobra.Command, args []string) error {
    89  			if err := setProjectID(&project); err != nil {
    90  				return err
    91  			}
    92  			return sdk.CreateVersionJSON(ctx, project, sdk.ProdChannel)
    93  		},
    94  	}
    95  	deploy.AddCommand(preview)
    96  	deploy.AddCommand(alpha)
    97  	deploy.AddCommand(beta)
    98  	deploy.AddCommand(prod)
    99  	root.AddCommand(deploy)
   100  }