Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update with pubsub / bucket * Make logging work * Fix logging * Update with working CLI * Fix build command
- Loading branch information
1 parent
33b3c2e
commit 92cf21e
Showing
1,085 changed files
with
2,165,552 additions
and
4,671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,2 @@ | ||
target | ||
.DS_Store | ||
tool/ | ||
client/ | ||
swagger/ | ||
app/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
t "github.com/MartinSahlen/go-cloud-fn/template" | ||
uuid "github.com/satori/go.uuid" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// deployCmd represents the deploy command | ||
var deployCmd = &cobra.Command{ | ||
Use: "deploy <function-name>", | ||
Short: "Deploy your cloud function", | ||
Long: `This command lets you deploy your function with a given | ||
set of parameters.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
log.Println("You need to provide the function name as first argument.") | ||
return | ||
} | ||
if !emulator && stageBucket == "" { | ||
log.Println("Production environment needs stage-bucket") | ||
return | ||
} | ||
if !triggerHTTP && triggerBucket == "" && triggerTopic == "" { | ||
log.Println("You need to set either trigger-http, trigger-bucket or trigger-topic") | ||
return | ||
} | ||
functionName := args[0] | ||
targetDir := "target/" + uuid.NewV4().String() + "/" | ||
index, err := t.GenerateIndex(t.IndexTemplateData{ | ||
FunctionName: functionName, | ||
TargetDir: targetDir, | ||
TriggerHTTP: triggerHTTP, | ||
}) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
err = os.MkdirAll(targetDir, os.ModePerm) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
err = ioutil.WriteFile(targetDir+"index.js", []byte(index), os.ModePerm) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
//Set the correct trigger (only one) | ||
trigger := []string{} | ||
if triggerHTTP { | ||
trigger = append(trigger, "--trigger-http") | ||
} else if triggerBucket != "" { | ||
trigger = append(trigger, "--trigger-bucket") | ||
trigger = append(trigger, triggerBucket) | ||
} else if triggerTopic != "" { | ||
trigger = append(trigger, "--trigger-topic") | ||
trigger = append(trigger, triggerTopic) | ||
} | ||
|
||
deployArguments := []string{ | ||
"gcloud beta functions deploy", | ||
functionName, | ||
"--local-path", targetDir, | ||
"--timeout", timeout, | ||
} | ||
//Use functions if it's emulator we're deploying to. | ||
var buildCmd string | ||
if emulator { | ||
deployArguments[0] = "functions deploy" | ||
buildCmd = "go build -o " | ||
} else { | ||
buildCmd = "GOOS=linux go build -o " | ||
deployArguments = append( | ||
deployArguments, | ||
"--memory", memory, | ||
"--stage-bucket", stageBucket) | ||
} | ||
|
||
compile, err := exec.Command("sh", "-c", buildCmd+targetDir+functionName).CombinedOutput() | ||
if err != nil { | ||
log.Println(compile) | ||
return | ||
} | ||
deploy, _ := exec.Command("sh", "-c", strings.Join(append(deployArguments, trigger...), " ")).CombinedOutput() | ||
log.Println(string(deploy)) | ||
}, | ||
} | ||
|
||
var emulator bool | ||
var stageBucket string | ||
var triggerHTTP bool | ||
var triggerBucket string | ||
var triggerTopic string | ||
var memory string | ||
var timeout string | ||
|
||
func init() { | ||
RootCmd.AddCommand(deployCmd) | ||
deployCmd.Flags().BoolVarP(&emulator, "emulator", "e", false, "Deploy to emulator") | ||
deployCmd.Flags().StringVarP(&stageBucket, "stage-bucket", "s", "", "Set GCS bucket to upload zip bundle") | ||
deployCmd.Flags().BoolVarP(&triggerHTTP, "trigger-http", "j", false, "Set function to trigger on HTTP event") | ||
deployCmd.Flags().StringVarP(&triggerBucket, "trigger-bucket", "b", "", "Set function to trigger on this GCS bucket event") | ||
deployCmd.Flags().StringVarP(&triggerTopic, "trigger-topic", "t", "", "Set function to trigger on this Pubsub topic") | ||
deployCmd.Flags().StringVarP(&memory, "memory", "m", "1024MB", "Set function memory [MAX 2048MB]") | ||
deployCmd.Flags().StringVarP(&timeout, "timeout", "o", "540s", "Set function timeout [MAX 540s]") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// RootCmd represents the base command when called without any subcommands | ||
var RootCmd = &cobra.Command{ | ||
Use: "go-cloud-fn", | ||
Short: "Write google cloud functions in go", | ||
Long: `go-cloud-fn lets you write and deploy google cloud functions in go.`, | ||
} | ||
|
||
// Execute adds all child commands to the root command sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := RootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(-1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,21 @@ | ||
package main | ||
// Copyright © 2017 NAME HERE <EMAIL ADDRESS> | ||
// | ||
// Licensed 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. | ||
|
||
import ( | ||
"log" | ||
"os/exec" | ||
package main | ||
|
||
docopt "github.com/docopt/docopt-go" | ||
) | ||
import "github.com/MartinSahlen/go-cloud-fn/cmd" | ||
|
||
func main() { | ||
usage := `go-cloud-fn. | ||
Usage: | ||
go-cloud-fn build <function-name> | ||
go-cloud-fn build <function-name> --production | ||
go-cloud-fn build <function-name> --development | ||
go-cloud-fn deploy <function-name> <stage-bucket> | ||
Options: | ||
-h --help Show this screen. | ||
-p --production Deploy for production | ||
-d --development Deploy for local development | ||
--version Show version.` | ||
|
||
arguments, err := docopt.Parse(usage, nil, true, "go-cloud-fn 0.0 Pre-Alpha", false) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
functionName := arguments["<function-name>"].(string) | ||
|
||
//build := arguments["build"].(bool) | ||
//build := arguments["deploy"].(bool) | ||
|
||
conf := config{ | ||
FunctionName: functionName, | ||
TargetDir: "target", | ||
TriggerHTTP: true, | ||
StageBucket: "your-bucket", | ||
ExecutableName: functionName, | ||
Production: true, | ||
} | ||
|
||
buildOut, err := parseTemplate(buildSh, conf) | ||
if err != nil { | ||
panic(err) | ||
} /* | ||
httpOut, err := parseTemplate(httpJs, conf) | ||
if err != nil { | ||
panic(err) | ||
} | ||
deployOut, err := parseTemplate(deploySh, conf) | ||
if err != nil { | ||
panic(err) | ||
}*/ | ||
log.Println(buildOut) | ||
out, err := exec.Command(buildOut).Output() | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Println(out) | ||
/* | ||
err = ioutil.WriteFile("build.sh", []byte(buildOut), os.ModePerm) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = ioutil.WriteFile(conf.TargetDir+"/index.js", []byte(httpOut), os.ModePerm) | ||
if err != nil { | ||
panic(err) | ||
}*/ | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,26 @@ | ||
package shim | ||
|
||
func HandleBucketEvent() {} | ||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
storage "google.golang.org/api/storage/v1beta2" | ||
) | ||
|
||
type BucketHandlerFunc func(object storage.Object) | ||
|
||
func HandleBucketEvent(h BucketHandlerFunc) { | ||
stdin, err := ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var object storage.Object | ||
err = json.Unmarshal(stdin, &object) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
h(object) | ||
} |
Oops, something went wrong.