github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/ginit/ginit.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 ginit provides an implementation of "gactions init" command.
    16  // Note(atulep): Switching package name to "init" led to compilation errors.
    17  package ginit
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"text/tabwriter"
    24  
    25  	"github.com/actions-on-google/gactions/api/sdk"
    26  	"github.com/actions-on-google/gactions/log"
    27  	"github.com/actions-on-google/gactions/project"
    28  
    29  	"github.com/spf13/cobra"
    30  )
    31  
    32  var (
    33  	samples []project.SampleProject
    34  )
    35  
    36  func isValidProject(projectTitle string) bool {
    37  	for _, v := range samples {
    38  		if v.Name == projectTitle {
    39  			return true
    40  		}
    41  	}
    42  	return false
    43  }
    44  
    45  func printSamples(samples []project.SampleProject) {
    46  	w := new(tabwriter.Writer)
    47  	w.Init(os.Stdout, 0, 4, 0, '\t', 0)
    48  	for i, v := range samples {
    49  		fmt.Fprintf(w, "%v) %v\t\n", i+1, v.Name)
    50  	}
    51  	fmt.Fprintln(w)
    52  	w.Flush()
    53  }
    54  
    55  var availableProjects = func(ctx context.Context, project project.Project) ([]project.SampleProject, error) {
    56  	return sdk.ListSampleProjectsJSON(ctx, project)
    57  }
    58  
    59  // AddCommand adds the init sub-command to the passed in root command.
    60  func AddCommand(ctx context.Context, root *cobra.Command, project project.Project) {
    61  	init := &cobra.Command{
    62  		Use:   "init",
    63  		Short: "Initialize a directory for a new project.",
    64  		Long:  "This command places sample Actions SDK project files into the current directory. You can choose from a list of sample projects. Current directory must be empty.",
    65  		RunE: func(cmd *cobra.Command, args []string) error {
    66  			return doInit(cmd, args, project)
    67  		},
    68  		Args: func(cmd *cobra.Command, args []string) error {
    69  			if len(args) > 1 {
    70  				return fmt.Errorf("unexpected arguments: %v", args)
    71  			}
    72  			l, err := availableProjects(ctx, project)
    73  			if err != nil {
    74  				return err
    75  			}
    76  			samples = l
    77  			if len(args) < 1 || !isValidProject(args[0]) {
    78  				log.Outf("Invalid sample specified: %v. Please select one of the following:\n\n", args)
    79  				printSamples(samples)
    80  				return fmt.Errorf("invalid sample specified: %v", args)
    81  			}
    82  			return nil
    83  		},
    84  	}
    85  	init.Flags().String("dest", ".", `Specify a directory for placing the project files (the default directory is ".")`)
    86  	root.AddCommand(init)
    87  }
    88  
    89  func doInit(cmd *cobra.Command, args []string, proj project.Project) error {
    90  	destination, _ := cmd.Flags().GetString("dest")
    91  	if alreadySetup := proj.AlreadySetup(destination); alreadySetup {
    92  		log.Outf("%s is not empty. Make sure to create an empty directory and run \"gactions init\" from there.", destination)
    93  		return fmt.Errorf("%s is not empty", destination)
    94  	}
    95  	log.Outf("Writing sample files for %v to %s\n", args[0], destination)
    96  	var s project.SampleProject
    97  	for _, v := range samples {
    98  		if v.Name == args[0] {
    99  			s = v
   100  		}
   101  	}
   102  	if err := proj.Download(s, destination); err != nil {
   103  		return err
   104  	}
   105  	log.DoneMsgln("Please checkout the following documentation - https://developers.google.com/assistant/conversational/build on the next steps on how to get started.")
   106  	return nil
   107  }