hancock

Check-in [e17191b0d8]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:cmd/hancock: minor fixes; cleanup
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e17191b0d8115a36eb7ada688b119976e8b8435fe8ff3f1ec576b5eade937365
User & Date: dnc 2020-01-09 19:35:38
Context
2020-01-09
19:36
cmd/hancock: fix support for relative file URLs check-in: edf65bff1c user: dnc tags: trunk
19:35
cmd/hancock: minor fixes; cleanup check-in: e17191b0d8 user: dnc tags: trunk
2020-01-06
21:04
cmd/hancock: update generated documentation check-in: 86031540c6 user: dnc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to cmd/hancock/publish.go.

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
..
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Publish signed testimony to an index with:
//    hancock publish
//
// This operation published the files produced by `hancock testimony`.
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path/filepath"

	"github.com/pkg/errors"
	"src.d10.dev/command"
	"src.d10.dev/hancock/model"
	"src.d10.dev/hancock/store"
)

................................................................................
	// defaults
	cfgTop := cfg.Section("")
	cfgOp := cfg.Section("publish")

	defaultStore := cfgOp.Key("store").MustString(cfgTop.Key("store").MustString("https://hancock.beyondcentral.com"))

	// args
	storeFlag := command.OperationFlagSet.String("store", "defaultStore", fmt.Sprintf("URL of remote testimony store (default %q)", defaultStore))

	// parse flags
	err = command.ParseOperationFlagSet()
	if err != nil {
		return err
	}

................................................................................

			// publish testimony from local to remote
			k, err := remote.PutTestimony(testimony)
			if err != nil {
				return err
			}

			command.V(1).Infof("published %v to %v", k, remote)
			pubs++
			return nil
		})
		if err != nil {
			command.Error(err)
		}
	}

	command.Infof("published %d items to %q", pubs, remote)

	return nil
}

// publish testimony either to local filesystem or via POST to online index.
func XXXpublishTestimonyToStore(storeURL string, ft fileTestimony) error {
	log.Panic("replace calls to publishTestimonyToStore() with store.Store interface!")
	u, err := url.Parse(storeURL)
	if err != nil {
		return err
	}

	err = ft.Check()
	if err != nil {
		return err
	}

	switch u.Scheme {
	case "http", "https":
		// post to URL
		urlPath := storeURL + "/testimony"
		resp, err := http.Post(urlPath, "application/json", bytes.NewBuffer(ft.Bytes()))
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		switch resp.StatusCode {
		case http.StatusOK, http.StatusCreated, http.StatusAccepted:
			// success
			if command.V(2) {
				command.Infof("posted testimony to %q", urlPath)
				b, _ := ioutil.ReadAll(resp.Body)
				command.Info(resp.StatusCode)
				command.Info(string(b))
			}
		default:
			command.V(1).Infof("Failed to post testimony: %s", string(ft.Bytes()))
			b, _ := ioutil.ReadAll(resp.Body)
			return fmt.Errorf("failed to publish testimony (%q) to index (%s): %s %q", ft.Path, storeURL, resp.Status, string(b))
		}

	case "file":
		// save to directory
		key, err := model.IndexKey(ft.Testimony.Content, ft.Authority)
		if err != nil {
			return err
		}

		// filepath.Join() turns the key values into directories.  If we
		// used flat file names, we might exceed filesystem limits for
		// number of files.
		path := filepath.Join(u.Path, filepath.Join(key...))
		err = os.MkdirAll(filepath.Dir(path), os.ModePerm)
		if err != nil {
			return fmt.Errorf("failed to store testimony file (%q): %w", path, err)
		}
		err = ioutil.WriteFile(path, ft.Bytes(), 0644)
		if err != nil {
			return fmt.Errorf("failed to store testimony file (%q): %w", path, err)
		} else {
			command.V(2).Infof("wrote testimony (%q) to file (%q)", ft.Path, path)
		}

	default:
		return fmt.Errorf("unexpected index URL scheme (%q)", u.Scheme)
	}
	return nil
}







<

<
<
<
<
<
<







 







|







 







|








|



<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
18
19
20
21
22
23
24

25






26
27
28
29
30
31
32
..
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

































































// Publish signed testimony to an index with:
//    hancock publish
//
// This operation published the files produced by `hancock testimony`.
package main

import (

	"fmt"







	"github.com/pkg/errors"
	"src.d10.dev/command"
	"src.d10.dev/hancock/model"
	"src.d10.dev/hancock/store"
)

................................................................................
	// defaults
	cfgTop := cfg.Section("")
	cfgOp := cfg.Section("publish")

	defaultStore := cfgOp.Key("store").MustString(cfgTop.Key("store").MustString("https://hancock.beyondcentral.com"))

	// args
	storeFlag := command.OperationFlagSet.String("store", defaultStore, "URL of remote testimony store (default %q)")

	// parse flags
	err = command.ParseOperationFlagSet()
	if err != nil {
		return err
	}

................................................................................

			// publish testimony from local to remote
			k, err := remote.PutTestimony(testimony)
			if err != nil {
				return err
			}

			command.V(1).Infof("published %v to %q", k, *storeFlag)
			pubs++
			return nil
		})
		if err != nil {
			command.Error(err)
		}
	}

	command.Infof("published %d items to %q", pubs, *storeFlag)

	return nil
}