Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
支持环境变量以及升级 fc-go-sdk 依赖以支持 MNS TriggerConfig (#49)
Browse files Browse the repository at this point in the history
* support environment variables

* 更新 fc-go-sdk 依赖包信息以支持 MNS TriggerConfig

* 支持 --env-file

* 优化 help 提示
  • Loading branch information
ChanDaoH authored Mar 31, 2019
1 parent 36581e3 commit f1e84ce
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 52 deletions.
58 changes: 43 additions & 15 deletions cmd/create_function.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
package cmd

import (
"github.com/aliyun/fc-go-sdk"

"fmt"
"github.com/aliyun/fc-go-sdk"
"io/ioutil"
"strings"

"github.com/spf13/cobra"

"github.com/aliyun/fcli/util"
)

type createFuncInputType struct {
serviceName string
functionName string
description string
runtime string
handler string
initializer string
codeDir string
codeFile string
codeOSSBucket string
codeOSSObject string
memory int32
timeout int32
initializationTimeout int32
serviceName string
functionName string
description string
runtime string
handler string
initializer string
codeDir string
codeFile string
codeOSSBucket string
codeOSSObject string
memory int32
timeout int32
initializationTimeout int32
environmentVariables []string
environmentConfigFiles []string
}

// Use a unique name to avoid global variable confliction.
Expand Down Expand Up @@ -53,6 +55,8 @@ func init() {
&createFuncInput.handler, "handler", "h", "", "handler is the entrypoint for the function execution")
createFuncCmd.Flags().StringVarP(
&createFuncInput.initializer, "initializer", "i", "", "initializer is the entrypoint for the initializer execution")
createFuncCmd.Flags().StringArrayVar(&createFuncInput.environmentVariables, "env", []string{}, "set environment variables. e.g. --env VAR1=val1 --env VAR2=val2")
createFuncCmd.Flags().StringArrayVar(&createFuncInput.environmentConfigFiles, "env-file", []string{}, "read in a file of environment variables. e.g. --env-file FILE1 --env-file FILE2")
}

var createFuncCmd = &cobra.Command{
Expand All @@ -70,6 +74,30 @@ var createFuncCmd = &cobra.Command{
WithHandler(createFuncInput.handler).
WithInitializer(createFuncInput.initializer).
WithRuntime(createFuncInput.runtime)

envMap := make(map[string]string)

if cmd.Flags().Changed("env-file") {
for _, envFilePath := range createFuncInput.environmentConfigFiles {
_, err := util.GetEnvSetting(envMap, envFilePath)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
}
input.WithEnvironmentVariables(envMap)
}

if cmd.Flags().Changed("env") {
for _, envVar := range createFuncInput.environmentVariables {
config := strings.Split(envVar, "=")
if len(config) == 2 {
envMap[config[0]] = config[1]
}
}
input.WithEnvironmentVariables(envMap)
}

if createFuncInput.codeFile != "" {
var data []byte
data, err := ioutil.ReadFile(createFuncInput.codeFile)
Expand Down
25 changes: 24 additions & 1 deletion cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ var shellCmd = &cobra.Command{
handler := flags.StringP("handler", "h", "", "handler is the entrypoint for the function execution")
initializer := flags.StringP("initializer", "i", "", "initializer is the entrypoint for the initializer execution")
etag := flags.String("etag", "", "function etag for update")
environmentVariables := flags.StringArray("env", []string{}, "set environment variables. e.g. --env VAR1=val1 --env VAR2=val2")
environmentConfigFiles := flags.StringArray("env-file", []string{}, "read in a file of environment variables. e.g. --env-file FILE1 --env-file FILE2")

err := flags.Parse(args)
if err != nil {
return err
Expand All @@ -365,6 +368,22 @@ var shellCmd = &cobra.Command{
serviceName := resrcList[2]
functionName := resrcList[3]

envMap := make(map[string]string)

for _, envFilePath := range *environmentConfigFiles {
_, err := util.GetEnvSetting(envMap, envFilePath)
if err != nil {
return err
}
}

for _, envVar := range *environmentVariables {
config := strings.Split(envVar, "=")
if len(config) == 2 {
envMap[config[0]] = config[1]
}
}

if op == "CreateFunction" {
input := fc.NewCreateFunctionInput(serviceName).
WithFunctionName(functionName).
Expand All @@ -374,7 +393,8 @@ var shellCmd = &cobra.Command{
WithInitializationTimeout(*initializationTimeout).
WithHandler(*handler).
WithInitializer(*initializer).
WithRuntime(*runtime)
WithRuntime(*runtime).
WithEnvironmentVariables(envMap)
if *codeFile != "" {
var data []byte
data, err = ioutil.ReadFile(*codeFile)
Expand All @@ -395,6 +415,9 @@ var shellCmd = &cobra.Command{
_, err = client.CreateFunction(input)
} else {
input := fc.NewUpdateFunctionInput(serviceName, functionName)
if flags.Changed("env") || flags.Changed("env-file") {
input.WithEnvironmentVariables(envMap)
}
if flags.Changed("description") {
input.WithDescription(*desc)
}
Expand Down
60 changes: 44 additions & 16 deletions cmd/update_function.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cmd

import (
"io/ioutil"

"github.com/aliyun/fc-go-sdk"
"io/ioutil"
"strings"

"fmt"

Expand All @@ -13,20 +13,22 @@ import (
)

type updateFuncInputType struct {
serviceName *string
functionName *string
description *string
runtime *string
handler *string
initializer *string
codeDir *string
codeFile *string
codeOSSBucket *string
codeOSSObject *string
memory *int32
timeout *int32
initializationTimeout *int32
etag *string
serviceName *string
functionName *string
description *string
runtime *string
handler *string
initializer *string
codeDir *string
codeFile *string
codeOSSBucket *string
codeOSSObject *string
memory *int32
timeout *int32
initializationTimeout *int32
etag *string
environmentVariables *[]string
environmentConfigFiles *[]string
}

var updateFuncInput updateFuncInputType
Expand Down Expand Up @@ -55,6 +57,8 @@ func init() {
updateFuncInput.etag = updateFuncCmd.Flags().String(
"etag", "", "provide etag to do the conditional update. "+
"If the specified etag does not match the function's, the update will fail.")
updateFuncInput.environmentVariables = updateFuncCmd.Flags().StringArray("env", []string{}, "set environment variables. e.g. --env VAR1=val1 --env VAR2=val2")
updateFuncInput.environmentConfigFiles = updateFuncCmd.Flags().StringArray("env-file", []string{}, "read in a file of environment variables. e.g. --env-file FILE1 --env-file FILE2")
}

var updateFuncCmd = &cobra.Command{
Expand All @@ -64,6 +68,30 @@ var updateFuncCmd = &cobra.Command{
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
input := fc.NewUpdateFunctionInput(*updateFuncInput.serviceName, *updateFuncInput.functionName)

envMap := make(map[string]string)

if cmd.Flags().Changed("env-file") {
for _, envFilePath := range *updateFuncInput.environmentConfigFiles {
_, err := util.GetEnvSetting(envMap, envFilePath)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
}
input.WithEnvironmentVariables(envMap)
}

if cmd.Flags().Changed("env") {
for _, envVar := range *updateFuncInput.environmentVariables {
config := strings.Split(envVar, "=")
if len(config) == 2 {
envMap[config[0]] = config[1]
}
}
input.WithEnvironmentVariables(envMap)
}

if cmd.Flags().Changed("description") {
input.WithDescription(*updateFuncInput.description)
}
Expand Down
42 changes: 22 additions & 20 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"bufio"
"encoding/json"
"fmt"
"github.com/aliyun/fcli/version"
Expand Down Expand Up @@ -486,3 +487,25 @@ func GetLocalImageDigest(name, tag string) (string, error) {
digest = digest[strings.Index(digest, "@")+1:]
return digest, nil
}

// GetEnvSetting Get env setting from filePath
func GetEnvSetting(envMap map[string]string, envFilePath string) (map[string]string, error) {
envFile, err := os.Open(envFilePath)
if err != nil {
return nil, err
}
defer envFile.Close()

sc := bufio.NewScanner(envFile)
for sc.Scan() {
line := sc.Text()
if strings.Contains(line, "#") {
continue
}
config := strings.Split(line, "=")
if len(config) == 2 {
envMap[config[0]] = config[1]
}
}
return envMap, nil
}

0 comments on commit f1e84ce

Please sign in to comment.