github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/pull/pull.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 pull provides an implementation of "gactions pull" command. 16 package pull 17 18 import ( 19 "context" 20 "fmt" 21 "net/url" 22 "os" 23 24 "github.com/actions-on-google/gactions/api/sdk" 25 "github.com/actions-on-google/gactions/log" 26 "github.com/actions-on-google/gactions/project" 27 "github.com/actions-on-google/gactions/project/studio" 28 "github.com/spf13/cobra" 29 ) 30 31 // exists returns whether the given file or directory exists or not 32 func exists(path string) bool { 33 if _, err := os.Stat(path); err != nil { 34 return os.IsExist(err) 35 } 36 return true 37 } 38 39 // AddCommand adds the push sub-command to the passed in root command. 40 func AddCommand(ctx context.Context, root *cobra.Command, project project.Project) { 41 pull := &cobra.Command{ 42 Use: "pull", 43 Short: "This command pulls files from Actions Console into the local file system.", 44 Long: "This command pulls files from Actions Console into the local file system.", 45 RunE: func(cmd *cobra.Command, args []string) error { 46 studioProj, ok := project.(studio.Studio) 47 if !ok { 48 return fmt.Errorf("can not convert %T to %T", project, studio.Studio{}) 49 } 50 // Developer may run pull from an empty directory, in which case projectRoot doesn't yet 51 // exist. In that case, os.Getwd() would be used. 52 if studioProj.ProjectRoot() == "" { 53 if err := (&studioProj).SetProjectRoot(); err != nil { 54 return err 55 } 56 } 57 // RC file will have a faulty path -- try to create it. 58 if !exists(studioProj.ProjectRoot()) { 59 log.Infof("%q doesn't exist.", studioProj.ProjectRoot()) 60 // 0750 sets permissions so that, (U)ser / owner can read, 61 // can write and can execute. (G)roup can read, can't write and can execute. 62 // (O)thers can't read, can't write and can't execute. 63 if err := os.MkdirAll(studioProj.ProjectRoot(), 0750); err != nil { 64 return err 65 } 66 log.Infof("Created %q", studioProj.ProjectRoot()) 67 } 68 pid, err := cmd.Flags().GetString("project-id") 69 if err != nil { 70 return err 71 } 72 if err := (&studioProj).SetProjectID(pid); err != nil { 73 return err 74 } 75 force, err := cmd.Flags().GetBool("force") 76 if err != nil { 77 return err 78 } 79 clean, err := cmd.Flags().GetBool("clean") 80 if err != nil { 81 return err 82 } 83 versionID, err := cmd.Flags().GetString("version-id") 84 if err != nil { 85 return err 86 } 87 if versionID == "" { 88 if err := sdk.ReadDraftJSON(ctx, studioProj, force, clean); err != nil { 89 return err 90 } 91 } else { 92 versionID = url.PathEscape(versionID) 93 if err := sdk.ReadVersionJSON(ctx, studioProj, force, clean, versionID); err != nil { 94 return err 95 } 96 } 97 log.DoneMsgln(fmt.Sprintf("You should see the files written in %s", studioProj.ProjectRoot())) 98 return nil 99 }, 100 Args: cobra.NoArgs, 101 } 102 pull.Flags().String("project-id", "", "Pull from the project specified by the ID. The value provided in this flag will overwrite the value from settings file, if present.") 103 pull.Flags().BoolP("force", "f", false, "Overwrite existing local files without asking.") 104 pull.Flags().Bool("clean", false, "Remove any local files that are not in the files pulled from Actions Builder.") 105 pull.Flags().String("version-id", "", "Pull the version specified by the ID.") 106 root.AddCommand(pull) 107 }