Skip to content

Commit

Permalink
Update to v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaikech committed Apr 30, 2017
1 parent 28080dd commit eced447
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 14 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ggsrun
- Upload Files
- Show File List
- Search Files
- Update Project
- [Applications](#Applications)
- For Sublime Text
- For CoffeeScript
Expand Down Expand Up @@ -980,6 +981,18 @@ $ ggsrun ls -si [file id] -j

Result includes file name, file id, modified time and URL.

<a name="Update_Project"></a>
## 10. Update Project
It updates exisiting project on Google Drive.

**Run :**

~~~bash
$ ggsrun ud -p [Project ID on Google Drive] -f [script .gs, .gas, .htm, .html]
~~~

If it is not used ``-p``, the project ID is used the script ID in "ggsrun.cfg". When a script for updating is the same to a script name in the project, it is overwritten. Other scripts in the project is not changed. So this can be also used for updating a script in the project.

---
<a name="Applications"></a>
# Applications
Expand Down Expand Up @@ -1450,6 +1463,13 @@ If you have any questions and commissions for me, feel free to tell me using e-m

Initial release.

* v1.1.0 (April 28, 2017)

1. Added a command for updating exisiting project on Google Drive. The detail infomration is [here](#Update_Project).
2. Added "TotalElapsedTime" for Show File List and Search Files.
3. Some modifications.


## Server
* v1.0.0 (April 24, 2017)

Expand Down
27 changes: 26 additions & 1 deletion ggsrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
app.Author = "tanaike [ https://github.com/tanaikech/ggsrun ] "
app.Email = "tanaike@hotmail.com"
app.Usage = "Executes Google Apps Script (GAS) on Google and Feeds Back Results."
app.Version = "1.0.0"
app.Version = "1.1.0"
app.Commands = []cli.Command{
{
Name: "exe1",
Expand Down Expand Up @@ -198,6 +198,31 @@ func main() {
},
},
},
{
Name: "updateproject",
Aliases: []string{"ud"},
Usage: "Updates project on Google Drive.",
Description: "In this mode, an access token is required.",
Action: updateProject,
Flags: []cli.Flag{
cli.StringFlag{
Name: "filename, f",
Usage: "File name. It's source files for updating.",
},
cli.StringFlag{
Name: "projectid, p",
Usage: "ID of existing project. It's a destination project for updating.",
},
cli.BoolFlag{
Name: "backup, b",
Usage: "Backup project with project ID you set as a file.",
},
cli.BoolFlag{
Name: "jsonparser, j",
Usage: "Display results by JSON parser",
},
},
},
{
Name: "filelist",
Aliases: []string{"ls"},
Expand Down
15 changes: 13 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func downloadFiles(c *cli.Context) error {
return nil
}

// uploadFiles :
// uploadFiles : Uploads files
func uploadFiles(c *cli.Context) error {
res := defAuthContainer(c).
ggsrunIni(c).
Expand All @@ -69,7 +69,18 @@ func uploadFiles(c *cli.Context) error {
return nil
}

// showFileList :
// updateProject : Updates projects and scripts
func updateProject(c *cli.Context) error {
res := defAuthContainer(c).
ggsrunIni(c).
goauth().
defExecutionContainer().
projectUpdateControl(c)
dispTransferResult(c, res)
return nil
}

// showFileList : Shows file list on Google Drive
func showFileList(c *cli.Context) error {
res := defAuthContainer(c).
ggsrunIni(c).
Expand Down
37 changes: 30 additions & 7 deletions materials.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"math"
"path/filepath"
"regexp"
"time"
Expand Down Expand Up @@ -180,6 +181,10 @@ type ProjectUpdaterMeta struct {
MimeType string `json:"mimeType"`
}

type updateProjectFiles struct {
UpFiles []string
}

// ByteSliceFile : File with byte slice
type ByteSliceFile struct {
FileData []int `json:"result"`
Expand Down Expand Up @@ -215,13 +220,14 @@ type AuthContainer struct {
// 1. Upload script using Execution API
// 2. Update project using Drive API and execute script using Execution API
type ExecutionContainer struct {
*InitVal // Initial values
*ResMsg // Response message
*GgsrunCfg // Config for ggsrun
*Param // Payload for Execution API
*FeedBackData // Feedbacked data from function using Execution API
*Project // Project for uploading using Drive API
*DlFileByScript // Information of download file by script
*InitVal // Initial values
*ResMsg // Response message
*GgsrunCfg // Config for ggsrun
*Param // Payload for Execution API
*FeedBackData // Feedbacked data from function using Execution API
*Project // Project for uploading using Drive API
*DlFileByScript // Information of download file by script
*updateProjectFiles // Files for updating Project
}

// DefAuthContainer : Struct container for authorization
Expand Down Expand Up @@ -273,6 +279,7 @@ func (a *AuthContainer) defExecutionContainer() *ExecutionContainer {
&FeedBackData{},
&Project{},
&DlFileByScript{},
&updateProjectFiles{},
}
e.GgsrunCfg = a.GgsrunCfg
e.InitVal = a.InitVal
Expand All @@ -292,6 +299,7 @@ func defExecutionContainerWebApps() *ExecutionContainer {
&FeedBackData{},
&Project{},
&DlFileByScript{},
&updateProjectFiles{},
}
e.InitVal.pstart = time.Now()
e.InitVal.workdir, err = filepath.Abs(".")
Expand Down Expand Up @@ -339,3 +347,18 @@ func (e *ExecutionContainer) defDownloadByScriptContainer() *utl.FileInf {
}
return p
}

// defUpdateProjectContainer : Struct container for downloading files by GAS
func (e *ExecutionContainer) defUpdateProjectContainer(c *cli.Context) *ExecutionContainer {
e.UpFiles = regexp.MustCompile(`\s*,\s*`).Split(c.String("filename"), -1)
return e
}

// dispUpdateProjectContainer : Struct container for downloading files by GAS
func (e *ExecutionContainer) dispUpdateProjectContainer() *utl.FileInf {
p := &utl.FileInf{
Msgar: e.Msg,
TotalEt: math.Trunc(time.Now().Sub(e.InitVal.pstart).Seconds()*1000) / 1000,
}
return p
}
68 changes: 68 additions & 0 deletions projectupdater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Package main (projectupdater.go) :
// These methods are for updating project.
package main

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/tanaikech/ggsrun/utl"
"github.com/urfave/cli"
)

// projectUpdateControl : Main method for updating project.
func (e *ExecutionContainer) projectUpdateControl(c *cli.Context) *utl.FileInf {
if len(c.String("filename")) == 0 {
fmt.Fprintf(os.Stderr, "Error: No Files. Please set them using '-f [ File name ]'. ")
os.Exit(1)
}
if len(c.String("projectid")) > 0 {
e.GgsrunCfg.Scriptid = c.String("projectid")
}
return e.defUpdateProjectContainer(c).
projectBackup(c).
ProjectMaker().
projectUpdate().
dispUpdateProjectContainer()
}

// ProjectMaker : Recreates the project using uploaded scripts.
func (e *ExecutionContainer) ProjectMaker() *ExecutionContainer {
for _, elm := range e.UpFiles {
if filepath.Ext(elm) == ".gs" ||
filepath.Ext(elm) == ".gas" ||
filepath.Ext(elm) == ".js" ||
filepath.Ext(elm) == ".htm" ||
filepath.Ext(elm) == ".html" {
filedata := &File{
Name: strings.Replace(filepath.Base(elm), filepath.Ext(elm), "", -1),
Type: func(ex string) string {
var scripttype string
switch ex {
case ".gs", ".gas", ".js":
scripttype = "server_js"
case ".htm", ".html":
scripttype = "html"
}
return scripttype
}(filepath.Ext(elm)),
Source: utl.ConvGasToUpload(elm),
}
var overwrite bool
for i, v := range e.Project.Files {
if v.Name == filedata.Name {
e.Project.Files[i].Source = filedata.Source
e.Msg = append(e.Msg, fmt.Sprintf("Script '%s' in project was overwritten.", v.Name))
overwrite = true
}
}
if !overwrite {
e.Project.Files = append(e.Project.Files, *filedata)
}
}
}
e.Msg = append(e.Msg, fmt.Sprintf("Project ID '%s' was uploaded.", e.Scriptid))
return e
}
12 changes: 8 additions & 4 deletions sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
// Exe1Function :
func (e *ExecutionContainer) exe1Function(c *cli.Context) *ExecutionContainer {
if len(c.String("scriptfile")) > 0 || c.Bool("backup") {
return e.projectBackup(c).projectUpdate(utl.ConvGasToPut(c))
return e.projectBackup(c).projectUpdateIni(utl.ConvGasToPut(c)).projectUpdate()
}
return e
}
Expand Down Expand Up @@ -234,8 +234,7 @@ func (e *ExecutionContainer) esenderForExe2(c *cli.Context) *ExecutionContainer
return e
}

// ProjectUpdate :
func (e *ExecutionContainer) projectUpdate(sendscript string) *ExecutionContainer {
func (e *ExecutionContainer) projectUpdateIni(sendscript string) *ExecutionContainer {
var overwrite bool
for i := range e.Project.Files {
if e.Project.Files[i].Name == defprojectname {
Expand All @@ -251,6 +250,11 @@ func (e *ExecutionContainer) projectUpdate(sendscript string) *ExecutionContaine
}
e.Project.Files = append(e.Project.Files, *filedata)
}
return e
}

// ProjectUpdate :
func (e *ExecutionContainer) projectUpdate() *ExecutionContainer {
script, _ := json.Marshal(e.Project)
metadata, _ := json.Marshal(&ProjectUpdaterMeta{MimeType: "application/vnd.google-apps.script"})
tokenparams := url.Values{}
Expand Down Expand Up @@ -308,7 +312,7 @@ func (e *ExecutionContainer) projectBackup(c *cli.Context) *ExecutionContainer {
}
res, err := r.FetchAPI()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v. Please check script ID or access token. ", err)
fmt.Fprintf(os.Stderr, "Error: %v. Please check project ID. Inputted project ID is '%s'.\n", err, e.Scriptid)
os.Exit(1)
}
json.Unmarshal(res, &e.Project)
Expand Down
3 changes: 3 additions & 0 deletions utl/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ func (p *FileInf) GetFileList(c *cli.Context) *FileInf {
fmt.Fprintf(os.Stderr, "Error: File name '%s' is not found. ", p.SearchByName)
os.Exit(1)
}
p.TotalEt = math.Trunc(time.Now().Sub(p.PstartTime).Seconds()*1000) / 1000
return p
}
if len(c.String("searchbyid")) > 0 {
Expand All @@ -567,6 +568,7 @@ func (p *FileInf) GetFileList(c *cli.Context) *FileInf {
os.Exit(1)
}
json.Unmarshal(body, &p)
p.TotalEt = math.Trunc(time.Now().Sub(p.PstartTime).Seconds()*1000) / 1000
return p
}
var fm fileListSt
Expand Down Expand Up @@ -614,6 +616,7 @@ func (p *FileInf) GetFileList(c *cli.Context) *FileInf {
btok, _ := json.MarshalIndent(fm, "", "\t")
ioutil.WriteFile(filename, btok, 0777)
}
p.TotalEt = math.Trunc(time.Now().Sub(p.PstartTime).Seconds()*1000) / 1000
return p
}

Expand Down

0 comments on commit eced447

Please sign in to comment.