This repository was archived by the owner on Jun 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcmd_upload.go
More file actions
78 lines (72 loc) · 2.6 KB
/
Copy pathcmd_upload.go
File metadata and controls
78 lines (72 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"fmt"
"net/url"
"strings"
"github.com/spf13/pflag"
"golang.org/x/exp/maps"
)
var (
flagsUpload *pflag.FlagSet
flagUploadAsFilename string
flagUploadDocumentId string
)
var ErrUploadUsage = ObserveError{Msg: "usage: observe upload <document usage> <document name>"}
var ErrUnsupportedType = ObserveError{Msg: "the document usage kind is not supported"}
var ErrNotTextFile = ObserveError{Msg: "the document is not a text file"}
var ErrFileNotReadable = ObserveError{Msg: "the file is not readable"}
func init() {
flagsUpload = pflag.NewFlagSet("upload", pflag.ContinueOnError)
flagsUpload.StringVarP(&flagUploadAsFilename, "as-filename", "f", "", "use this as uploaded name instead of the source file path")
flagsUpload.StringVarP(&flagUploadDocumentId, "document-id", "d", "", "replace this particular document id, rather than matching on name")
RegisterCommand(&Command{
Name: "upload",
Help: "Put (or overwrite) a document of some sort, identified by filename or id.",
Flags: flagsUpload,
Func: cmdUpload,
})
}
// 'upload' and 'document' are somewhat intertwined
func cmdUpload(fa FuncArgs) error {
if len(fa.args) != 3 {
return ErrUploadUsage
}
kind, has := docTypes[fa.args[1]]
if !has {
return ObserveError{Msg: "supported document usage kinds are: " + strings.Join(sorted(maps.Keys(docTypes)), ", ")}.WithInner(ErrUnsupportedType)
}
data, err := fa.fs.ReadFile(fa.args[2])
if err != nil {
return ErrFileNotReadable.WithInner(err)
}
mimetype, err := kind.SniffMimetype(data)
if err != nil {
return ErrNotTextFile.WithInner(err)
}
// is this a "create" or a "replace"?
asName := fa.args[2]
if flagUploadAsFilename != "" {
asName = flagUploadAsFilename
}
fa.op.Debug("as_name=%s\n", asName)
prevId := flagUploadDocumentId
if prevId == "" {
prevId = determinePreviousDocumentId(fa.cfg, fa.op, fa.hc, asName)
}
fa.op.Debug("prev_id=%s\n", prevId)
var obj object
if prevId == "" {
obj, err = Query(fa.hc).Config(fa.cfg).Output(fa.op).Path("/v1/document/").Args(map[string]string{"name": asName, "usage": kind.Kind()}).Header(headers("content-type", mimetype)).Body(bytes.NewReader(data)).PropMap(propertyMapDocument).Post()
} else {
obj, err = Query(fa.hc).Config(fa.cfg).Output(fa.op).Path("/v1/document/" + url.QueryEscape(prevId)).Args(map[string]string{"name": asName, "usage": kind.Kind()}).Header(headers("content-type", mimetype)).Body(bytes.NewReader(data)).PropMap(propertyMapDocument).Put()
}
if err != nil {
return err
}
keys := sorted(maps.Keys(obj))
for _, k := range keys {
fmt.Fprintf(fa.op, "%s: %v\n", k, obj[k])
}
return nil
}