diff options
author | Martin Polden <mpolden@mpolden.no> | 2023-11-29 11:05:57 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2023-11-29 16:12:02 +0100 |
commit | 7357f331d12532f3f039804c9a5c136bec604698 (patch) | |
tree | a5e3c36b04c8529757961936bc8945d45ca6f399 /client | |
parent | 8a37c49b869d4bf5e4ee0239c512200dc9663ecd (diff) |
Extract ioutil package
Diffstat (limited to 'client')
26 files changed, 112 insertions, 104 deletions
diff --git a/client/go/internal/admin/jvm/env.go b/client/go/internal/admin/jvm/env.go index 5c1e938d46b..6fcd39a5a1b 100644 --- a/client/go/internal/admin/jvm/env.go +++ b/client/go/internal/admin/jvm/env.go @@ -11,6 +11,7 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/admin/envvars" "github.com/vespa-engine/vespa/client/go/internal/admin/prog" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/util" ) @@ -33,7 +34,7 @@ func (opts *Options) exportEnvSettings(ps *prog.Spec) { if preload := ps.Getenv(envvars.PRELOAD); preload != "" { checked := []string{} for _, fileName := range strings.Split(preload, ":") { - if util.PathExists(fileName) { + if ioutil.Exists(fileName) { checked = append(checked, fileName) } else { trace.Info("File in PRELOAD missing, skipped:", fileName) diff --git a/client/go/internal/admin/jvm/standalone_container.go b/client/go/internal/admin/jvm/standalone_container.go index 859aea9157d..542304c099a 100644 --- a/client/go/internal/admin/jvm/standalone_container.go +++ b/client/go/internal/admin/jvm/standalone_container.go @@ -9,6 +9,7 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/admin/defaults" "github.com/vespa-engine/vespa/client/go/internal/admin/envvars" "github.com/vespa-engine/vespa/client/go/internal/admin/prog" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/util" ) @@ -78,7 +79,7 @@ func (a *StandaloneContainer) addJdiscProperties() { func (c *StandaloneContainer) exportExtraEnv(ps *prog.Spec) { vespaHome := defaults.VespaHome() app := fmt.Sprintf("%s/conf/%s-app", vespaHome, c.ServiceName()) - if util.IsDirectory(app) { + if ioutil.IsDir(app) { ps.Setenv(envvars.STANDALONE_JDISC_APP_LOCATION, app) } else { util.JustExitMsg("standalone container requires an application directory, missing: " + app) diff --git a/client/go/internal/admin/vespa-wrapper/configserver/runserver.go b/client/go/internal/admin/vespa-wrapper/configserver/runserver.go index 25935c814e7..f5b5e0ac204 100644 --- a/client/go/internal/admin/vespa-wrapper/configserver/runserver.go +++ b/client/go/internal/admin/vespa-wrapper/configserver/runserver.go @@ -8,6 +8,7 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/admin/defaults" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/util" ) @@ -27,11 +28,11 @@ func (rs *RunServer) PidFile() string { func (rs *RunServer) ProgPath() string { p := fmt.Sprintf("%s/bin64/%s", defaults.VespaHome(), PROG_NAME) - if util.IsExecutableFile(p) { + if ioutil.IsExecutable(p) { return p } p = fmt.Sprintf("%s/bin/%s", defaults.VespaHome(), PROG_NAME) - if util.IsExecutableFile(p) { + if ioutil.IsExecutable(p) { return p } panic(fmt.Errorf("not an executable file: %s", p)) diff --git a/client/go/internal/admin/vespa-wrapper/startcbinary/cmd.go b/client/go/internal/admin/vespa-wrapper/startcbinary/cmd.go index 8902aef80df..657c941ad51 100644 --- a/client/go/internal/admin/vespa-wrapper/startcbinary/cmd.go +++ b/client/go/internal/admin/vespa-wrapper/startcbinary/cmd.go @@ -9,6 +9,7 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/admin/envvars" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/util" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -34,12 +35,12 @@ func Run(args []string) int { func IsCandidate(program string) bool { binary := program + "-bin" if strings.Contains(binary, "/") { - return util.IsRegularFile(binary) + return ioutil.IsFile(binary) } else { path := strings.Split(os.Getenv(envvars.PATH), ":") for _, dir := range path { fn := dir + "/" + binary - if util.IsRegularFile(fn) { + if ioutil.IsFile(fn) { return true } } diff --git a/client/go/internal/cli/cmd/api_key.go b/client/go/internal/cli/cmd/api_key.go index 133c9db0d3b..d882c527516 100644 --- a/client/go/internal/cli/cmd/api_key.go +++ b/client/go/internal/cli/cmd/api_key.go @@ -10,7 +10,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -71,7 +71,7 @@ func doApiKey(cli *CLI, overwriteKey bool, args []string) error { return err } apiKeyFile := cli.config.apiKeyPath(app.Tenant) - if util.PathExists(apiKeyFile) && !overwriteKey { + if ioutil.Exists(apiKeyFile) && !overwriteKey { err := fmt.Errorf("refusing to overwrite %s", apiKeyFile) cli.printErr(err, "Use -f to overwrite it") printPublicKey(system, apiKeyFile, app.Tenant) diff --git a/client/go/internal/cli/cmd/cert.go b/client/go/internal/cli/cmd/cert.go index 6aa99a01902..3e18fafb815 100644 --- a/client/go/internal/cli/cmd/cert.go +++ b/client/go/internal/cli/cmd/cert.go @@ -10,7 +10,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -122,10 +122,10 @@ func doCert(cli *CLI, overwriteCertificate, skipApplicationPackage bool, args [] if !overwriteCertificate { hint := "Use -f flag to force overwriting" - if util.PathExists(privateKeyFile.path) { + if ioutil.Exists(privateKeyFile.path) { return errHint(fmt.Errorf("private key %s already exists", color.CyanString(privateKeyFile.path)), hint) } - if util.PathExists(certificateFile.path) { + if ioutil.Exists(certificateFile.path) { return errHint(fmt.Errorf("certificate %s already exists", color.CyanString(certificateFile.path)), hint) } } @@ -213,7 +213,7 @@ func copyCertificate(cli *CLI, target vespa.Target, pkg vespa.ApplicationPackage if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil { return fmt.Errorf("could not create security directory: %w", err) } - err = util.AtomicWriteFile(dstPath, data) + err = ioutil.AtomicWriteFile(dstPath, data) if err == nil { cli.printSuccess("Copied certificate from ", tlsOptions.CertificateFile, " to ", dstPath) } diff --git a/client/go/internal/cli/cmd/clone_test.go b/client/go/internal/cli/cmd/clone_test.go index 331845b3883..e783f75d9d4 100644 --- a/client/go/internal/cli/cmd/clone_test.go +++ b/client/go/internal/cli/cmd/clone_test.go @@ -13,8 +13,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/mock" - "github.com/vespa-engine/vespa/client/go/internal/util" ) func TestClone(t *testing.T) { @@ -101,9 +101,9 @@ func TestClone(t *testing.T) { func assertFiles(t *testing.T, app string) { t.Helper() - 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"))) + assert.True(t, ioutil.Exists(filepath.Join(app, "README.md"))) + assert.True(t, ioutil.Exists(filepath.Join(app, "src", "main", "application"))) + assert.True(t, ioutil.IsDir(filepath.Join(app, "src", "main", "application"))) servicesStat, err := os.Stat(filepath.Join(app, "src", "main", "application", "services.xml")) require.Nil(t, err) diff --git a/client/go/internal/cli/cmd/document.go b/client/go/internal/cli/cmd/document.go index 72b853de94e..0393a9b2595 100644 --- a/client/go/internal/cli/cmd/document.go +++ b/client/go/internal/cli/cmd/document.go @@ -16,7 +16,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" "github.com/vespa-engine/vespa/client/go/internal/httputil" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/vespa" "github.com/vespa-engine/vespa/client/go/internal/vespa/document" ) @@ -111,15 +111,15 @@ func operationResult(read bool, doc document.Document, service *vespa.Service, r bodyReader := bytes.NewReader(result.Body) if result.HTTPStatus == 200 { if read { - return SuccessWithPayload("Read "+doc.Id.String(), util.ReaderToJSON(bodyReader)) + return SuccessWithPayload("Read "+doc.Id.String(), ioutil.ReaderToJSON(bodyReader)) } else { return Success(doc.Operation.String() + " " + doc.Id.String()) } } if result.HTTPStatus/100 == 4 { - return FailureWithPayload("Invalid document operation: Status "+strconv.Itoa(result.HTTPStatus), util.ReaderToJSON(bodyReader)) + return FailureWithPayload("Invalid document operation: Status "+strconv.Itoa(result.HTTPStatus), ioutil.ReaderToJSON(bodyReader)) } - return FailureWithPayload(service.Description()+" at "+service.BaseURL+": Status "+strconv.Itoa(result.HTTPStatus), util.ReaderToJSON(bodyReader)) + return FailureWithPayload(service.Description()+" at "+service.BaseURL+": Status "+strconv.Itoa(result.HTTPStatus), ioutil.ReaderToJSON(bodyReader)) } func newDocumentCmd(cli *CLI) *cobra.Command { diff --git a/client/go/internal/cli/cmd/document_test.go b/client/go/internal/cli/cmd/document_test.go index 0b8d5a50615..3cfc66fdad4 100644 --- a/client/go/internal/cli/cmd/document_test.go +++ b/client/go/internal/cli/cmd/document_test.go @@ -12,8 +12,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/mock" - "github.com/vespa-engine/vespa/client/go/internal/util" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -148,7 +148,7 @@ func assertDocumentSend(args []string, expectedOperation string, expectedMethod Fields json.RawMessage `json:"fields"` } assert.Nil(t, json.Unmarshal(data, &expectedPayload)) - assert.Equal(t, `{"fields":`+string(expectedPayload.Fields)+"}", util.ReaderToString(client.LastRequest.Body)) + assert.Equal(t, `{"fields":`+string(expectedPayload.Fields)+"}", ioutil.ReaderToString(client.LastRequest.Body)) } else { assert.Nil(t, client.LastRequest.Body) } diff --git a/client/go/internal/cli/cmd/man_test.go b/client/go/internal/cli/cmd/man_test.go index ad05efcb2a3..ae434624ac7 100644 --- a/client/go/internal/cli/cmd/man_test.go +++ b/client/go/internal/cli/cmd/man_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" ) func TestMan(t *testing.T) { @@ -15,5 +15,5 @@ func TestMan(t *testing.T) { cli, stdout, _ := newTestCLI(t) assert.Nil(t, cli.Run("man", tmpDir)) assert.Equal(t, fmt.Sprintf("Success: Man pages written to %s\n", tmpDir), stdout.String()) - assert.True(t, util.PathExists(filepath.Join(tmpDir, "vespa.1"))) + assert.True(t, ioutil.Exists(filepath.Join(tmpDir, "vespa.1"))) } diff --git a/client/go/internal/cli/cmd/prod.go b/client/go/internal/cli/cmd/prod.go index ddf2995126a..0912ca31e25 100644 --- a/client/go/internal/cli/cmd/prod.go +++ b/client/go/internal/cli/cmd/prod.go @@ -15,7 +15,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/vespa" "github.com/vespa-engine/vespa/client/go/internal/vespa/xml" ) @@ -187,7 +187,7 @@ $ vespa prod deploy`, func writeWithBackup(stdout io.Writer, pkg vespa.ApplicationPackage, filename, contents string) error { dst := filepath.Join(pkg.Path, filename) - if util.PathExists(dst) { + if ioutil.Exists(dst) { data, err := os.ReadFile(dst) if err != nil { return err @@ -199,7 +199,7 @@ func writeWithBackup(stdout io.Writer, pkg vespa.ApplicationPackage, filename, c renamed := false for i := 1; i <= 1000; i++ { bak := fmt.Sprintf("%s.%d.bak", dst, i) - if !util.PathExists(bak) { + if !ioutil.Exists(bak) { fmt.Fprintf(stdout, "Backing up existing %s to %s\n", color.YellowString(filename), color.YellowString(bak)) if err := os.Rename(dst, bak); err != nil { return err diff --git a/client/go/internal/cli/cmd/prod_test.go b/client/go/internal/cli/cmd/prod_test.go index 7f2836125d8..6d8a50124ac 100644 --- a/client/go/internal/cli/cmd/prod_test.go +++ b/client/go/internal/cli/cmd/prod_test.go @@ -10,8 +10,8 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/mock" - "github.com/vespa-engine/vespa/client/go/internal/util" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -76,8 +76,8 @@ func TestProdInit(t *testing.T) { assert.Contains(t, servicesXML, contentFragment) // Backups are created - assert.True(t, util.PathExists(deploymentPath+".1.bak")) - assert.True(t, util.PathExists(servicesPath+".1.bak")) + assert.True(t, ioutil.Exists(deploymentPath+".1.bak")) + assert.True(t, ioutil.Exists(servicesPath+".1.bak")) } func readFileString(t *testing.T, filename string) string { diff --git a/client/go/internal/cli/cmd/query.go b/client/go/internal/cli/cmd/query.go index bf2272ca981..3e5a60a15df 100644 --- a/client/go/internal/cli/cmd/query.go +++ b/client/go/internal/cli/cmd/query.go @@ -16,7 +16,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" "github.com/vespa-engine/vespa/client/go/internal/curl" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -98,11 +98,11 @@ func query(cli *CLI, arguments []string, timeoutSecs, waitSecs int, curl bool) e defer response.Body.Close() if response.StatusCode == 200 { - log.Print(util.ReaderToJSON(response.Body)) + log.Print(ioutil.ReaderToJSON(response.Body)) } else if response.StatusCode/100 == 4 { - return fmt.Errorf("invalid query: %s\n%s", response.Status, util.ReaderToJSON(response.Body)) + return fmt.Errorf("invalid query: %s\n%s", response.Status, ioutil.ReaderToJSON(response.Body)) } else { - return fmt.Errorf("%s from container at %s\n%s", response.Status, color.CyanString(url.Host), util.ReaderToJSON(response.Body)) + return fmt.Errorf("%s from container at %s\n%s", response.Status, color.CyanString(url.Host), ioutil.ReaderToJSON(response.Body)) } return nil } diff --git a/client/go/internal/cli/cmd/test.go b/client/go/internal/cli/cmd/test.go index 81bf37c5653..3bc78fc91c8 100644 --- a/client/go/internal/cli/cmd/test.go +++ b/client/go/internal/cli/cmd/test.go @@ -22,7 +22,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" "github.com/vespa-engine/vespa/client/go/internal/httputil" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -295,7 +295,7 @@ func verify(step step, defaultCluster string, defaultParameters map[string]strin color.RedString(strconv.Itoa(response.StatusCode)), color.CyanString(method), color.CyanString(requestUrl.String()), - util.ReaderToJSON(response.Body)), nil + ioutil.ReaderToJSON(response.Body)), nil } if responseBodySpec == nil { diff --git a/client/go/internal/cli/cmd/test_test.go b/client/go/internal/cli/cmd/test_test.go index 1888db017d4..728e8c29691 100644 --- a/client/go/internal/cli/cmd/test_test.go +++ b/client/go/internal/cli/cmd/test_test.go @@ -15,8 +15,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/mock" - "github.com/vespa-engine/vespa/client/go/internal/util" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -205,6 +205,6 @@ func assertRequests(requests []*http.Request, client *mock.HTTPClient, t *testin if actualBody == nil { actualBody = io.NopCloser(strings.NewReader("")) } - assert.Equal(t, util.ReaderToJSON(want.Body), util.ReaderToJSON(actualBody)) + assert.Equal(t, ioutil.ReaderToJSON(want.Body), ioutil.ReaderToJSON(actualBody)) } } diff --git a/client/go/internal/cli/cmd/visit.go b/client/go/internal/cli/cmd/visit.go index 6ac1b3b231f..963833337c2 100644 --- a/client/go/internal/cli/cmd/visit.go +++ b/client/go/internal/cli/cmd/visit.go @@ -16,7 +16,7 @@ import ( "time" "github.com/spf13/cobra" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/vespa" ) @@ -244,7 +244,7 @@ func probeHandler(service *vespa.Service, cli *CLI) (res OperationResult) { cli.printWarning("Missing /document/v1/ API; add <document-api /> to the container cluster declaration in services.xml") return Failure("Missing /document/v1 API") } else { - return FailureWithPayload(service.Description()+" at "+request.URL.Host+": "+response.Status, util.ReaderToJSON(response.Body)) + return FailureWithPayload(service.Description()+" at "+request.URL.Host+": "+response.Status, ioutil.ReaderToJSON(response.Body)) } } @@ -397,9 +397,9 @@ func runOneVisit(vArgs *visitArgs, service *vespa.Service, contToken string) (*V return nil, Failure("error reading response: " + err.Error()) } } else if response.StatusCode/100 == 4 { - return vvo, FailureWithPayload("Invalid document operation: "+response.Status, util.ReaderToJSON(response.Body)) + return vvo, FailureWithPayload("Invalid document operation: "+response.Status, ioutil.ReaderToJSON(response.Body)) } else { - return vvo, FailureWithPayload(service.Description()+" at "+request.URL.Host+": "+response.Status, util.ReaderToJSON(response.Body)) + return vvo, FailureWithPayload(service.Description()+" at "+request.URL.Host+": "+response.Status, ioutil.ReaderToJSON(response.Body)) } } diff --git a/client/go/internal/util/io.go b/client/go/internal/ioutil/ioutil.go index 9e755737035..d3a33698d13 100644 --- a/client/go/internal/util/io.go +++ b/client/go/internal/ioutil/ioutil.go @@ -2,7 +2,7 @@ // File utilities. // Author: bratseth -package util +package ioutil import ( "bytes" @@ -14,26 +14,26 @@ import ( "strings" ) -// Returns true if the given path exists -func PathExists(path string) bool { +// Exists returns true if the given path exists. +func Exists(path string) bool { info, err := os.Stat(path) return !errors.Is(err, os.ErrNotExist) && info != nil } -// Returns true if the given path points to an existing directory -func IsDirectory(path string) bool { +// IsDir returns true if the given path points to an existing directory. +func IsDir(path string) bool { info, err := os.Stat(path) return !errors.Is(err, os.ErrNotExist) && info != nil && info.IsDir() } -// Returns true if the given path points to an existing file -func IsRegularFile(path string) bool { +// IsFile returns true if the given path points to an existing regular file. +func IsFile(path string) bool { info, err := os.Stat(path) return !errors.Is(err, os.ErrNotExist) && info != nil && info.Mode().IsRegular() } -// Returns true if the given path points to an executable -func IsExecutableFile(path string) bool { +// IsExecutable returns true if the given path points to an executable file. +func IsExecutable(path string) bool { info, err := os.Stat(path) return !errors.Is(err, os.ErrNotExist) && info != nil && @@ -41,21 +41,21 @@ func IsExecutableFile(path string) bool { ((int(info.Mode()) & 0111) == 0111) } -// Returns the content of a reader as a string +// ReaderToString Returns the content of reader as a string. Read errors are ignored. func ReaderToString(reader io.Reader) string { var buffer strings.Builder io.Copy(&buffer, reader) return buffer.String() } -// Returns the content of a reader as a byte array +// ReaderToBytes returns the content of a reader as a byte array. Read errors are ignored. func ReaderToBytes(reader io.Reader) []byte { var buffer bytes.Buffer buffer.ReadFrom(reader) return buffer.Bytes() } -// Returns the contents of reader as indented JSON +// ReaderToJSON returns the contents of reader as indented JSON. Read errors are ignored. func ReaderToJSON(reader io.Reader) string { bodyBytes, _ := io.ReadAll(reader) var prettyJSON bytes.Buffer diff --git a/client/go/internal/util/io_test.go b/client/go/internal/ioutil/ioutil_test.go index 0b2ad0f081b..907132c9eaa 100644 --- a/client/go/internal/util/io_test.go +++ b/client/go/internal/ioutil/ioutil_test.go @@ -1,5 +1,5 @@ // Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package util +package ioutil import ( "os" @@ -9,46 +9,46 @@ import ( ) func TestPathExists(t *testing.T) { - assert.Equal(t, true, PathExists("io.go")) - assert.Equal(t, false, PathExists("nosuchthing.go")) + assert.Equal(t, true, Exists("ioutil.go")) + assert.Equal(t, false, Exists("nosuchthing.go")) tmpDir := t.TempDir() err := os.MkdirAll(tmpDir+"/no", 0755) assert.Nil(t, err) err = os.MkdirAll(tmpDir+"/no/such", 0) assert.Nil(t, err) - assert.Equal(t, false, PathExists(tmpDir+"/no/such/thing.go")) + assert.Equal(t, false, Exists(tmpDir+"/no/such/thing.go")) } func TestIsDir(t *testing.T) { tmpDir := t.TempDir() err := os.MkdirAll(tmpDir+"/no", 0755) assert.Nil(t, err) - assert.Equal(t, true, IsDirectory(tmpDir+"/no")) + assert.Equal(t, true, IsDir(tmpDir+"/no")) err = os.MkdirAll(tmpDir+"/no/such", 0) assert.Nil(t, err) - assert.Equal(t, true, IsDirectory(tmpDir+"/no/such")) - assert.Equal(t, false, IsDirectory(tmpDir+"/no/such/thing.go")) + assert.Equal(t, true, IsDir(tmpDir+"/no/such")) + assert.Equal(t, false, IsDir(tmpDir+"/no/such/thing.go")) } func TestIsRegularFile(t *testing.T) { - assert.Equal(t, true, IsRegularFile("io.go")) - assert.Equal(t, false, IsRegularFile(".")) + assert.Equal(t, true, IsFile("ioutil.go")) + assert.Equal(t, false, IsFile(".")) tmpDir := t.TempDir() err := os.MkdirAll(tmpDir+"/no", 0755) assert.Nil(t, err) err = os.MkdirAll(tmpDir+"/no/such", 0) assert.Nil(t, err) - assert.Equal(t, false, IsRegularFile(tmpDir+"/no/such/thing.go")) + assert.Equal(t, false, IsFile(tmpDir+"/no/such/thing.go")) } func TestIsExecutableFile(t *testing.T) { - assert.Equal(t, false, IsExecutableFile("io.go")) - assert.Equal(t, false, IsExecutableFile("nosuchthing.go")) + assert.Equal(t, false, IsExecutable("io.go")) + assert.Equal(t, false, IsExecutable("nosuchthing.go")) tmpDir := t.TempDir() err := os.WriteFile(tmpDir+"/run.sh", []byte("#!/bin/sh\necho foo\n"), 0755) assert.Nil(t, err) - assert.Equal(t, true, IsExecutableFile(tmpDir+"/run.sh")) + assert.Equal(t, true, IsExecutable(tmpDir+"/run.sh")) /* unix only: out, err := BackTicksWithStderr.Run(tmpDir + "/run.sh") assert.Nil(t, err) diff --git a/client/go/internal/util/execvp.go b/client/go/internal/util/execvp.go index 38514696365..418eb3d8f6c 100644 --- a/client/go/internal/util/execvp.go +++ b/client/go/internal/util/execvp.go @@ -12,6 +12,7 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/admin/envvars" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "golang.org/x/sys/unix" ) @@ -22,7 +23,7 @@ func findInPath(prog string) string { path := strings.Split(os.Getenv(envvars.PATH), ":") for _, dir := range path { fn := dir + "/" + prog - if IsExecutableFile(fn) { + if ioutil.IsExecutable(fn) { return fn } } diff --git a/client/go/internal/util/fix_fs_test.go b/client/go/internal/util/fix_fs_test.go index 0ecf2e06535..535da2c80ad 100644 --- a/client/go/internal/util/fix_fs_test.go +++ b/client/go/internal/util/fix_fs_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" ) func setup(t *testing.T) string { @@ -36,15 +37,15 @@ func testFixSpec(t *testing.T, spec FixSpec) { spec.FixFile(tmpDir + "/a/f3") spec.FixFile(tmpDir + "/b/f4") spec.FixFile(tmpDir + "/a/bad/f5") - assert.Equal(t, true, IsDirectory(tmpDir+"/a")) - assert.Equal(t, true, IsDirectory(tmpDir+"/b")) - assert.Equal(t, true, IsDirectory(tmpDir+"/a/bad")) - assert.Equal(t, true, IsDirectory(tmpDir+"/a/bad/ok")) - assert.Equal(t, true, IsRegularFile(tmpDir+"/a/f1")) - assert.Equal(t, true, IsRegularFile(tmpDir+"/a/f2")) - assert.Equal(t, false, IsRegularFile(tmpDir+"/a/f3")) - assert.Equal(t, false, IsRegularFile(tmpDir+"/b/f4")) - assert.Equal(t, false, IsRegularFile(tmpDir+"/a/bad/f5")) + assert.Equal(t, true, ioutil.IsDir(tmpDir+"/a")) + assert.Equal(t, true, ioutil.IsDir(tmpDir+"/b")) + assert.Equal(t, true, ioutil.IsDir(tmpDir+"/a/bad")) + assert.Equal(t, true, ioutil.IsDir(tmpDir+"/a/bad/ok")) + assert.Equal(t, true, ioutil.IsFile(tmpDir+"/a/f1")) + assert.Equal(t, true, ioutil.IsFile(tmpDir+"/a/f2")) + assert.Equal(t, false, ioutil.IsFile(tmpDir+"/a/f3")) + assert.Equal(t, false, ioutil.IsFile(tmpDir+"/b/f4")) + assert.Equal(t, false, ioutil.IsFile(tmpDir+"/a/bad/f5")) info, err := os.Stat(tmpDir + "/a") assert.Nil(t, err) diff --git a/client/go/internal/vespa/application.go b/client/go/internal/vespa/application.go index 6d28b24100f..5d1ab610e38 100644 --- a/client/go/internal/vespa/application.go +++ b/client/go/internal/vespa/application.go @@ -10,7 +10,7 @@ import ( "path/filepath" "strings" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" ) type ApplicationPackage struct { @@ -24,7 +24,7 @@ func (ap *ApplicationPackage) HasDeploymentSpec() bool { return ap.hasFile("depl func (ap *ApplicationPackage) hasFile(pathSegment ...string) bool { if !ap.IsZip() { - return util.PathExists(filepath.Join(append([]string{ap.Path}, pathSegment...)...)) + return ioutil.Exists(filepath.Join(append([]string{ap.Path}, pathSegment...)...)) } zipName := filepath.Join(pathSegment...) return ap.hasZipEntry(func(name string) bool { return zipName == name }) @@ -50,7 +50,7 @@ func (ap *ApplicationPackage) IsJava() bool { if ap.IsZip() { return ap.hasZipEntry(func(name string) bool { return filepath.Ext(name) == ".jar" }) } - return util.PathExists(filepath.Join(ap.Path, "pom.xml")) + return ioutil.Exists(filepath.Join(ap.Path, "pom.xml")) } func (ap *ApplicationPackage) Validate() error { @@ -74,11 +74,11 @@ func (ap *ApplicationPackage) Validate() error { func isZip(filename string) bool { return filepath.Ext(filename) == ".zip" } func zipDir(dir string, destination string) error { - if !util.PathExists(dir) { + if !ioutil.Exists(dir) { message := "'" + dir + "' should be an application package zip or dir, but does not exist" return errors.New(message) } - if !util.IsDirectory(dir) { + if !ioutil.IsDir(dir) { message := "'" + dir + "' should be an application package dir, but is a (non-zip) file" return errors.New(message) } @@ -267,10 +267,10 @@ func findApplicationPackage(zipOrDir string, options PackageOptions) (Applicatio } // Pre-packaged application. We prefer the uncompressed application because this allows us to add // security/clients.pem to the package on-demand - hasPOM := util.PathExists(filepath.Join(zipOrDir, "pom.xml")) + hasPOM := ioutil.Exists(filepath.Join(zipOrDir, "pom.xml")) if hasPOM && !options.SourceOnly { path := filepath.Join(zipOrDir, "target", "application") - if util.PathExists(path) { + if ioutil.Exists(path) { testPath := existingPath(filepath.Join(zipOrDir, "target", "application-test")) return ApplicationPackage{Path: path, TestPath: testPath}, nil } @@ -279,14 +279,14 @@ func findApplicationPackage(zipOrDir string, options PackageOptions) (Applicatio } } // Application with Maven directory structure, but with no POM or no hard requirement on packaging - if path := filepath.Join(zipOrDir, "src", "main", "application"); util.PathExists(path) { + if path := filepath.Join(zipOrDir, "src", "main", "application"); ioutil.Exists(path) { testPath := existingPath(filepath.Join(zipOrDir, "src", "test", "application")) return ApplicationPackage{Path: path, TestPath: testPath}, nil } // Application without Java components - if util.PathExists(filepath.Join(zipOrDir, "services.xml")) { + if ioutil.Exists(filepath.Join(zipOrDir, "services.xml")) { testPath := "" - if util.PathExists(filepath.Join(zipOrDir, "tests")) { + if ioutil.Exists(filepath.Join(zipOrDir, "tests")) { testPath = zipOrDir } return ApplicationPackage{Path: zipOrDir, TestPath: testPath}, nil @@ -295,7 +295,7 @@ func findApplicationPackage(zipOrDir string, options PackageOptions) (Applicatio } func existingPath(path string) string { - if util.PathExists(path) { + if ioutil.Exists(path) { return path } return "" diff --git a/client/go/internal/vespa/crypto.go b/client/go/internal/vespa/crypto.go index 13d3ac570cc..9b4d776d97d 100644 --- a/client/go/internal/vespa/crypto.go +++ b/client/go/internal/vespa/crypto.go @@ -20,7 +20,7 @@ import ( "strings" "time" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" ) const ( @@ -36,18 +36,18 @@ type PemKeyPair struct { // WriteCertificateFile writes the certificate contained in this key pair to certificateFile. func (kp *PemKeyPair) WriteCertificateFile(certificateFile string, overwrite bool) error { - if util.PathExists(certificateFile) && !overwrite { + if ioutil.Exists(certificateFile) && !overwrite { return fmt.Errorf("cannot overwrite existing file: %s", certificateFile) } - return util.AtomicWriteFile(certificateFile, kp.Certificate) + return ioutil.AtomicWriteFile(certificateFile, kp.Certificate) } // WritePrivateKeyFile writes the private key contained in this key pair to privateKeyFile. func (kp *PemKeyPair) WritePrivateKeyFile(privateKeyFile string, overwrite bool) error { - if util.PathExists(privateKeyFile) && !overwrite { + if ioutil.Exists(privateKeyFile) && !overwrite { return fmt.Errorf("cannot overwrite existing file: %s", privateKeyFile) } - return util.AtomicWriteFile(privateKeyFile, kp.PrivateKey) + return ioutil.AtomicWriteFile(privateKeyFile, kp.PrivateKey) } // CreateKeyPair creates a key pair containing a private key and self-signed X509 certificate. diff --git a/client/go/internal/vespa/deploy.go b/client/go/internal/vespa/deploy.go index 35fd523f15a..10ddb321e19 100644 --- a/client/go/internal/vespa/deploy.go +++ b/client/go/internal/vespa/deploy.go @@ -18,7 +18,7 @@ import ( "strings" "time" - "github.com/vespa-engine/vespa/client/go/internal/util" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/version" ) @@ -120,10 +120,10 @@ func ZoneFromString(s string) (ZoneID, error) { } func Fetch(deployment DeploymentOptions, path string) (string, error) { - if util.IsDirectory(path) { + if ioutil.IsDir(path) { path = filepath.Join(path, "application.zip") } - if util.PathExists(path) { + if ioutil.Exists(path) { return "", fmt.Errorf("%s already exists", path) } if deployment.Target.IsCloud() { @@ -540,7 +540,7 @@ func checkResponse(req *http.Request, response *http.Response) error { if response.StatusCode/100 == 4 { return fmt.Errorf("invalid application package (%s)\n%s", response.Status, extractError(response.Body)) } else if response.StatusCode != 200 { - return fmt.Errorf("error from deploy API at %s (%s):\n%s", req.URL.Host, response.Status, util.ReaderToJSON(response.Body)) + return fmt.Errorf("error from deploy API at %s (%s):\n%s", req.URL.Host, response.Status, ioutil.ReaderToJSON(response.Body)) } return nil } diff --git a/client/go/internal/vespa/deploy_test.go b/client/go/internal/vespa/deploy_test.go index 516a21e7786..4c2fb912224 100644 --- a/client/go/internal/vespa/deploy_test.go +++ b/client/go/internal/vespa/deploy_test.go @@ -14,8 +14,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/mock" - "github.com/vespa-engine/vespa/client/go/internal/util" "github.com/vespa-engine/vespa/client/go/internal/version" ) @@ -235,7 +235,7 @@ func TestFetch(t *testing.T) { dir := t.TempDir() dst, err := Fetch(opts, dir) require.Nil(t, err) - assert.True(t, util.PathExists(dst)) + assert.True(t, ioutil.Exists(dst)) f, err := os.Open(dst) require.Nil(t, err) @@ -264,7 +264,7 @@ func TestFetchCloud(t *testing.T) { dir := t.TempDir() dst, err := Fetch(opts, dir) require.Nil(t, err) - assert.True(t, util.PathExists(dst)) + assert.True(t, ioutil.Exists(dst)) } type pkgFixture struct { diff --git a/client/go/internal/vespa/find_home.go b/client/go/internal/vespa/find_home.go index 46dcdedd71c..1043e5c785b 100644 --- a/client/go/internal/vespa/find_home.go +++ b/client/go/internal/vespa/find_home.go @@ -11,6 +11,7 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/admin/envvars" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/util" ) @@ -46,7 +47,7 @@ func FindHome() string { } for _, dir := range strings.Split(os.Getenv(envvars.PATH), ":") { fn := fmt.Sprintf("%s/%s", dir, myProgName) - if util.IsRegularFile(fn) { + if ioutil.IsFile(fn) { trace.Debug("findPath", myProgName, "=>", dir) return dir } @@ -56,7 +57,7 @@ func FindHome() string { // detect path from argv[0] for path := findPath(); path != ""; path = dirName(path) { mySelf := fmt.Sprintf("%s/%s", path, scriptUtilsFilename) - if util.IsRegularFile(mySelf) { + if ioutil.IsFile(mySelf) { trace.Debug("found", mySelf, "VH =>", path) os.Setenv(envvars.VESPA_HOME, path) return path @@ -82,7 +83,7 @@ func HasFileUnderVespaHome(fn string) (bool, string) { func FindAndVerifyVespaHome() string { vespaHome := FindHome() myself := fmt.Sprintf("%s/%s", vespaHome, scriptUtilsFilename) - if !util.IsExecutableFile(myself) { + if !ioutil.IsExecutable(myself) { trace.Warning("missing or bad file:", myself) util.JustExitMsg("Not a valid VESPA_HOME: " + vespaHome) } diff --git a/client/go/internal/vespa/load_env.go b/client/go/internal/vespa/load_env.go index 24e1b1cdefa..4457ec20d90 100644 --- a/client/go/internal/vespa/load_env.go +++ b/client/go/internal/vespa/load_env.go @@ -14,6 +14,7 @@ import ( "github.com/vespa-engine/vespa/client/go/internal/admin/envvars" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" + "github.com/vespa-engine/vespa/client/go/internal/ioutil" "github.com/vespa-engine/vespa/client/go/internal/util" ) @@ -276,7 +277,7 @@ func (builder *pathBuilder) applyTo(receiver loadEnvReceiver) { } func (builder *pathBuilder) appendPath(p string) { - if !util.IsDirectory(p) { + if !ioutil.IsDir(p) { return } for _, elem := range builder.curPath { |