diff options
author | Jon Bratseth <bratseth@gmail.com> | 2021-08-25 09:48:07 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2021-08-25 09:48:07 +0200 |
commit | f335be06085e8f8105246fd84a5e568c4e193c15 (patch) | |
tree | c4e2075f8f0126b2a1e089066443ec9f1b440d40 /client | |
parent | c43e37dcfb49d50a2b4109e4f8ac9a1d3ae0609e (diff) |
go fmt
Diffstat (limited to 'client')
-rw-r--r-- | client/go/cmd/activate.go | 18 | ||||
-rw-r--r-- | client/go/cmd/command_tester.go | 56 | ||||
-rw-r--r-- | client/go/cmd/config.go | 72 | ||||
-rw-r--r-- | client/go/cmd/deploy.go | 26 | ||||
-rw-r--r-- | client/go/cmd/deploy_test.go | 62 | ||||
-rw-r--r-- | client/go/cmd/document.go | 164 | ||||
-rw-r--r-- | client/go/cmd/document_test.go | 74 | ||||
-rw-r--r-- | client/go/cmd/init.go | 234 | ||||
-rw-r--r-- | client/go/cmd/init_test.go | 24 | ||||
-rw-r--r-- | client/go/cmd/prepare.go | 26 | ||||
-rw-r--r-- | client/go/cmd/query.go | 96 | ||||
-rw-r--r-- | client/go/cmd/query_test.go | 71 | ||||
-rw-r--r-- | client/go/cmd/root.go | 2 | ||||
-rw-r--r-- | client/go/cmd/status.go | 72 | ||||
-rw-r--r-- | client/go/cmd/status_test.go | 56 | ||||
-rw-r--r-- | client/go/cmd/target.go | 85 | ||||
-rw-r--r-- | client/go/main.go | 4 | ||||
-rw-r--r-- | client/go/util/http.go | 50 | ||||
-rw-r--r-- | client/go/util/http_test.go | 55 | ||||
-rw-r--r-- | client/go/util/io.go | 26 | ||||
-rw-r--r-- | client/go/util/print.go | 52 | ||||
-rw-r--r-- | client/go/vespa/deploy.go | 225 |
22 files changed, 774 insertions, 776 deletions
diff --git a/client/go/cmd/activate.go b/client/go/cmd/activate.go index fe989726253..bb75fff2a49 100644 --- a/client/go/cmd/activate.go +++ b/client/go/cmd/activate.go @@ -5,21 +5,21 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/vespa" + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/vespa" ) func init() { - rootCmd.AddCommand(activateCmd) + rootCmd.AddCommand(activateCmd) } // TODO: Implement and test var activateCmd = &cobra.Command{ - Use: "activate", - Short: "Activates (deploys) the previously prepared application package", - Long: `TODO`, - Run: func(cmd *cobra.Command, args []string) { - vespa.Deploy(true, "", deployTarget()) - }, + Use: "activate", + Short: "Activates (deploys) the previously prepared application package", + Long: `TODO`, + Run: func(cmd *cobra.Command, args []string) { + vespa.Deploy(true, "", deployTarget()) + }, } diff --git a/client/go/cmd/command_tester.go b/client/go/cmd/command_tester.go index 40c3d04446e..50684d87a1b 100644 --- a/client/go/cmd/command_tester.go +++ b/client/go/cmd/command_tester.go @@ -5,26 +5,26 @@ package cmd import ( - "bytes" - "github.com/vespa-engine/vespa/util" - "github.com/stretchr/testify/assert" + "bytes" + "github.com/stretchr/testify/assert" + "github.com/vespa-engine/vespa/util" "io/ioutil" - "net/http" - "testing" - "strconv" - "time" + "net/http" + "strconv" + "testing" + "time" ) func executeCommand(t *testing.T, client *mockHttpClient, args []string, moreArgs []string) (standardout string) { - util.ActiveHttpClient = client + util.ActiveHttpClient = client - // Reset - persistent flags in Cobra persists over tests - util.Out = bytes.NewBufferString("") + // Reset - persistent flags in Cobra persists over tests + util.Out = bytes.NewBufferString("") rootCmd.SetArgs([]string{"status", "-t", ""}) rootCmd.Execute() b := bytes.NewBufferString("") - util.Out = b + util.Out = b rootCmd.SetArgs(append(args, moreArgs...)) rootCmd.Execute() out, err := ioutil.ReadAll(b) @@ -33,26 +33,26 @@ func executeCommand(t *testing.T, client *mockHttpClient, args []string, moreArg } type mockHttpClient struct { - // The HTTP status code that will be returned from the next invocation. Default: 200 - nextStatus int + // The HTTP status code that will be returned from the next invocation. Default: 200 + nextStatus int - // The response body code that will be returned from the next invocation. Default: "" - nextBody string + // The response body code that will be returned from the next invocation. Default: "" + nextBody string - // A recording of the last HTTP request made through this - lastRequest *http.Request + // A recording of the last HTTP request made through this + lastRequest *http.Request } func (c *mockHttpClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) { - if c.nextStatus == 0 { - c.nextStatus = 200 - } - c.lastRequest = request - return &http.Response{ - Status: "Status " + strconv.Itoa(c.nextStatus), - StatusCode: c.nextStatus, - Body: ioutil.NopCloser(bytes.NewBufferString(c.nextBody)), - Header: make(http.Header), - }, - nil + if c.nextStatus == 0 { + c.nextStatus = 200 + } + c.lastRequest = request + return &http.Response{ + Status: "Status " + strconv.Itoa(c.nextStatus), + StatusCode: c.nextStatus, + Body: ioutil.NopCloser(bytes.NewBufferString(c.nextBody)), + Header: make(http.Header), + }, + nil } diff --git a/client/go/cmd/config.go b/client/go/cmd/config.go index 2c10eaf8f02..6d25f33d07c 100644 --- a/client/go/cmd/config.go +++ b/client/go/cmd/config.go @@ -5,59 +5,59 @@ package cmd import ( - "github.com/spf13/cobra" + "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/vespa-engine/vespa/util" - "os" - "path/filepath" + "github.com/vespa-engine/vespa/util" + "os" + "path/filepath" ) func init() { - rootCmd.AddCommand(configCmd) + rootCmd.AddCommand(configCmd) } var configCmd = &cobra.Command{ - Use: "config", - Short: "Configure the Vespa command", - Long: `TODO`, - Run: func(cmd *cobra.Command, args []string) { - }, + Use: "config", + Short: "Configure the Vespa command", + Long: `TODO`, + Run: func(cmd *cobra.Command, args []string) { + }, } func readConfig() { - home, err := os.UserHomeDir() - configName := ".vespa" - configType := "yaml" - - cobra.CheckErr(err) - viper.AddConfigPath(home) - viper.SetConfigType(configType) - viper.SetConfigName(configName) + home, err := os.UserHomeDir() + configName := ".vespa" + configType := "yaml" + + cobra.CheckErr(err) + viper.AddConfigPath(home) + viper.SetConfigType(configType) + viper.SetConfigName(configName) viper.AutomaticEnv() - viper.ReadInConfig(); + viper.ReadInConfig() } // WIP: Not used yet func writeConfig() { - //viper.BindPFlag("container-target", rootCmd.PersistentFlags().Lookup("container-target")) - //viper.SetDefault("container-target", "http://127.0.0.1:8080") - - home, _ := os.UserHomeDir() - configName := ".vespa" - configType := "yaml" - - // Viper bug: WriteConfig() will not create the file if missing - configPath := filepath.Join(home, configName + "." + configType) - _, statErr := os.Stat(configPath) - if !os.IsExist(statErr) { - if _, createErr := os.Create(configPath); createErr != nil { - util.Error("Warning: Can not remember flag parameters: " + createErr.Error()) - } - } + //viper.BindPFlag("container-target", rootCmd.PersistentFlags().Lookup("container-target")) + //viper.SetDefault("container-target", "http://127.0.0.1:8080") + + home, _ := os.UserHomeDir() + configName := ".vespa" + configType := "yaml" + + // Viper bug: WriteConfig() will not create the file if missing + configPath := filepath.Join(home, configName+"."+configType) + _, statErr := os.Stat(configPath) + if !os.IsExist(statErr) { + if _, createErr := os.Create(configPath); createErr != nil { + util.Error("Warning: Can not remember flag parameters: " + createErr.Error()) + } + } writeErr := viper.WriteConfig() if writeErr != nil { - util.Error("Could not write config:", writeErr.Error()) + util.Error("Could not write config:", writeErr.Error()) } -}
\ No newline at end of file +} diff --git a/client/go/cmd/deploy.go b/client/go/cmd/deploy.go index 0b0cd55abf9..6377abda00c 100644 --- a/client/go/cmd/deploy.go +++ b/client/go/cmd/deploy.go @@ -5,23 +5,23 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/vespa" + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/vespa" ) func init() { - rootCmd.AddCommand(deployCmd) + rootCmd.AddCommand(deployCmd) } var deployCmd = &cobra.Command{ - Use: "deploy", - Short: "Deploys (prepares and activates) an application package", - Long: `TODO`, - Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - vespa.Deploy(false, "", deployTarget()) - } else { - vespa.Deploy(false, args[0], deployTarget()) - } - }, + Use: "deploy", + Short: "Deploys (prepares and activates) an application package", + Long: `TODO`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + vespa.Deploy(false, "", deployTarget()) + } else { + vespa.Deploy(false, args[0], deployTarget()) + } + }, } diff --git a/client/go/cmd/deploy_test.go b/client/go/cmd/deploy_test.go index c80b18d0878..a5c110f332e 100644 --- a/client/go/cmd/deploy_test.go +++ b/client/go/cmd/deploy_test.go @@ -5,74 +5,74 @@ package cmd import ( - "github.com/stretchr/testify/assert" - "strconv" - "testing" + "github.com/stretchr/testify/assert" + "strconv" + "testing" ) func TestDeployZip(t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mSuccess\n", - executeCommand(t, client, []string{"deploy", "testdata/application.zip"}, []string{})) + "\x1b[32mSuccess\n", + executeCommand(t, client, []string{"deploy", "testdata/application.zip"}, []string{})) assertDeployRequestMade("http://127.0.0.1:19071", client, t) } func TestDeployZipWithURLTargetArgument(t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mSuccess\n", - executeCommand(t, client, []string{"deploy", "testdata/application.zip", "-t", "http://target:19071"}, []string{})) + "\x1b[32mSuccess\n", + executeCommand(t, client, []string{"deploy", "testdata/application.zip", "-t", "http://target:19071"}, []string{})) assertDeployRequestMade("http://target:19071", client, t) } func TestDeployZipWitLocalTargetArgument(t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mSuccess\n", - executeCommand(t, client, []string{"deploy", "testdata/application.zip", "-t", "local"}, []string{})) + "\x1b[32mSuccess\n", + executeCommand(t, client, []string{"deploy", "testdata/application.zip", "-t", "local"}, []string{})) assertDeployRequestMade("http://127.0.0.1:19071", client, t) } func TestDeployDirectory(t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mSuccess\n", - executeCommand(t, client, []string{"deploy", "testdata/src/main/application"}, []string{})) + "\x1b[32mSuccess\n", + executeCommand(t, client, []string{"deploy", "testdata/src/main/application"}, []string{})) assertDeployRequestMade("http://127.0.0.1:19071", client, t) } func TestDeployApplicationPackageError(t *testing.T) { - assertApplicationPackageError(t, 401, "Application package error") + assertApplicationPackageError(t, 401, "Application package error") } func TestDeployError(t *testing.T) { - assertDeployServerError(t, 501, "Deploy service error") + assertDeployServerError(t, 501, "Deploy service error") } // TODO: Test prepare and activate prepared func assertDeployRequestMade(target string, client *mockHttpClient, t *testing.T) { - assert.Equal(t, target + "/application/v2/tenant/default/prepareandactivate", client.lastRequest.URL.String()) - assert.Equal(t, "application/zip", client.lastRequest.Header.Get("Content-Type")) - assert.Equal(t, "POST", client.lastRequest.Method) - var body = client.lastRequest.Body - assert.NotNil(t, body) - buf := make([]byte, 7) // Just check the first few bytes - body.Read(buf) - assert.Equal(t, "PK\x03\x04\x14\x00\b", string(buf)) + assert.Equal(t, target+"/application/v2/tenant/default/prepareandactivate", client.lastRequest.URL.String()) + assert.Equal(t, "application/zip", client.lastRequest.Header.Get("Content-Type")) + assert.Equal(t, "POST", client.lastRequest.Method) + var body = client.lastRequest.Body + assert.NotNil(t, body) + buf := make([]byte, 7) // Just check the first few bytes + body.Read(buf) + assert.Equal(t, "PK\x03\x04\x14\x00\b", string(buf)) } func assertApplicationPackageError(t *testing.T, status int, errorMessage string) { - client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } + client := &mockHttpClient{nextStatus: status, nextBody: errorMessage} assert.Equal(t, - "\x1b[31mInvalid application package (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"deploy", "testdata/src/main/application"}, []string{})) + "\x1b[31mInvalid application package (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n", + executeCommand(t, client, []string{"deploy", "testdata/src/main/application"}, []string{})) } func assertDeployServerError(t *testing.T, status int, errorMessage string) { - client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } + client := &mockHttpClient{nextStatus: status, nextBody: errorMessage} assert.Equal(t, - "\x1b[31mError from deploy service at 127.0.0.1:19071 (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"deploy", "testdata/src/main/application"}, []string{})) + "\x1b[31mError from deploy service at 127.0.0.1:19071 (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n", + executeCommand(t, client, []string{"deploy", "testdata/src/main/application"}, []string{})) } diff --git a/client/go/cmd/document.go b/client/go/cmd/document.go index 9e51f3c8857..176e78bc14b 100644 --- a/client/go/cmd/document.go +++ b/client/go/cmd/document.go @@ -5,110 +5,110 @@ package cmd import ( - "bytes" - "encoding/json" - "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/util" - "io/ioutil" - "net/http" - "net/url" - "os" - "strings" - "time" + "bytes" + "encoding/json" + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/util" + "io/ioutil" + "net/http" + "net/url" + "os" + "strings" + "time" ) func init() { - rootCmd.AddCommand(documentCmd) - documentCmd.AddCommand(documentPostCmd) - documentCmd.AddCommand(documentGetCmd) + rootCmd.AddCommand(documentCmd) + documentCmd.AddCommand(documentPostCmd) + documentCmd.AddCommand(documentGetCmd) } var documentCmd = &cobra.Command{ - Use: "document", - Short: "Issue document operations", - Long: `TODO: Example vespa document mynamespace/mydocumenttype/myid document.json`, - // TODO: Check args - Run: func(cmd *cobra.Command, args []string) { - post("", args[0]) - }, + Use: "document", + Short: "Issue document operations", + Long: `TODO: Example vespa document mynamespace/mydocumenttype/myid document.json`, + // TODO: Check args + Run: func(cmd *cobra.Command, args []string) { + post("", args[0]) + }, } var documentPostCmd = &cobra.Command{ - Use: "post", - Short: "Posts the document in the given file", - Long: `TODO`, - // TODO: Check args - Run: func(cmd *cobra.Command, args []string) { - if len(args) == 1 { - post("", args[0]) - } else { - post(args[0], args[1]) - } - }, + Use: "post", + Short: "Posts the document in the given file", + Long: `TODO`, + // TODO: Check args + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 1 { + post("", args[0]) + } else { + post(args[0], args[1]) + } + }, } var documentGetCmd = &cobra.Command{ - Use: "get", - Short: "Gets a document", - Long: `TODO`, - // TODO: Check args - Run: func(cmd *cobra.Command, args []string) { - get(args[0]) - }, + Use: "get", + Short: "Gets a document", + Long: `TODO`, + // TODO: Check args + Run: func(cmd *cobra.Command, args []string) { + get(args[0]) + }, } func get(documentId string) { - // TODO + // TODO } func post(documentId string, jsonFile string) { - header := http.Header{} - header.Add("Content-Type", "application/json") + header := http.Header{} + header.Add("Content-Type", "application/json") - fileReader, fileError := os.Open(jsonFile) - if fileError != nil { - util.Error("Could not open file at " + jsonFile) - util.Detail(fileError.Error()) - return - } + fileReader, fileError := os.Open(jsonFile) + if fileError != nil { + util.Error("Could not open file at " + jsonFile) + util.Detail(fileError.Error()) + return + } - documentData := util.ReaderToBytes(fileReader) + documentData := util.ReaderToBytes(fileReader) - if documentId == "" { - var doc map[string]interface{} - json.Unmarshal(documentData, &doc) - if doc["id"] != nil { - documentId = doc["id"].(string) - } else if doc["put"] != nil { - documentId = doc["put"].(string) // document feeder format - } else { - util.Error("No document id given neither as argument or an 'id' key in the json file") - return - } - } + if documentId == "" { + var doc map[string]interface{} + json.Unmarshal(documentData, &doc) + if doc["id"] != nil { + documentId = doc["id"].(string) + } else if doc["put"] != nil { + documentId = doc["put"].(string) // document feeder format + } else { + util.Error("No document id given neither as argument or an 'id' key in the json file") + return + } + } - url, _ := url.Parse(documentTarget() + "/document/v1/" + documentId) + url, _ := url.Parse(documentTarget() + "/document/v1/" + documentId) - request := &http.Request{ - URL: url, - Method: "POST", - Header: header, - Body: ioutil.NopCloser(bytes.NewReader(documentData)), - } - serviceDescription := "Container (document API)" - response := util.HttpDo(request, time.Second * 60, serviceDescription) - if (response == nil) { - return - } + request := &http.Request{ + URL: url, + Method: "POST", + Header: header, + Body: ioutil.NopCloser(bytes.NewReader(documentData)), + } + serviceDescription := "Container (document API)" + response := util.HttpDo(request, time.Second*60, serviceDescription) + if response == nil { + return + } - defer response.Body.Close() - if response.StatusCode == 200 { - util.Success("Success") // TODO: Change to something including document id - } else if response.StatusCode / 100 == 4 { - util.Error("Invalid document (" + response.Status + "):") - util.PrintReader(response.Body) - } else { - util.Error("Error from", strings.ToLower(serviceDescription), "at", request.URL.Host, "(" + response.Status + "):") - util.PrintReader(response.Body) - } + defer response.Body.Close() + if response.StatusCode == 200 { + util.Success("Success") // TODO: Change to something including document id + } else if response.StatusCode/100 == 4 { + util.Error("Invalid document (" + response.Status + "):") + util.PrintReader(response.Body) + } else { + util.Error("Error from", strings.ToLower(serviceDescription), "at", request.URL.Host, "("+response.Status+"):") + util.PrintReader(response.Body) + } } diff --git a/client/go/cmd/document_test.go b/client/go/cmd/document_test.go index 688e9f58080..9d37e29c661 100644 --- a/client/go/cmd/document_test.go +++ b/client/go/cmd/document_test.go @@ -5,39 +5,39 @@ package cmd import ( - "github.com/stretchr/testify/assert" - "github.com/vespa-engine/vespa/util" - "io/ioutil" - "strconv" - "testing" + "github.com/stretchr/testify/assert" + "github.com/vespa-engine/vespa/util" + "io/ioutil" + "strconv" + "testing" ) func TestDocumentPostWithIdArg(t *testing.T) { assertDocumentPost([]string{"document", "post", "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams.json"}, - "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams.json", t) + "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams.json", t) } func TestDocumentPostWithIdInDocument(t *testing.T) { assertDocumentPost([]string{"document", "post", "testdata/A-Head-Full-of-Dreams-With-Id.json"}, - "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams-With-Id.json", t) + "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams-With-Id.json", t) } func TestDocumentPostWithIdInDocumentShortForm(t *testing.T) { assertDocumentPost([]string{"document", "testdata/A-Head-Full-of-Dreams-With-Id.json"}, - "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams-With-Id.json", t) + "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams-With-Id.json", t) } func TestDocumentPostWithIdAsPutInDocument(t *testing.T) { assertDocumentPost([]string{"document", "post", "testdata/A-Head-Full-of-Dreams-With-Put.json"}, - "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams-With-Put.json", t) + "mynamespace/music/docid/1", "testdata/A-Head-Full-of-Dreams-With-Put.json", t) } func TestDocumentIdNotSpecified(t *testing.T) { arguments := []string{"document", "post", "testdata/A-Head-Full-of-Dreams.json"} - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[31mNo document id given neither as argument or an 'id' key in the json file\n", - executeCommand(t, client, arguments, []string{})) + "\x1b[31mNo document id given neither as argument or an 'id' key in the json file\n", + executeCommand(t, client, arguments, []string{})) } func TestDocumentPostDocumentError(t *testing.T) { @@ -49,42 +49,42 @@ func TestDocumentPostServerError(t *testing.T) { } func assertDocumentPost(arguments []string, documentId string, jsonFile string, t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mSuccess\n", - executeCommand(t, client, arguments, []string{})) - target := getTarget(documentContext).document - assert.Equal(t, target + "/document/v1/" + documentId, client.lastRequest.URL.String()) - assert.Equal(t, "application/json", client.lastRequest.Header.Get("Content-Type")) - assert.Equal(t, "POST", client.lastRequest.Method) + "\x1b[32mSuccess\n", + executeCommand(t, client, arguments, []string{})) + target := getTarget(documentContext).document + assert.Equal(t, target+"/document/v1/"+documentId, client.lastRequest.URL.String()) + assert.Equal(t, "application/json", client.lastRequest.Header.Get("Content-Type")) + assert.Equal(t, "POST", client.lastRequest.Method) - fileContent, _ := ioutil.ReadFile(jsonFile) - assert.Equal(t, string(fileContent), util.ReaderToString(client.lastRequest.Body)) + fileContent, _ := ioutil.ReadFile(jsonFile) + assert.Equal(t, string(fileContent), util.ReaderToString(client.lastRequest.Body)) } func assertDocumentPostShortForm(documentId string, jsonFile string, t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mSuccess\n", - executeCommand(t, client, []string{"document", jsonFile}, []string{})) - target := getTarget(documentContext).document - assert.Equal(t, target + "/document/v1/" + documentId, client.lastRequest.URL.String()) + "\x1b[32mSuccess\n", + executeCommand(t, client, []string{"document", jsonFile}, []string{})) + target := getTarget(documentContext).document + assert.Equal(t, target+"/document/v1/"+documentId, client.lastRequest.URL.String()) } func assertDocumentError(t *testing.T, status int, errorMessage string) { - client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } + client := &mockHttpClient{nextStatus: status, nextBody: errorMessage} assert.Equal(t, - "\x1b[31mInvalid document (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"document", "post", - "mynamespace/music/docid/1", - "testdata/A-Head-Full-of-Dreams.json"}, []string{})) + "\x1b[31mInvalid document (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n", + executeCommand(t, client, []string{"document", "post", + "mynamespace/music/docid/1", + "testdata/A-Head-Full-of-Dreams.json"}, []string{})) } func assertDocumentServerError(t *testing.T, status int, errorMessage string) { - client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } + client := &mockHttpClient{nextStatus: status, nextBody: errorMessage} assert.Equal(t, - "\x1b[31mError from container (document api) at 127.0.0.1:8080 (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"document", "post", - "mynamespace/music/docid/1", - "testdata/A-Head-Full-of-Dreams.json"}, []string{})) -}
\ No newline at end of file + "\x1b[31mError from container (document api) at 127.0.0.1:8080 (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n", + executeCommand(t, client, []string{"document", "post", + "mynamespace/music/docid/1", + "testdata/A-Head-Full-of-Dreams.json"}, []string{})) +} diff --git a/client/go/cmd/init.go b/client/go/cmd/init.go index 64e8b342309..bae5aa1908b 100644 --- a/client/go/cmd/init.go +++ b/client/go/cmd/init.go @@ -5,153 +5,153 @@ package cmd import ( - "archive/zip" - "errors" - "path/filepath" - "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/util" - "io" - "io/ioutil" - "net/http" - "net/url" - "os" - "strings" - "time" + "archive/zip" + "errors" + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/util" + "io" + "io/ioutil" + "net/http" + "net/url" + "os" + "path/filepath" + "strings" + "time" ) // Set this to test without downloading this file from github var existingSampleAppsZip string func init() { - existingSampleAppsZip = "" - rootCmd.AddCommand(initCmd) + existingSampleAppsZip = "" + rootCmd.AddCommand(initCmd) } var initCmd = &cobra.Command{ - // TODO: "application" and "list" subcommands? - Use: "init", - Short: "Creates the files and directory structure for a new Vespa application", - Long: `TODO: vespa init applicationName source`, - Args: func(cmd *cobra.Command, args []string) error { - if len(args) != 2 { - 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]) - }, + // TODO: "application" and "list" subcommands? + Use: "init", + Short: "Creates the files and directory structure for a new Vespa application", + Long: `TODO: vespa init applicationName source`, + Args: func(cmd *cobra.Command, args []string) error { + if len(args) != 2 { + 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) { - zipFile := getSampleAppsZip() - if zipFile == nil { - return - } - if existingSampleAppsZip == "" { // Indicates we created a temp file now - defer os.Remove(zipFile.Name()) - } + zipFile := getSampleAppsZip() + if zipFile == nil { + return + } + if existingSampleAppsZip == "" { // Indicates we created a temp file now + defer os.Remove(zipFile.Name()) + } - createErr := os.Mkdir(name, 0755) - if createErr != nil { - util.Error("Could not create directory '" + name + "'") - util.Detail(createErr.Error()) - return - } + createErr := os.Mkdir(name, 0755) + if createErr != nil { + util.Error("Could not create directory '" + name + "'") + util.Detail(createErr.Error()) + return + } zipReader, zipOpenError := zip.OpenReader(zipFile.Name()) if zipOpenError != nil { - util.Error("Could not open sample apps zip '" + zipFile.Name() + "'") - util.Detail(zipOpenError.Error()) + util.Error("Could not open sample apps zip '" + zipFile.Name() + "'") + util.Detail(zipOpenError.Error()) } defer zipReader.Close() - found := false - for _, f := range zipReader.File { - zipEntryPrefix := "sample-apps-master/" + source + "/" - if strings.HasPrefix(f.Name, zipEntryPrefix) { - found = true - copyError := copy(f, name, zipEntryPrefix) - if copyError != nil { - util.Error("Could not copy zip entry '" + f.Name + "' to " + name) - util.Detail(copyError.Error()) - return - } - } + found := false + for _, f := range zipReader.File { + zipEntryPrefix := "sample-apps-master/" + source + "/" + if strings.HasPrefix(f.Name, zipEntryPrefix) { + found = true + copyError := copy(f, name, zipEntryPrefix) + if copyError != nil { + util.Error("Could not copy zip entry '" + f.Name + "' to " + name) + util.Detail(copyError.Error()) + return + } + } } if !found { - util.Error("Could not find source application '" + source + "'") + util.Error("Could not find source application '" + source + "'") } else { - util.Success("Created " + name) - } + util.Success("Created " + name) + } } func getSampleAppsZip() *os.File { - if existingSampleAppsZip != "" { - existing, openExistingError := os.Open(existingSampleAppsZip) - if openExistingError != nil { - util.Error("Could not open existing sample apps zip file '" + existingSampleAppsZip + "'") - util.Detail(openExistingError.Error()) - } - return existing - } + if existingSampleAppsZip != "" { + existing, openExistingError := os.Open(existingSampleAppsZip) + if openExistingError != nil { + util.Error("Could not open existing sample apps zip file '" + existingSampleAppsZip + "'") + util.Detail(openExistingError.Error()) + } + return existing + } - // TODO: Cache it? - util.Detail("Downloading sample apps ...") // TODO: Spawn thread to indicate progress - zipUrl, _ := url.Parse("https://github.com/vespa-engine/sample-apps/archive/refs/heads/master.zip") - request := &http.Request{ - URL: zipUrl, - Method: "GET", - } - response := util.HttpDo(request, time.Minute * 60, "GitHub") - defer response.Body.Close() - if response.StatusCode != 200 { - util.Error("Could not download sample apps from github") - util.Detail(response.Status) - return nil - } + // TODO: Cache it? + util.Detail("Downloading sample apps ...") // TODO: Spawn thread to indicate progress + zipUrl, _ := url.Parse("https://github.com/vespa-engine/sample-apps/archive/refs/heads/master.zip") + request := &http.Request{ + URL: zipUrl, + Method: "GET", + } + response := util.HttpDo(request, time.Minute*60, "GitHub") + defer response.Body.Close() + if response.StatusCode != 200 { + util.Error("Could not download sample apps from github") + util.Detail(response.Status) + return nil + } - destination, tempFileError := ioutil.TempFile("", "prefix") - if tempFileError != nil { - util.Error("Could not create a temp file to hold sample apps") - util.Detail(tempFileError.Error()) - } - // destination, _ := os.Create("./" + name + "/sample-apps.zip") - // defer destination.Close() - _, err := io.Copy(destination, response.Body) - if err != nil { - util.Error("Could not download sample apps from GitHub") - util.Detail(err.Error()) - return nil - } - return destination + destination, tempFileError := ioutil.TempFile("", "prefix") + if tempFileError != nil { + util.Error("Could not create a temp file to hold sample apps") + util.Detail(tempFileError.Error()) + } + // destination, _ := os.Create("./" + name + "/sample-apps.zip") + // defer destination.Close() + _, err := io.Copy(destination, response.Body) + if err != nil { + util.Error("Could not download sample apps from GitHub") + util.Detail(err.Error()) + return nil + } + return destination } func copy(f *zip.File, destinationDir string, zipEntryPrefix string) error { - destinationPath := filepath.Join(destinationDir, filepath.FromSlash(strings.TrimPrefix(f.Name, zipEntryPrefix))) - if strings.HasSuffix(f.Name, "/") { - if f.Name != zipEntryPrefix { // root is already created - createError := os.Mkdir(destinationPath, 0755) - if createError != nil { - return createError - } - } - } else { - zipEntry, zipEntryOpenError := f.Open() - if zipEntryOpenError != nil { - return zipEntryOpenError - } - defer zipEntry.Close() + destinationPath := filepath.Join(destinationDir, filepath.FromSlash(strings.TrimPrefix(f.Name, zipEntryPrefix))) + if strings.HasSuffix(f.Name, "/") { + if f.Name != zipEntryPrefix { // root is already created + createError := os.Mkdir(destinationPath, 0755) + if createError != nil { + return createError + } + } + } else { + zipEntry, zipEntryOpenError := f.Open() + if zipEntryOpenError != nil { + return zipEntryOpenError + } + defer zipEntry.Close() - destination, createError := os.Create(destinationPath) - if createError != nil { - return createError - } + destination, createError := os.Create(destinationPath) + if createError != nil { + return createError + } - _, copyError := io.Copy(destination, zipEntry) - if copyError != nil { - return copyError - } - } - return nil + _, copyError := io.Copy(destination, zipEntry) + if copyError != nil { + return copyError + } + } + return nil } diff --git a/client/go/cmd/init_test.go b/client/go/cmd/init_test.go index 3cb526ad0f1..f93af26b2eb 100644 --- a/client/go/cmd/init_test.go +++ b/client/go/cmd/init_test.go @@ -5,28 +5,28 @@ package cmd import ( - "github.com/vespa-engine/vespa/util" - "github.com/stretchr/testify/assert" - "os" - "testing" - "path/filepath" + "github.com/stretchr/testify/assert" + "github.com/vespa-engine/vespa/util" + "os" + "path/filepath" + "testing" ) func TestInit(t *testing.T) { - assertCreated("mytestapp", "album-recommendation-selfhosted", t) + assertCreated("mytestapp", "album-recommendation-selfhosted", t) } func assertCreated(app string, sampleAppName string, t *testing.T) { - existingSampleAppsZip = "testdata/sample-apps-master.zip" - standardOut := executeCommand(t, &mockHttpClient{}, []string{"init", app, sampleAppName}, []string{}) + existingSampleAppsZip = "testdata/sample-apps-master.zip" + standardOut := executeCommand(t, &mockHttpClient{}, []string{"init", app, sampleAppName}, []string{}) defer os.RemoveAll(app) - assert.Equal(t, "\x1b[32mCreated " + app + "\n", standardOut) + assert.Equal(t, "\x1b[32mCreated "+app+"\n", standardOut) assert.True(t, util.PathExists(filepath.Join(app, "README.md"))) assert.True(t, util.PathExists(filepath.Join(app, "src", "main", "application"))) assert.True(t, util.IsDirectory(filepath.Join(app, "src", "main", "application"))) - servicesStat, _ := os.Stat(filepath.Join(app, "src", "main", "application", "services.xml")) - var servicesSize int64 - servicesSize = 2474 + servicesStat, _ := os.Stat(filepath.Join(app, "src", "main", "application", "services.xml")) + var servicesSize int64 + servicesSize = 2474 assert.Equal(t, servicesSize, servicesStat.Size()) } diff --git a/client/go/cmd/prepare.go b/client/go/cmd/prepare.go index b65f845be81..7dfa5f92c33 100644 --- a/client/go/cmd/prepare.go +++ b/client/go/cmd/prepare.go @@ -5,25 +5,25 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/vespa" + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/vespa" ) func init() { - rootCmd.AddCommand(prepareCmd) + rootCmd.AddCommand(prepareCmd) } // TODO: Implement and test var prepareCmd = &cobra.Command{ - Use: "prepare", - Short: "Prepares an application package for activation", - Long: `TODO`, - Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - vespa.Deploy(true, "", deployTarget()) - } else { - vespa.Deploy(true, args[0], deployTarget()) - } - }, + Use: "prepare", + Short: "Prepares an application package for activation", + Long: `TODO`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + vespa.Deploy(true, "", deployTarget()) + } else { + vespa.Deploy(true, args[0], deployTarget()) + } + }, } diff --git a/client/go/cmd/query.go b/client/go/cmd/query.go index ed412d4a396..7a6a569eb70 100644 --- a/client/go/cmd/query.go +++ b/client/go/cmd/query.go @@ -5,66 +5,66 @@ package cmd import ( - "errors" - "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/util" - "strings" - "net/http" - "net/url" - "time" + "errors" + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/util" + "net/http" + "net/url" + "strings" + "time" ) func init() { - rootCmd.AddCommand(queryCmd) + rootCmd.AddCommand(queryCmd) } var queryCmd = &cobra.Command{ - Use: "query", - Short: "Issue a query to Vespa", - Long: `TODO, example \"yql=select from sources * where title contains 'foo'\" hits=5`, - // TODO: Support referencing a query json file - Args: func(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return errors.New("vespa query requires at least one argument containing the query string") - } - return nil - }, - Run: func(cmd *cobra.Command, args []string) { - query(args) - }, + Use: "query", + Short: "Issue a query to Vespa", + Long: `TODO, example \"yql=select from sources * where title contains 'foo'\" hits=5`, + // TODO: Support referencing a query json file + Args: func(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return errors.New("vespa query requires at least one argument containing the query string") + } + return nil + }, + Run: func(cmd *cobra.Command, args []string) { + query(args) + }, } func query(arguments []string) { - url, _ := url.Parse(queryTarget() + "/search/") - urlQuery := url.Query() - for i := 0; i < len(arguments); i++ { - key, value := splitArg(arguments[i]) - urlQuery.Set(key, value) - } - url.RawQuery = urlQuery.Encode() + url, _ := url.Parse(queryTarget() + "/search/") + urlQuery := url.Query() + for i := 0; i < len(arguments); i++ { + key, value := splitArg(arguments[i]) + urlQuery.Set(key, value) + } + url.RawQuery = urlQuery.Encode() - response := util.HttpDo(&http.Request{URL: url,}, time.Second * 10, "Container") - if (response == nil) { - return - } - defer response.Body.Close() + response := util.HttpDo(&http.Request{URL: url}, time.Second*10, "Container") + if response == nil { + return + } + defer response.Body.Close() - if (response.StatusCode == 200) { - util.PrintReader(response.Body) - } else if response.StatusCode / 100 == 4 { - util.Error("Invalid query (" + response.Status + "):") - util.PrintReader(response.Body) - } else { - util.Error("Error from container at", url.Host, "(" + response.Status + "):") - util.PrintReader(response.Body) - } + if response.StatusCode == 200 { + util.PrintReader(response.Body) + } else if response.StatusCode/100 == 4 { + util.Error("Invalid query (" + response.Status + "):") + util.PrintReader(response.Body) + } else { + util.Error("Error from container at", url.Host, "("+response.Status+"):") + util.PrintReader(response.Body) + } } func splitArg(argument string) (string, string) { - equalsIndex := strings.Index(argument, "=") - if equalsIndex < 1 { - return "yql", argument - } else { - return argument[0:equalsIndex], argument[equalsIndex + 1:len(argument)] - } + equalsIndex := strings.Index(argument, "=") + if equalsIndex < 1 { + return "yql", argument + } else { + return argument[0:equalsIndex], argument[equalsIndex+1 : len(argument)] + } } diff --git a/client/go/cmd/query_test.go b/client/go/cmd/query_test.go index f55956bc1ce..bf6cc13fe8a 100644 --- a/client/go/cmd/query_test.go +++ b/client/go/cmd/query_test.go @@ -5,74 +5,73 @@ package cmd import ( - "github.com/stretchr/testify/assert" - "strconv" - "testing" + "github.com/stretchr/testify/assert" + "strconv" + "testing" ) func TestQuery(t *testing.T) { - assertQuery(t, - "?yql=select+from+sources+%2A+where+title+contains+%27foo%27", - "select from sources * where title contains 'foo'") + assertQuery(t, + "?yql=select+from+sources+%2A+where+title+contains+%27foo%27", + "select from sources * where title contains 'foo'") } func TestQueryNonJsonResult(t *testing.T) { - assertQuery(t, - "?yql=select+from+sources+%2A+where+title+contains+%27foo%27", - "select from sources * where title contains 'foo'") + assertQuery(t, + "?yql=select+from+sources+%2A+where+title+contains+%27foo%27", + "select from sources * where title contains 'foo'") } func TestQueryWithMultipleParameters(t *testing.T) { - assertQuery(t, - "?hits=5&yql=select+from+sources+%2A+where+title+contains+%27foo%27", - "select from sources * where title contains 'foo'", "hits=5") + assertQuery(t, + "?hits=5&yql=select+from+sources+%2A+where+title+contains+%27foo%27", + "select from sources * where title contains 'foo'", "hits=5") } func TestQueryWithExplicitYqlParameter(t *testing.T) { - assertQuery(t, - "?yql=select+from+sources+%2A+where+title+contains+%27foo%27", - "yql=select from sources * where title contains 'foo'") + assertQuery(t, + "?yql=select+from+sources+%2A+where+title+contains+%27foo%27", + "yql=select from sources * where title contains 'foo'") } func TestIllegalQuery(t *testing.T) { - assertQueryError(t, 401, "query error message") + assertQueryError(t, 401, "query error message") } func TestServerError(t *testing.T) { - assertQueryServiceError(t, 501, "server error message") + assertQueryServiceError(t, 501, "server error message") } func assertQuery(t *testing.T, expectedQuery string, query ...string) { - client := &mockHttpClient{ nextBody: "{\"query\":\"result\"}", } + client := &mockHttpClient{nextBody: "{\"query\":\"result\"}"} assert.Equal(t, - "{\n \"query\": \"result\"\n}\n", - executeCommand(t, client, []string{"query"}, query), - "query output") - assert.Equal(t, getTarget(queryContext).query + "/search/" + expectedQuery, client.lastRequest.URL.String()) + "{\n \"query\": \"result\"\n}\n", + executeCommand(t, client, []string{"query"}, query), + "query output") + assert.Equal(t, getTarget(queryContext).query+"/search/"+expectedQuery, client.lastRequest.URL.String()) } func assertQueryNonJsonResult(t *testing.T, expectedQuery string, query ...string) { - client := &mockHttpClient{ nextBody: "query result", } + client := &mockHttpClient{nextBody: "query result"} assert.Equal(t, - "query result\n", - executeCommand(t, client, []string{"query"}, query), - "query output") - assert.Equal(t, getTarget(queryContext).query + "/search/" + expectedQuery, client.lastRequest.URL.String()) + "query result\n", + executeCommand(t, client, []string{"query"}, query), + "query output") + assert.Equal(t, getTarget(queryContext).query+"/search/"+expectedQuery, client.lastRequest.URL.String()) } func assertQueryError(t *testing.T, status int, errorMessage string) { - client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } + client := &mockHttpClient{nextStatus: status, nextBody: errorMessage} assert.Equal(t, - "\x1b[31mInvalid query (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"query"}, []string{"yql=select from sources * where title contains 'foo'"}), - "error output") + "\x1b[31mInvalid query (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n", + executeCommand(t, client, []string{"query"}, []string{"yql=select from sources * where title contains 'foo'"}), + "error output") } func assertQueryServiceError(t *testing.T, status int, errorMessage string) { - client := &mockHttpClient{ nextStatus: status, nextBody: errorMessage, } + client := &mockHttpClient{nextStatus: status, nextBody: errorMessage} assert.Equal(t, - "\x1b[31mError from container at 127.0.0.1:8080 (Status " + strconv.Itoa(status) + "):\n" + errorMessage + "\n", - executeCommand(t, client, []string{"query"}, []string{"yql=select from sources * where title contains 'foo'"}), - "error output") + "\x1b[31mError from container at 127.0.0.1:8080 (Status "+strconv.Itoa(status)+"):\n"+errorMessage+"\n", + executeCommand(t, client, []string{"query"}, []string{"yql=select from sources * where title contains 'foo'"}), + "error output") } - diff --git a/client/go/cmd/root.go b/client/go/cmd/root.go index 8f33cb47c43..b3306bbe617 100644 --- a/client/go/cmd/root.go +++ b/client/go/cmd/root.go @@ -5,7 +5,7 @@ package cmd import ( - "github.com/spf13/cobra" + "github.com/spf13/cobra" ) var ( diff --git a/client/go/cmd/status.go b/client/go/cmd/status.go index 4187c34c2b1..4425bc44d5b 100644 --- a/client/go/cmd/status.go +++ b/client/go/cmd/status.go @@ -5,57 +5,57 @@ package cmd import ( - "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/util" + "github.com/spf13/cobra" + "github.com/vespa-engine/vespa/util" ) func init() { - rootCmd.AddCommand(statusCmd) - statusCmd.AddCommand(statusContainerCmd) - statusCmd.AddCommand(statusConfigServerCmd) + rootCmd.AddCommand(statusCmd) + statusCmd.AddCommand(statusContainerCmd) + statusCmd.AddCommand(statusConfigServerCmd) } // TODO: Use deploy, query and document instead of container and config-server var statusCmd = &cobra.Command{ - Use: "status", - Short: "Verifies that a vespa target is ready to use (container by default)", - Long: `TODO`, - Run: func(cmd *cobra.Command, args []string) { - status(queryTarget(), "Container") - }, + Use: "status", + Short: "Verifies that a vespa target is ready to use (container by default)", + Long: `TODO`, + Run: func(cmd *cobra.Command, args []string) { + status(queryTarget(), "Container") + }, } var statusContainerCmd = &cobra.Command{ - Use: "container", - Short: "Verifies that your Vespa container endpoint is ready [Default]", - Long: `TODO`, - Run: func(cmd *cobra.Command, args []string) { - status(queryTarget(), "Container") - }, + Use: "container", + Short: "Verifies that your Vespa container endpoint is ready [Default]", + Long: `TODO`, + Run: func(cmd *cobra.Command, args []string) { + status(queryTarget(), "Container") + }, } var statusConfigServerCmd = &cobra.Command{ - Use: "config-server", - Short: "Verifies that your Vespa config server endpoint is ready", - Long: `TODO`, - Run: func(cmd *cobra.Command, args []string) { - status(deployTarget(), "Config server") - }, + Use: "config-server", + Short: "Verifies that your Vespa config server endpoint is ready", + Long: `TODO`, + Run: func(cmd *cobra.Command, args []string) { + status(deployTarget(), "Config server") + }, } func status(target string, description string) { - path := "/ApplicationStatus" - response := util.HttpGet(target, path, description) - if (response == nil) { - return - } - defer response.Body.Close() - - if response.StatusCode != 200 { - util.Error(description, "at", target, "is not ready") - util.Detail(response.Status) - } else { - util.Success(description, "at", target, "is ready") - } + path := "/ApplicationStatus" + response := util.HttpGet(target, path, description) + if response == nil { + return + } + defer response.Body.Close() + + if response.StatusCode != 200 { + util.Error(description, "at", target, "is not ready") + util.Detail(response.Status) + } else { + util.Success(description, "at", target, "is ready") + } } diff --git a/client/go/cmd/status_test.go b/client/go/cmd/status_test.go index 0910a6007b9..8e81dd5f566 100644 --- a/client/go/cmd/status_test.go +++ b/client/go/cmd/status_test.go @@ -5,66 +5,66 @@ package cmd import ( - "github.com/stretchr/testify/assert" - "testing" + "github.com/stretchr/testify/assert" + "testing" ) func TestStatusConfigServerCommand(t *testing.T) { - assertConfigServerStatus("http://127.0.0.1:19071", []string{}, t) + assertConfigServerStatus("http://127.0.0.1:19071", []string{}, t) } func TestStatusConfigServerCommandWithURLTarget(t *testing.T) { - assertConfigServerStatus("http://mydeploytarget", []string{"-t", "http://mydeploytarget"}, t) + assertConfigServerStatus("http://mydeploytarget", []string{"-t", "http://mydeploytarget"}, t) } func TestStatusConfigServerCommandWithLocalTarget(t *testing.T) { - assertConfigServerStatus("http://127.0.0.1:19071", []string{"-t", "local"}, t) + assertConfigServerStatus("http://127.0.0.1:19071", []string{"-t", "local"}, t) } func TestStatusContainerCommand(t *testing.T) { - assertContainerStatus("http://127.0.0.1:8080", []string{}, t) + assertContainerStatus("http://127.0.0.1:8080", []string{}, t) } func TestStatusContainerCommandWithUrlTarget(t *testing.T) { - assertContainerStatus("http://mycontainertarget", []string{"-t", "http://mycontainertarget"}, t) + assertContainerStatus("http://mycontainertarget", []string{"-t", "http://mycontainertarget"}, t) } func TestStatusContainerCommandWithLocalTarget(t *testing.T) { - assertContainerStatus("http://127.0.0.1:8080", []string{"-t", "local"}, t) + assertContainerStatus("http://127.0.0.1:8080", []string{"-t", "local"}, t) } func TestStatusErrorResponse(t *testing.T) { - assertContainerError("http://127.0.0.1:8080", []string{}, t) + assertContainerError("http://127.0.0.1:8080", []string{}, t) } func assertConfigServerStatus(target string, args []string, t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mConfig server at " + target + " is ready\n", - executeCommand(t, client, []string{"status", "config-server"}, args), - "vespa status config-server") - assert.Equal(t, target + "/ApplicationStatus", client.lastRequest.URL.String()) + "\x1b[32mConfig server at "+target+" is ready\n", + executeCommand(t, client, []string{"status", "config-server"}, args), + "vespa status config-server") + assert.Equal(t, target+"/ApplicationStatus", client.lastRequest.URL.String()) } func assertContainerStatus(target string, args []string, t *testing.T) { - client := &mockHttpClient{} + client := &mockHttpClient{} assert.Equal(t, - "\x1b[32mContainer at " + target + " is ready\n", - executeCommand(t, client, []string{"status", "container"}, args), - "vespa status container") - assert.Equal(t, target + "/ApplicationStatus", client.lastRequest.URL.String()) + "\x1b[32mContainer at "+target+" is ready\n", + executeCommand(t, client, []string{"status", "container"}, args), + "vespa status container") + assert.Equal(t, target+"/ApplicationStatus", client.lastRequest.URL.String()) assert.Equal(t, - "\x1b[32mContainer at " + target + " is ready\n", - executeCommand(t, client, []string{"status"}, args), - "vespa status (the default)") - assert.Equal(t, target + "/ApplicationStatus", client.lastRequest.URL.String()) + "\x1b[32mContainer at "+target+" is ready\n", + executeCommand(t, client, []string{"status"}, args), + "vespa status (the default)") + assert.Equal(t, target+"/ApplicationStatus", client.lastRequest.URL.String()) } func assertContainerError(target string, args []string, t *testing.T) { - client := &mockHttpClient{ nextStatus: 500,} + client := &mockHttpClient{nextStatus: 500} assert.Equal(t, - "\x1b[31mContainer at " + target + " is not ready\n\x1b[33mStatus 500\n", - executeCommand(t, client, []string{"status", "container"}, args), - "vespa status container") -}
\ No newline at end of file + "\x1b[31mContainer at "+target+" is not ready\n\x1b[33mStatus 500\n", + executeCommand(t, client, []string{"status", "container"}, args), + "vespa status container") +} diff --git a/client/go/cmd/target.go b/client/go/cmd/target.go index 48ce71bba41..bf6487bf790 100644 --- a/client/go/cmd/target.go +++ b/client/go/cmd/target.go @@ -5,68 +5,69 @@ package cmd import ( - "github.com/vespa-engine/vespa/util" - "strings" + "github.com/vespa-engine/vespa/util" + "strings" ) type target struct { - deploy string - query string - document string + deploy string + query string + document string } type context int32 + const ( - deployContext context = 0 - queryContext context = 1 - documentContext context = 2 + deployContext context = 0 + queryContext context = 1 + documentContext context = 2 ) func deployTarget() string { - return getTarget(deployContext).deploy + return getTarget(deployContext).deploy } func queryTarget() string { - return getTarget(queryContext).query + return getTarget(queryContext).query } func documentTarget() string { - return getTarget(documentContext).document + return getTarget(documentContext).document } func getTarget(targetContext context) *target { - if strings.HasPrefix(targetArgument, "http") { - // TODO: Add default ports if missing - switch targetContext { - case deployContext: - return &target{ - deploy: targetArgument, - } - case queryContext: - return &target{ - query: targetArgument, - } - case documentContext: - return &target{ - document: targetArgument, - } - } - } + if strings.HasPrefix(targetArgument, "http") { + // TODO: Add default ports if missing + switch targetContext { + case deployContext: + return &target{ + deploy: targetArgument, + } + case queryContext: + return &target{ + query: targetArgument, + } + case documentContext: + return &target{ + document: targetArgument, + } + } + } - // Otherwise, target is a name + // Otherwise, target is a name - if targetArgument == "" || targetArgument == "local" { - return &target{ - deploy: "http://127.0.0.1:19071", - query: "http://127.0.0.1:8080", - document: "http://127.0.0.1:8080", - } - } + if targetArgument == "" || targetArgument == "local" { + return &target{ + deploy: "http://127.0.0.1:19071", + query: "http://127.0.0.1:8080", + document: "http://127.0.0.1:8080", + } + } - if targetArgument == "cloud" { - return nil // TODO - } + if targetArgument == "cloud" { + return nil // TODO + } - util.Error("Unknown target argument '" + targetArgument + ": Use 'local', 'cloud' or an URL") - return nil -}
\ No newline at end of file + util.Error("Unknown target argument '" + targetArgument + ": Use 'local', 'cloud' or an URL") + return nil +} diff --git a/client/go/main.go b/client/go/main.go index 3803ee357b2..85a9f94a59c 100644 --- a/client/go/main.go +++ b/client/go/main.go @@ -5,9 +5,9 @@ package main import ( - "github.com/vespa-engine/vespa/cmd" + "github.com/vespa-engine/vespa/cmd" ) func main() { - cmd.Execute() + cmd.Execute() } diff --git a/client/go/util/http.go b/client/go/util/http.go index 24e2416117c..6a4bab6c108 100644 --- a/client/go/util/http.go +++ b/client/go/util/http.go @@ -5,51 +5,51 @@ package util import ( - "net/http" - "net/url" - "strings" - "time" + "net/http" + "net/url" + "strings" + "time" ) // Set this to a mock HttpClient instead to unit test HTTP requests var ActiveHttpClient = CreateClient(time.Second * 10) type HttpClient interface { - Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) + Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) } type defaultHttpClient struct { - client *http.Client + client *http.Client } func (c *defaultHttpClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) { - if c.client.Timeout != timeout { // Create a new client with the right timeout - c.client = &http.Client{Timeout: timeout,} - } - return c.client.Do(request) + if c.client.Timeout != timeout { // Create a new client with the right timeout + c.client = &http.Client{Timeout: timeout} + } + return c.client.Do(request) } func CreateClient(timeout time.Duration) HttpClient { - return &defaultHttpClient{ - client: &http.Client{Timeout: timeout,}, - } + return &defaultHttpClient{ + client: &http.Client{Timeout: timeout}, + } } // Convenience function for doing a HTTP GET func HttpGet(host string, path string, description string) *http.Response { - url, urlError := url.Parse(host + path) - if urlError != nil { - Error("Invalid target url '" + host + path + "'") - return nil - } - return HttpDo(&http.Request{URL: url,}, time.Second * 10, description) + url, urlError := url.Parse(host + path) + if urlError != nil { + Error("Invalid target url '" + host + path + "'") + return nil + } + return HttpDo(&http.Request{URL: url}, time.Second*10, description) } func HttpDo(request *http.Request, timeout time.Duration, description string) *http.Response { - response, error := ActiveHttpClient.Do(request, timeout) - if error != nil { - Error("Could not connect to", strings.ToLower(description), "at", request.URL.Host) - Detail(error.Error()) - } - return response + response, error := ActiveHttpClient.Do(request, timeout) + if error != nil { + Error("Could not connect to", strings.ToLower(description), "at", request.URL.Host) + Detail(error.Error()) + } + return response } diff --git a/client/go/util/http_test.go b/client/go/util/http_test.go index 03b155488d9..731fc935a1d 100644 --- a/client/go/util/http_test.go +++ b/client/go/util/http_test.go @@ -5,42 +5,41 @@ package util import ( - "bytes" - "github.com/stretchr/testify/assert" + "bytes" + "github.com/stretchr/testify/assert" "io/ioutil" - "net/http" - "testing" - "time" + "net/http" + "testing" + "time" ) -type mockHttpClient struct {} +type mockHttpClient struct{} func (c mockHttpClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) { - var status int - var body string - if request.URL.String() == "http://host/okpath" { - status = 200 - body = "OK body" - } else { - status = 500 - body = "Unexpected url body" - } - - return &http.Response{ - StatusCode: status, - Header: make(http.Header), - Body: ioutil.NopCloser(bytes.NewBufferString(body)), - }, - nil + var status int + var body string + if request.URL.String() == "http://host/okpath" { + status = 200 + body = "OK body" + } else { + status = 500 + body = "Unexpected url body" + } + + return &http.Response{ + StatusCode: status, + Header: make(http.Header), + Body: ioutil.NopCloser(bytes.NewBufferString(body)), + }, + nil } func TestHttpRequest(t *testing.T) { - ActiveHttpClient = mockHttpClient{} + ActiveHttpClient = mockHttpClient{} - response := HttpGet("http://host", "/okpath", "description") - assert.Equal(t, 200, response.StatusCode) + response := HttpGet("http://host", "/okpath", "description") + assert.Equal(t, 200, response.StatusCode) - response = HttpGet("http://host", "/otherpath", "description") - assert.Equal(t, 500, response.StatusCode) + response = HttpGet("http://host", "/otherpath", "description") + assert.Equal(t, 500, response.StatusCode) } - diff --git a/client/go/util/io.go b/client/go/util/io.go index d7b849ba9a4..cbde22ad0eb 100644 --- a/client/go/util/io.go +++ b/client/go/util/io.go @@ -5,35 +5,35 @@ package util import ( - "bytes" - "errors" - "io" - "os" - "strings" + "bytes" + "errors" + "io" + "os" + "strings" ) // Returns true if the given path exists func PathExists(path string) bool { - _, err := os.Stat(path) - return ! errors.Is(err, os.ErrNotExist) + _, err := os.Stat(path) + return !errors.Is(err, os.ErrNotExist) } // Returns true is the given path points to an existing directory func IsDirectory(path string) bool { - info, err := os.Stat(path) - return ! errors.Is(err, os.ErrNotExist) && info.IsDir() + info, err := os.Stat(path) + return !errors.Is(err, os.ErrNotExist) && info.IsDir() } // Returns the content of a reader as a string func ReaderToString(reader io.Reader) string { - buffer := new(strings.Builder) - io.Copy(buffer, reader) - return buffer.String() + buffer := new(strings.Builder) + io.Copy(buffer, reader) + return buffer.String() } // Returns the content of a reader as a byte array func ReaderToBytes(reader io.Reader) []byte { - buffer := new(bytes.Buffer) + buffer := new(bytes.Buffer) buffer.ReadFrom(reader) return buffer.Bytes() } diff --git a/client/go/util/print.go b/client/go/util/print.go index 91a223a3c3a..6f6e40be07f 100644 --- a/client/go/util/print.go +++ b/client/go/util/print.go @@ -5,59 +5,59 @@ package util import ( - "bytes" - "encoding/json" - "fmt" - "io" - "os" + "bytes" + "encoding/json" + "fmt" + "io" + "os" ) // Set this to have output written somewhere else than os.Stdout var Out io.Writer func init() { - Out = os.Stdout + Out = os.Stdout } // Prints in default color func Print(messages ...string) { - print("", messages) + print("", messages) } // Prints in a color appropriate for errors func Error(messages ...string) { - print("\033[31m", messages) + print("\033[31m", messages) } // Prints in a color appropriate for success messages func Success(messages ...string) { - print("\033[32m", messages) + print("\033[32m", messages) } // Prints in a color appropriate for detail messages func Detail(messages ...string) { - print("\033[33m", messages) + print("\033[33m", messages) } // Prints all the text of the given reader func PrintReader(reader io.Reader) { - bodyBytes := ReaderToBytes(reader) - var prettyJSON bytes.Buffer - parseError := json.Indent(&prettyJSON, bodyBytes, "", " ") - if parseError != nil { // Not JSON: Print plainly - Print(string(bodyBytes)) - } else { - Print(string(prettyJSON.Bytes())) - } + bodyBytes := ReaderToBytes(reader) + var prettyJSON bytes.Buffer + parseError := json.Indent(&prettyJSON, bodyBytes, "", " ") + if parseError != nil { // Not JSON: Print plainly + Print(string(bodyBytes)) + } else { + Print(string(prettyJSON.Bytes())) + } } func print(prefix string, messages []string) { - fmt.Fprint(Out, prefix) - for i := 0; i < len(messages); i++ { - fmt.Fprint(Out, messages[i]) - if (i < len(messages) - 1) { - fmt.Fprint(Out, " ") - } - } - fmt.Fprintln(Out, "") + fmt.Fprint(Out, prefix) + for i := 0; i < len(messages); i++ { + fmt.Fprint(Out, messages[i]) + if i < len(messages)-1 { + fmt.Fprint(Out, " ") + } + } + fmt.Fprintln(Out, "") } diff --git a/client/go/vespa/deploy.go b/client/go/vespa/deploy.go index 3dd3fc21993..12312ef7fb8 100644 --- a/client/go/vespa/deploy.go +++ b/client/go/vespa/deploy.go @@ -5,133 +5,132 @@ package vespa import ( - "archive/zip" - "errors" - "github.com/vespa-engine/vespa/util" - "io" - "io/ioutil" - "net/http" - "net/url" - "os" - "path/filepath" - "strings" - "time" + "archive/zip" + "errors" + "github.com/vespa-engine/vespa/util" + "io" + "io/ioutil" + "net/http" + "net/url" + "os" + "path/filepath" + "strings" + "time" ) func Deploy(prepare bool, application string, target string) { - // TODO: Support no application (activate) - // TODO: Support application home as argument instead of src/main and - // - if target exists, use target/application.zip - // - else if src/main/application exists, use that - // - else if current dir has services.xml use that - if filepath.Ext(application) != ".zip" { - tempZip, error := ioutil.TempFile("", "application.zip") - if error != nil { - util.Error("Could not create a temporary zip file for the application package") - util.Detail(error.Error()) - return - } + // TODO: Support no application (activate) + // TODO: Support application home as argument instead of src/main and + // - if target exists, use target/application.zip + // - else if src/main/application exists, use that + // - else if current dir has services.xml use that + if filepath.Ext(application) != ".zip" { + tempZip, error := ioutil.TempFile("", "application.zip") + if error != nil { + util.Error("Could not create a temporary zip file for the application package") + util.Detail(error.Error()) + return + } - error = zipDir(application, tempZip.Name()) - if (error != nil) { - util.Error(error.Error()) - return - } - defer os.Remove(tempZip.Name()) - application = tempZip.Name() - } + error = zipDir(application, tempZip.Name()) + if error != nil { + util.Error(error.Error()) + return + } + defer os.Remove(tempZip.Name()) + application = tempZip.Name() + } - zipFileReader, zipFileError := os.Open(application) - if zipFileError != nil { - util.Error("Could not open application package at " + application) - util.Detail(zipFileError.Error()) - return - } + zipFileReader, zipFileError := os.Open(application) + if zipFileError != nil { + util.Error("Could not open application package at " + application) + util.Detail(zipFileError.Error()) + return + } - var deployUrl *url.URL - if prepare { - deployUrl, _ = url.Parse(target + "/application/v2/tenant/default/prepare") - } else if application == "" { - deployUrl, _ = url.Parse(target + "/application/v2/tenant/default/activate") - } else { - deployUrl, _ = url.Parse(target + "/application/v2/tenant/default/prepareandactivate") - } + var deployUrl *url.URL + if prepare { + deployUrl, _ = url.Parse(target + "/application/v2/tenant/default/prepare") + } else if application == "" { + deployUrl, _ = url.Parse(target + "/application/v2/tenant/default/activate") + } else { + deployUrl, _ = url.Parse(target + "/application/v2/tenant/default/prepareandactivate") + } - header := http.Header{} - header.Add("Content-Type", "application/zip") - request := &http.Request{ - URL: deployUrl, - Method: "POST", - Header: header, - Body: ioutil.NopCloser(zipFileReader), - } - serviceDescription := "Deploy service" - response := util.HttpDo(request, time.Minute * 10, serviceDescription) - if (response == nil) { - return - } + header := http.Header{} + header.Add("Content-Type", "application/zip") + request := &http.Request{ + URL: deployUrl, + Method: "POST", + Header: header, + Body: ioutil.NopCloser(zipFileReader), + } + serviceDescription := "Deploy service" + response := util.HttpDo(request, time.Minute*10, serviceDescription) + if response == nil { + return + } - defer response.Body.Close() - if response.StatusCode == 200 { - util.Success("Success") - } else if response.StatusCode / 100 == 4 { - util.Error("Invalid application package", "(" + response.Status + "):") - util.PrintReader(response.Body) - } else { - util.Error("Error from", strings.ToLower(serviceDescription), "at", request.URL.Host, "(" + response.Status + "):") - util.PrintReader(response.Body) - } + defer response.Body.Close() + if response.StatusCode == 200 { + util.Success("Success") + } else if response.StatusCode/100 == 4 { + util.Error("Invalid application package", "("+response.Status+"):") + util.PrintReader(response.Body) + } else { + util.Error("Error from", strings.ToLower(serviceDescription), "at", request.URL.Host, "("+response.Status+"):") + util.PrintReader(response.Body) + } } func zipDir(dir string, destination string) error { - if filepath.IsAbs(dir) { - message := "Path must be relative, but '" + dir + "'" - return errors.New(message) - } - if ! util.PathExists(dir) { - message := "'" + dir + "' should be an application package zip or dir, but does not exist" - return errors.New(message) - } - if ! util.IsDirectory(dir) { - message := "'" + dir + "' should be an application package dir, but is a (non-zip) file" - return errors.New(message) - } + if filepath.IsAbs(dir) { + message := "Path must be relative, but '" + dir + "'" + return errors.New(message) + } + if !util.PathExists(dir) { + message := "'" + dir + "' should be an application package zip or dir, but does not exist" + return errors.New(message) + } + if !util.IsDirectory(dir) { + message := "'" + dir + "' should be an application package dir, but is a (non-zip) file" + return errors.New(message) + } - file, err := os.Create(destination) - if err != nil { - message := "Could not create a temporary zip file for the application package: " + err.Error() - return errors.New(message) - } - defer file.Close() + file, err := os.Create(destination) + if err != nil { + message := "Could not create a temporary zip file for the application package: " + err.Error() + return errors.New(message) + } + defer file.Close() - w := zip.NewWriter(file) - defer w.Close() + w := zip.NewWriter(file) + defer w.Close() - walker := func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if info.IsDir() { - return nil - } - file, err := os.Open(path) - if err != nil { - return err - } - defer file.Close() + walker := func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() - zippath := strings.TrimPrefix(path, dir) - zipfile, err := w.Create(zippath) - if err != nil { - return err - } + zippath := strings.TrimPrefix(path, dir) + zipfile, err := w.Create(zippath) + if err != nil { + return err + } - _, err = io.Copy(zipfile, file) - if err != nil { - return err - } - return nil - } - return filepath.Walk(dir, walker) + _, err = io.Copy(zipfile, file) + if err != nil { + return err + } + return nil + } + return filepath.Walk(dir, walker) } - |