summaryrefslogtreecommitdiffstats
path: root/client/go
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2021-08-17 20:40:58 +0200
committerJon Bratseth <bratseth@gmail.com>2021-08-17 20:40:58 +0200
commit6474b8a196e1feaa8b0450231b5a1083be84213d (patch)
tree086c9554b76169dc909ff848c79f4da3d0253dca /client/go
parentc909f7ddba9233be56a1a82f5a50b701351fc0d1 (diff)
init command WIP
Diffstat (limited to 'client/go')
-rw-r--r--client/go/src/github.com/vespa-engine/vespa/cmd/init.go80
-rw-r--r--client/go/src/github.com/vespa-engine/vespa/go.mod1
-rw-r--r--client/go/src/github.com/vespa-engine/vespa/utils/http.go17
3 files changed, 96 insertions, 2 deletions
diff --git a/client/go/src/github.com/vespa-engine/vespa/cmd/init.go b/client/go/src/github.com/vespa-engine/vespa/cmd/init.go
new file mode 100644
index 00000000000..114012b614f
--- /dev/null
+++ b/client/go/src/github.com/vespa-engine/vespa/cmd/init.go
@@ -0,0 +1,80 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+// vespa init command
+// author: bratseth
+
+package cmd
+
+import (
+ "errors"
+ "gopkg.in/src-d/go-git.v4"
+ "github.com/spf13/cobra"
+ "github.com/vespa-engine/vespa/utils"
+ "io"
+ "net/http"
+ "net/url"
+ "os"
+)
+
+func init() {
+ rootCmd.AddCommand(initCmd)
+}
+
+var initCmd = &cobra.Command{
+ Use: "init applicationName source",
+ Short: "Creates the files and directory structure for a new Vespa application",
+ Long: `TODO`,
+ Args: func(cmd *cobra.Command, args []string) error {
+ if len(args) != 2 {
+ // TODO: Support creating an "empty" application by not specifying a source
+ return errors.New("vespa init requires a project name and source")
+ }
+ return nil
+ },
+ Run: func(cmd *cobra.Command, args []string) {
+ initApplication(args[0], args[1])
+ },
+}
+
+func initApplication(name string, source string) {
+ // TODO: Support third-party full github url sources
+ //err := os.Mkdir(name, 0755)
+ //if err != nil {
+ // utils.Error("Could not create directory '" + name + "'")
+ // utils.Detail(err.Error())
+ //}
+ zipUrl, _ := url.Parse("https://github.com/vespa-engine/sample-apps/archive/refs/heads/master.zip")
+ request := &http.Request{
+ URL: zipUrl,
+ Method: "GET",
+ }
+ response := utils.HttpDoWithoutReadingData(request, "GitHub")
+ if response.StatusCode != 200 {
+ utils.Error("Could not download sample apps from github")
+ utils.Detail(response.Status)
+ }
+ defer response.Body.Close()
+
+ destination, _ := os.Create("./" + name + "/sample-apps.zip") // TODO: Path
+ defer destination.Close()
+ _, err := io.Copy(destination, response.Body)
+ if err != nil {
+ utils.Error("Could not download sample apps from GitHub")
+ utils.Detail(err.Error())
+ return
+ }
+ utils.Success("Downloaded zip, possibly")
+}
+
+func gitStuff(name string, source string) {
+ _, err := git.PlainClone("./" + name, false, &git.CloneOptions{ // TODO: Path
+ URL: "https://github.com/vespa-engine/sample-apps",
+ Progress: os.Stdout,
+ Depth: 1,
+ })
+ if err != nil {
+ utils.Error("Could not clone repo in '" + source + "'")
+ utils.Detail(err.Error())
+ } else {
+ utils.Success("Initialized to " + name)
+ }
+}
diff --git a/client/go/src/github.com/vespa-engine/vespa/go.mod b/client/go/src/github.com/vespa-engine/vespa/go.mod
index 64a8b723b9f..dc36ba5a45b 100644
--- a/client/go/src/github.com/vespa-engine/vespa/go.mod
+++ b/client/go/src/github.com/vespa-engine/vespa/go.mod
@@ -6,4 +6,5 @@ require (
github.com/spf13/cobra v1.2.1 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
+ gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
)
diff --git a/client/go/src/github.com/vespa-engine/vespa/utils/http.go b/client/go/src/github.com/vespa-engine/vespa/utils/http.go
index a4f474ea7cb..d3c415386b9 100644
--- a/client/go/src/github.com/vespa-engine/vespa/utils/http.go
+++ b/client/go/src/github.com/vespa-engine/vespa/utils/http.go
@@ -12,7 +12,7 @@ import (
"time"
)
-// Set this to a mock HttpClient to unit test HTTP requests
+// Set this to a mock HttpClient instead to unit test HTTP requests
var ActiveHttpClient = CreateClient()
type HttpClient interface {
@@ -20,6 +20,7 @@ type HttpClient interface {
}
type defaultHttpClient struct {
+ timeout time.Duration // The timeout set on this client
client http.Client
}
@@ -27,8 +28,9 @@ func (c defaultHttpClient) Do(request *http.Request) (response *http.Response, e
return c.client.Do(request)
}
-func CreateClient() (client HttpClient) {
+func CreateClient() HttpClient {
return &defaultHttpClient{
+ timeout: time.Second * 10,
client: http.Client{Timeout: time.Second * 10,},
}
}
@@ -62,3 +64,14 @@ func HttpDo(request *http.Request, description string) (response *http.Response)
return response
}
}
+
+// TODO: Always use this and rename to HttpDo
+func HttpDoWithoutReadingData(request *http.Request, description string) (response *http.Response) {
+ response, error := ActiveHttpClient.Do(request)
+ if error != nil {
+ Error("Could not connect to", strings.ToLower(description), "at", request.URL.Host)
+ Detail(error.Error())
+ }
+ return response
+}
+