-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
243 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/go-kivik/xkivik/v4/cmd/kouchctl/output" | ||
) | ||
|
||
type descrAttachment struct { | ||
*root | ||
} | ||
|
||
func descrAttachmentCmd(r *root) *cobra.Command { | ||
c := &descrAttachment{ | ||
root: r, | ||
} | ||
return &cobra.Command{ | ||
Use: "attachment [dsn]/[database]/[document]/[filename]", | ||
Aliases: []string{"att", "attach"}, | ||
Short: "Describe an attachment", | ||
Long: `Fetch attachment with the HTTP HEAD verb`, | ||
RunE: c.RunE, | ||
} | ||
} | ||
|
||
func (c *descrAttachment) RunE(cmd *cobra.Command, _ []string) error { | ||
client, err := c.client() | ||
if err != nil { | ||
return err | ||
} | ||
db, docID, filename, err := c.conf.DBDocFilename() | ||
if err != nil { | ||
return err | ||
} | ||
c.log.Debugf("[get] Will fetch document: %s/%s/%s", client.DSN(), db, docID) | ||
return c.retry(func() error { | ||
att, err := client.DB(db).GetAttachmentMeta(cmd.Context(), docID, filename, c.opts()) | ||
if err != nil { | ||
return err | ||
} | ||
att.Filename = filename | ||
|
||
format := `Filename: {{ .Filename }} | ||
Content-Type: {{ .ContentType }} | ||
Content-Length: {{ .Size }} | ||
Digest: {{ .Digest }} | ||
{{- with .RevPos }} | ||
RevPos: {{ . }} | ||
{{ end -}} | ||
` | ||
|
||
result := output.TemplateReader(format, att, output.JSONReader(att)) | ||
return c.fmt.Output(result) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"gitlab.com/flimzy/testy" | ||
|
||
"github.com/go-kivik/xkivik/v4/cmd/kouchctl/errors" | ||
) | ||
|
||
func Test_describe_attachment_RunE(t *testing.T) { | ||
tests := testy.NewTable() | ||
|
||
tests.Add("missing resource", cmdTest{ | ||
args: []string{"descr", "attachment"}, | ||
status: errors.ErrUsage, | ||
}) | ||
tests.Add("not found", func(t *testing.T) interface{} { | ||
s := testy.ServeResponse(&http.Response{ | ||
StatusCode: http.StatusNotFound, | ||
Header: http.Header{ | ||
"Content-Type": []string{"application/json"}, | ||
}, | ||
Body: ioutil.NopCloser(strings.NewReader(`{"error":"not_found","reason":"Document is missing attachment"} | ||
`)), | ||
}) | ||
|
||
return cmdTest{ | ||
args: []string{"descr", "attachment", s.URL + "/db/doc/foo.txt"}, | ||
status: errors.ErrNotFound, | ||
} | ||
}) | ||
tests.Add("success", func(t *testing.T) interface{} { | ||
s := testy.ServeResponse(&http.Response{ | ||
StatusCode: http.StatusOK, | ||
Header: http.Header{ | ||
"Content-Type": []string{"text/plain"}, | ||
"Server": []string{"CouchDB/2.3.1 (Erlang OTP/20)"}, | ||
"ETag": []string{`"cy5z3SF7yaYp4vmLX0k31Q==`}, | ||
}, | ||
Body: ioutil.NopCloser(strings.NewReader(`Testing`)), | ||
}) | ||
|
||
return cmdTest{ | ||
args: []string{"descr", "attachment", s.URL + "/db/doc/foo.txt"}, | ||
} | ||
}) | ||
tests.Add("success json", func(t *testing.T) interface{} { | ||
s := testy.ServeResponse(&http.Response{ | ||
StatusCode: http.StatusOK, | ||
Header: http.Header{ | ||
"Content-Type": []string{"text/plain"}, | ||
"Server": []string{"CouchDB/2.3.1 (Erlang OTP/20)"}, | ||
"ETag": []string{`"cy5z3SF7yaYp4vmLX0k31Q==`}, | ||
}, | ||
Body: ioutil.NopCloser(strings.NewReader(`Testing`)), | ||
}) | ||
|
||
return cmdTest{ | ||
args: []string{"descr", "attachment", s.URL + "/db/doc/foo.txt", "-f", "json"}, | ||
} | ||
}) | ||
|
||
tests.Run(t, func(t *testing.T, tt cmdTest) { | ||
tt.Test(t) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
cmd/kouchctl/cmd/testdata/Test_describe_RunE_auto_attachment__stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Filename: foo.txt | ||
Content-Type: text/plain | ||
Content-Length: 7 | ||
Digest: cy5z3SF7yaYp4vmLX0k31Q== |
25 changes: 25 additions & 0 deletions
25
cmd/kouchctl/cmd/testdata/Test_describe_attachment_RunE_missing_resource__stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Error: no context specified | ||
Usage: | ||
kouchctl describe attachment [dsn]/[database]/[document]/[filename] [flags] | ||
|
||
Aliases: | ||
attachment, att, attach | ||
|
||
Flags: | ||
-h, --help help for attachment | ||
|
||
Global Flags: | ||
--connect-timeout string Limits the time spent establishing a TCP connection. | ||
--debug Enable debug output | ||
-f, --format string Output format. One of: json[=...]|raw|yaml|go-template=... | ||
-H, --header Output response header | ||
--kouchconfig string Path to kouchconfig file to use for CLI requests (default "~/.kouchctl/config") | ||
-O, --option stringToString CouchDB options, specified as key=value. May be repeated. (default []) | ||
-o, --output string Output file/directory. | ||
-F, --overwrite Overwrite output file | ||
--request-timeout string The time limit for each request. | ||
--retry int In case of transient error, retry up to this many times. A negative value retries forever. | ||
--retry-delay string Delay between retry attempts. Disables the default exponential backoff algorithm. | ||
--retry-timeout string When used with --retry, no more retries will be attempted after this timeout. | ||
-v, --verbose Output bi-directional network traffic | ||
|
1 change: 1 addition & 0 deletions
1
cmd/kouchctl/cmd/testdata/Test_describe_attachment_RunE_not_found__stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Error: Not Found |
4 changes: 4 additions & 0 deletions
4
cmd/kouchctl/cmd/testdata/Test_describe_attachment_RunE_success__stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Filename: foo.txt | ||
Content-Type: text/plain | ||
Content-Length: 7 | ||
Digest: cy5z3SF7yaYp4vmLX0k31Q== |
5 changes: 5 additions & 0 deletions
5
cmd/kouchctl/cmd/testdata/Test_describe_attachment_RunE_success_json__stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"content_type": "text/plain", | ||
"digest": "cy5z3SF7yaYp4vmLX0k31Q==", | ||
"length": 7 | ||
} |
1 change: 1 addition & 0 deletions
1
cmd/kouchctl/cmd/testdata/Test_get_RunE_auto_attachment__stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Testing |
2 changes: 1 addition & 1 deletion
2
cmd/kouchctl/cmd/testdata/Test_get_attachment_RunE_missing_resource__stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters