You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2018/10/04 13:19:54 UTC

[camel-k] 01/01: Add option to run from URL

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch url-support
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit b3eccd05e29aca691661dbb6a7373c30d66f52a5
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Thu Oct 4 15:17:53 2018 +0200

    Add option to run from URL
---
 pkg/client/cmd/run.go | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go
index b9f40eb..3215543 100644
--- a/pkg/client/cmd/run.go
+++ b/pkg/client/cmd/run.go
@@ -21,6 +21,7 @@ import (
 	"errors"
 	"fmt"
 	"io/ioutil"
+	"net/http"
 	"os"
 	"strconv"
 	"strings"
@@ -97,10 +98,19 @@ func (*runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
 		return errors.New("accepts 1 arg, received " + strconv.Itoa(len(args)))
 	}
 	fileName := args[0]
-	if _, err := os.Stat(fileName); err != nil && os.IsNotExist(err) {
-		return errors.New("file " + fileName + " does not exist")
-	} else if err != nil {
-		return errors.New("error while accessing file " + fileName)
+	if !strings.HasPrefix(fileName, "http://") && !strings.HasPrefix(fileName, "https://") {
+		if _, err := os.Stat(fileName); err != nil && os.IsNotExist(err) {
+			return errors.New("file " + fileName + " does not exist")
+		} else if err != nil {
+			return errors.New("error while accessing file " + fileName)
+		}
+	} else {
+		resp, err := http.Get(fileName)
+		if err != nil {
+			return errors.New("The URL provided is not reachable " + fileName)
+		} else if resp.StatusCode != 200 {
+			return errors.New("The URL provided is not reachable " + fileName)
+		}
 	}
 	return nil
 }
@@ -334,10 +344,20 @@ func (o *runCmdOptions) updateIntegrationCode(filename string) (*v1alpha1.Integr
 }
 
 func (*runCmdOptions) loadCode(fileName string) (string, error) {
-	content, err := ioutil.ReadFile(fileName)
+	if !strings.HasPrefix(fileName, "http://") && !strings.HasPrefix(fileName, "https://") {
+		content, err := ioutil.ReadFile(fileName)
+		if err != nil {
+			return "", err
+		}
+		return string(content), nil
+	}
+
+	resp, err := http.Get(fileName)
 	if err != nil {
 		return "", err
 	}
-	// TODO check encoding issues
-	return string(content), err
+	defer resp.Body.Close()
+	bodyBytes, err := ioutil.ReadAll(resp.Body)
+	bodyString := string(bodyBytes)
+	return string(bodyString), err
 }