Skip to content

Commit

Permalink
Convert file to function source code so that it's displayed in dashbo…
Browse files Browse the repository at this point in the history
…ard (#857)
  • Loading branch information
pavius committed Jun 24, 2018
1 parent 5a23960 commit 9ccf77c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
1 change: 1 addition & 0 deletions pkg/common/helper.go
Expand Up @@ -117,6 +117,7 @@ func StripPrefixes(input string, prefixes []string) string {
return input
}

// RemoveEmptyLines removes all empty lines from a string
func RemoveEmptyLines(input string) string {
var nonEmptyLines []string

Expand Down
64 changes: 50 additions & 14 deletions pkg/processor/build/builder.go
Expand Up @@ -152,26 +152,29 @@ func (b *Builder) Build(options *platform.CreateFunctionBuildOptions) (*platform
// before we resolve the path, save it so that we can restore it later
b.originalFunctionConfig.Spec.Build.Path = b.options.FunctionConfig.Spec.Build.Path

if b.options.FunctionConfig.Spec.Build.FunctionSourceCode != "" {

// if user gave function as source code rather than a path - write it to a temporary file
b.options.FunctionConfig.Spec.Build.Path, err = b.writeFunctionSourceCodeToTempFile(b.options.FunctionConfig.Spec.Build.FunctionSourceCode)
if err != nil {
return nil, errors.Wrap(err, "Failed to save function code to temporary file")
}
} else {

// resolve the function path - download in case its a URL
b.options.FunctionConfig.Spec.Build.Path, err = b.resolveFunctionPath(b.options.FunctionConfig.Spec.Build.Path)
if err != nil {
return nil, errors.Wrap(err, "Failed to resolve function path")
}
// resolve the function path - download in case its a URL
b.options.FunctionConfig.Spec.Build.Path, err = b.resolveFunctionPath(b.options.FunctionConfig.Spec.Build.Path)
if err != nil {
return nil, errors.Wrap(err, "Failed to resolve function path")
}

// parse the inline blocks in the file - blocks of comments starting with @nuclio.<something>. this may be used
// later on (e.g. for creating files)
if common.IsFile(b.options.FunctionConfig.Spec.Build.Path) {
var functionSourceCode string

// see if there are any inline blocks in the code
b.parseInlineBlocks() // nolint: errcheck

// try to see if we need to convert the file path -> functionSourceCode
functionSourceCode, err = b.getSourceCodeFromFilePath()
if err != nil {
b.logger.DebugWith("Not populating function source code", "reason", errors.Cause(err))
}

// set into source code
b.logger.DebugWith("Populating functionSourceCode from file path", "contents", functionSourceCode)
b.options.FunctionConfig.Spec.Build.FunctionSourceCode = base64.StdEncoding.EncodeToString([]byte(functionSourceCode))
}

// prepare configuration from both configuration files and things builder infers
Expand Down Expand Up @@ -442,6 +445,17 @@ func (b *Builder) writeFunctionSourceCodeToTempFile(functionSourceCode string) (

func (b *Builder) resolveFunctionPath(functionPath string) (string, error) {

if b.options.FunctionConfig.Spec.Build.FunctionSourceCode != "" {

// if user gave function as source code rather than a path - write it to a temporary file
functionSourceCodeTempPath, err := b.writeFunctionSourceCodeToTempFile(b.options.FunctionConfig.Spec.Build.FunctionSourceCode)
if err != nil {
return "", errors.Wrap(err, "Failed to save function code to temporary file")
}

return functionSourceCodeTempPath, nil
}

// function can either be in the path, received inline or an executable via handler
if b.options.FunctionConfig.Spec.Build.Path == "" &&
b.options.FunctionConfig.Spec.Image == "" {
Expand Down Expand Up @@ -1246,3 +1260,25 @@ func (b *Builder) mergeDirectives(first map[string][]functionconfig.Directive,

return merged
}

func (b *Builder) getSourceCodeFromFilePath() (string, error) {

// if the file path is after resolving function source code, do nothing
if b.options.FunctionConfig.Spec.Build.FunctionSourceCode != "" {
return "", errors.New("Function source code already exists")
}

// if the file path extension is of certain binary types, ignore
if path.Ext(b.options.FunctionConfig.Spec.Build.Path) == ".jar" {
return "", errors.New("Function source code cannot be extracted from this file type")
}

// if user supplied a file containing printable only characters (i.e. not a zip, jar, etc) - copy the contents
// to functionSourceCode so that the dashboard may display it
functionContents, err := ioutil.ReadFile(b.options.FunctionConfig.Spec.Build.Path)
if err != nil {
return "", errors.Wrap(err, "Failed to read file contents to source code")
}

return string(functionContents), nil
}

0 comments on commit 9ccf77c

Please sign in to comment.