[GitHub] kpavel commented on a change in pull request #715: Adding support for export managed project back to manifest

2018-02-09 Thread GitBox
kpavel commented on a change in pull request #715: Adding support for export 
managed project back to manifest
URL: 
https://github.com/apache/incubator-openwhisk-wskdeploy/pull/715#discussion_r167239098
 
 

 ##
 File path: cmd/export.go
 ##
 @@ -0,0 +1,266 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package cmd
+
+import (
+   "errors"
+   "fmt"
+   "os"
+   "path/filepath"
+   "strings"
+
+   "github.com/apache/incubator-openwhisk-client-go/whisk"
+   "github.com/apache/incubator-openwhisk-wskdeploy/deployers"
+   "github.com/apache/incubator-openwhisk-wskdeploy/parsers"
+   "github.com/apache/incubator-openwhisk-wskdeploy/utils"
+   "github.com/apache/incubator-openwhisk-wskdeploy/wskderrors"
+   "github.com/spf13/cobra"
+)
+
+var exportCmd = &cobra.Command{
+   Use:"export",
+   SuggestFor: []string{"capture"},
+   Short:  "Export project assets from OpenWhisk",
+   Long:   `Exports managed project assets from OpenWhisk to manifest 
and function files`,
+   RunE:   ExportCmdImp,
+}
+
+var config *whisk.Config
+
+func ExportAction(actionName string, packageName string, maniyaml 
*parsers.YAML) error {
+
+   pkg := maniyaml.Packages[packageName]
+   if pkg.Actions == nil {
+   pkg.Actions = make(map[string]parsers.Action)
+   maniyaml.Packages[packageName] = pkg
+   }
+
+   wskAction, _, err := client.Actions.Get(actionName)
+   if err != nil {
+   return err
+   }
+   if wskAction.Exec.Kind == "sequence" {
+   seq := new(parsers.Sequence)
+   for _, component := range wskAction.Exec.Components {
+   // must ommit namespace from seq component name
+   ExportAction(strings.SplitN(component, "/", 3)[2], 
packageName, maniyaml)
+   slices := strings.Split(component, "/")
+
+   // save in the seq list only action names
+   if len(seq.Actions) > 0 {
+   seq.Actions += ","
+   }
+
+   seq.Actions += slices[len(slices)-1]
+   }
+
+   pkg = maniyaml.Packages[packageName]
+   if pkg.Sequences == nil {
+   pkg.Sequences = make(map[string]parsers.Sequence)
+   }
+
+   pkg.Sequences[wskAction.Name] = *seq
+   } else {
+   parsedAction := *maniyaml.ComposeParsersAction(*wskAction)
+
+   // get the action file extension according to action kind (e.g. 
js for nodejs)
+   ext := utils.FileRuntimeExtensionsMap[wskAction.Exec.Kind]
+
+   manifestDir := filepath.Dir(utils.Flags.ManifestPath)
+
+   // store function file under action package name subdirectory 
in the specified manifest folder
+   functionDir := filepath.Join(manifestDir, packageName)
+   os.MkdirAll(functionDir, os.ModePerm)
+
+   // store function in manifest under path relative to manifest 
root
+   functionFile := filepath.Join(packageName, wskAction.Name) + 
"." + ext
+   parsedAction.Function = functionFile
+
+   // create function file at the full path
+   functionFile = filepath.Join(manifestDir, functionFile)
+   f, err := os.Create(functionFile)
+   if err != nil {
+   return wskderrors.NewFileReadError(functionFile, 
err.Error())
+   }
+
+   defer f.Close()
+
+   // store action function in the filesystem next to the 
manifest.yml
+   // TODO: consider to name files by namespace + action to make 
function file names uniqueue
+   f.Write([]byte(*wskAction.Exec.Code))
+   pkg.Actions[wskAction.Name] = parsedAction
+   }
+
+   maniyaml.Packages[packageName] = pkg
+   return nil
+}
+
+func ExportCmdImp(cmd *cobra.Command, args []string) error {
+
+   projectName := utils.Flags.ProjectPath
+   maniyaml := &parsers.YAML{}
+   maniyaml.Project.Name = projectName
+
+   config

[GitHub] kpavel commented on a change in pull request #715: Adding support for export managed project back to manifest

2018-02-09 Thread GitBox
kpavel commented on a change in pull request #715: Adding support for export 
managed project back to manifest
URL: 
https://github.com/apache/incubator-openwhisk-wskdeploy/pull/715#discussion_r167239098
 
 

 ##
 File path: cmd/export.go
 ##
 @@ -0,0 +1,266 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package cmd
+
+import (
+   "errors"
+   "fmt"
+   "os"
+   "path/filepath"
+   "strings"
+
+   "github.com/apache/incubator-openwhisk-client-go/whisk"
+   "github.com/apache/incubator-openwhisk-wskdeploy/deployers"
+   "github.com/apache/incubator-openwhisk-wskdeploy/parsers"
+   "github.com/apache/incubator-openwhisk-wskdeploy/utils"
+   "github.com/apache/incubator-openwhisk-wskdeploy/wskderrors"
+   "github.com/spf13/cobra"
+)
+
+var exportCmd = &cobra.Command{
+   Use:"export",
+   SuggestFor: []string{"capture"},
+   Short:  "Export project assets from OpenWhisk",
+   Long:   `Exports managed project assets from OpenWhisk to manifest 
and function files`,
+   RunE:   ExportCmdImp,
+}
+
+var config *whisk.Config
+
+func ExportAction(actionName string, packageName string, maniyaml 
*parsers.YAML) error {
+
+   pkg := maniyaml.Packages[packageName]
+   if pkg.Actions == nil {
+   pkg.Actions = make(map[string]parsers.Action)
+   maniyaml.Packages[packageName] = pkg
+   }
+
+   wskAction, _, err := client.Actions.Get(actionName)
+   if err != nil {
+   return err
+   }
+   if wskAction.Exec.Kind == "sequence" {
+   seq := new(parsers.Sequence)
+   for _, component := range wskAction.Exec.Components {
+   // must ommit namespace from seq component name
+   ExportAction(strings.SplitN(component, "/", 3)[2], 
packageName, maniyaml)
+   slices := strings.Split(component, "/")
+
+   // save in the seq list only action names
+   if len(seq.Actions) > 0 {
+   seq.Actions += ","
+   }
+
+   seq.Actions += slices[len(slices)-1]
+   }
+
+   pkg = maniyaml.Packages[packageName]
+   if pkg.Sequences == nil {
+   pkg.Sequences = make(map[string]parsers.Sequence)
+   }
+
+   pkg.Sequences[wskAction.Name] = *seq
+   } else {
+   parsedAction := *maniyaml.ComposeParsersAction(*wskAction)
+
+   // get the action file extension according to action kind (e.g. 
js for nodejs)
+   ext := utils.FileRuntimeExtensionsMap[wskAction.Exec.Kind]
+
+   manifestDir := filepath.Dir(utils.Flags.ManifestPath)
+
+   // store function file under action package name subdirectory 
in the specified manifest folder
+   functionDir := filepath.Join(manifestDir, packageName)
+   os.MkdirAll(functionDir, os.ModePerm)
+
+   // store function in manifest under path relative to manifest 
root
+   functionFile := filepath.Join(packageName, wskAction.Name) + 
"." + ext
+   parsedAction.Function = functionFile
+
+   // create function file at the full path
+   functionFile = filepath.Join(manifestDir, functionFile)
+   f, err := os.Create(functionFile)
+   if err != nil {
+   return wskderrors.NewFileReadError(functionFile, 
err.Error())
+   }
+
+   defer f.Close()
+
+   // store action function in the filesystem next to the 
manifest.yml
+   // TODO: consider to name files by namespace + action to make 
function file names uniqueue
+   f.Write([]byte(*wskAction.Exec.Code))
+   pkg.Actions[wskAction.Name] = parsedAction
+   }
+
+   maniyaml.Packages[packageName] = pkg
+   return nil
+}
+
+func ExportCmdImp(cmd *cobra.Command, args []string) error {
+
+   projectName := utils.Flags.ProjectPath
+   maniyaml := &parsers.YAML{}
+   maniyaml.Project.Name = projectName
+
+   config