This file is indexed.

/usr/share/gocode/src/github.com/hashicorp/atlas-go/v1/artifact.go is in golang-github-hashicorp-atlas-go-dev 0.0~git20170808.8261ea0-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package atlas

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/url"
)

// Artifact represents a single instance of an artifact.
type Artifact struct {
	// User and name are self-explanatory. Tag is the combination
	// of both into "username/name"
	User string `json:"username"`
	Name string `json:"name"`
	Tag  string `json:",omitempty"`
}

// ArtifactVersion represents a single version of an artifact.
type ArtifactVersion struct {
	User     string            `json:"username"`
	Name     string            `json:"name"`
	Tag      string            `json:",omitempty"`
	Type     string            `json:"artifact_type"`
	ID       string            `json:"id"`
	Version  int               `json:"version"`
	Metadata map[string]string `json:"metadata"`
	File     bool              `json:"file"`
	Slug     string            `json:"slug"`

	UploadPath  string `json:"upload_path"`
	UploadToken string `json:"upload_token"`
}

// ArtifactSearchOpts are the options used to search for an artifact.
type ArtifactSearchOpts struct {
	User string
	Name string
	Type string

	Build    string
	Version  string
	Metadata map[string]string
}

// UploadArtifactOpts are the options used to upload an artifact.
type UploadArtifactOpts struct {
	User      string
	Name      string
	Type      string
	ID        string
	File      io.Reader
	FileSize  int64
	Metadata  map[string]string
	BuildID   int
	CompileID int
}

// MarshalJSON converts the UploadArtifactOpts into a JSON struct.
func (o *UploadArtifactOpts) MarshalJSON() ([]byte, error) {
	return json.Marshal(map[string]interface{}{
		"artifact_version": map[string]interface{}{
			"id":         o.ID,
			"file":       o.File != nil,
			"metadata":   o.Metadata,
			"build_id":   o.BuildID,
			"compile_id": o.CompileID,
		},
	})
}

// This is the value that should be used for metadata in ArtifactSearchOpts
// if you don't care what the value is.
const MetadataAnyValue = "943febbf-589f-401b-8f25-58f6d8786848"

// Artifact finds the Atlas artifact by the given name and returns it. Any
// errors that occur are returned, including ErrAuth and ErrNotFound special
// exceptions which the user may want to handle separately.
func (c *Client) Artifact(user, name string) (*Artifact, error) {
	endpoint := fmt.Sprintf("/api/v1/artifacts/%s/%s", user, name)
	request, err := c.Request("GET", endpoint, nil)
	if err != nil {
		return nil, err
	}

	response, err := checkResp(c.HTTPClient.Do(request))
	if err != nil {
		return nil, err
	}

	var aw artifactWrapper
	if err := decodeJSON(response, &aw); err != nil {
		return nil, err
	}

	return aw.Artifact, nil
}

// ArtifactSearch searches Atlas for the given ArtifactSearchOpts and returns
// a slice of ArtifactVersions.
func (c *Client) ArtifactSearch(opts *ArtifactSearchOpts) ([]*ArtifactVersion, error) {
	log.Printf("[INFO] searching artifacts: %#v", opts)

	params := make(map[string]string)
	if opts.Version != "" {
		params["version"] = opts.Version
	}
	if opts.Build != "" {
		params["build"] = opts.Build
	}

	i := 1
	for k, v := range opts.Metadata {
		prefix := fmt.Sprintf("metadata.%d.", i)
		params[prefix+"key"] = k
		if v != MetadataAnyValue {
			params[prefix+"value"] = v
		}

		i++
	}

	endpoint := fmt.Sprintf("/api/v1/artifacts/%s/%s/%s/search",
		opts.User, opts.Name, opts.Type)
	request, err := c.Request("GET", endpoint, &RequestOptions{
		Params: params,
	})
	if err != nil {
		return nil, err
	}

	response, err := checkResp(c.HTTPClient.Do(request))
	if err != nil {
		return nil, err
	}

	var w artifactSearchWrapper
	if err := decodeJSON(response, &w); err != nil {
		return nil, err
	}

	return w.Versions, nil
}

// CreateArtifact creates and returns a new Artifact in Atlas. Any errors that
// occurr are returned.
func (c *Client) CreateArtifact(user, name string) (*Artifact, error) {
	log.Printf("[INFO] creating artifact: %s/%s", user, name)
	body, err := json.Marshal(&artifactWrapper{&Artifact{
		User: user,
		Name: name,
	}})
	if err != nil {
		return nil, err
	}

	endpoint := "/api/v1/artifacts"
	request, err := c.Request("POST", endpoint, &RequestOptions{
		Body: bytes.NewReader(body),
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
	})
	if err != nil {
		return nil, err
	}

	response, err := checkResp(c.HTTPClient.Do(request))
	if err != nil {
		return nil, err
	}

	var aw artifactWrapper
	if err := decodeJSON(response, &aw); err != nil {
		return nil, err
	}

	return aw.Artifact, nil
}

// ArtifactFileURL is a helper method for getting the URL for an ArtifactVersion
// from the Client.
func (c *Client) ArtifactFileURL(av *ArtifactVersion) (*url.URL, error) {
	if !av.File {
		return nil, nil
	}

	u := *c.URL
	u.Path = fmt.Sprintf("/api/v1/artifacts/%s/%s/%s/%d/file",
		av.User, av.Name, av.Type, av.Version)
	return &u, nil
}

// UploadArtifact streams the upload of a file on disk using the given
// UploadArtifactOpts. Any errors that occur are returned.
func (c *Client) UploadArtifact(opts *UploadArtifactOpts) (*ArtifactVersion, error) {
	log.Printf("[INFO] uploading artifact: %s/%s (%s)", opts.User, opts.Name, opts.Type)

	endpoint := fmt.Sprintf("/api/v1/artifacts/%s/%s/%s",
		opts.User, opts.Name, opts.Type)

	body, err := json.Marshal(opts)
	if err != nil {
		return nil, err
	}

	request, err := c.Request("POST", endpoint, &RequestOptions{
		Body: bytes.NewReader(body),
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
	})
	if err != nil {
		return nil, err
	}

	response, err := checkResp(c.HTTPClient.Do(request))
	if err != nil {
		return nil, err
	}

	var av ArtifactVersion
	if err := decodeJSON(response, &av); err != nil {
		return nil, err
	}

	if opts.File != nil {
		if err := c.putFile(av.UploadPath, opts.File, opts.FileSize); err != nil {
			return nil, err
		}
	}

	return &av, nil
}

type artifactWrapper struct {
	Artifact *Artifact `json:"artifact"`
}

type artifactSearchWrapper struct {
	Versions []*ArtifactVersion
}

type artifactVersionWrapper struct {
	Version *ArtifactVersion
}