Skip to content

Instantly share code, notes, and snippets.

@koral--
koral-- / downloadutil.go
Last active May 7, 2018 16:47
Defer statement in Go
func downloadFile(downloadURL string, outFile *os.File) error {
response, err := http.Get(downloadURL)
if err != nil {
return err
}
defer func() {
if err := response.Body.Close(); err != nil {
log.Warnf("Failed to close (%s) body", downloadURL)
}
@koral--
koral-- / bitrise.yml
Created May 7, 2018 00:21
bitrise.yml example
- change-workdir:
title: Switch working dir to test / _tmp dir
description: |-
To prevent step testing issues, like referencing relative
files with just './some-file' in the step's code, which would
work for testing the step from this directory directly
but would break if the step is included in another `bitrise.yml`.
run_if: "true"
inputs:
- path: ./_tmp
@koral--
koral-- / sample_test.go
Created May 7, 2018 00:37
Go unit test example
func TestDownloadFileUnreachableURL(t *testing.T) {
dummyFile, err := os.Open("/dev/null")
require.NoError(t, err)
err = downloadFile("http://unreachable.invalid", dummyFile)
require.Error(t, err)
}
@koral--
koral-- / bitirise.yml
Created October 16, 2018 12:26
Chuck Norris jokes step inputs
inputs:
- category:
opts:
title: "Joke category, optional."
summary: |
Optional category of the joke, see [list of available categories](https://api.chucknorris.io/jokes/categories).
If empty joke won't be restricted to any category.
is_expand: true
is_required: false
- api_base_url: "https://api.chucknorris.io"
@koral--
koral-- / main.go
Created October 16, 2018 12:38
Chuck Norris jokes Bitirse step config
// Config ...
type Config struct {
APIBaseURL string `env:"api_base_url,required"`
Category string `env:"category"`
}
@koral--
koral-- / main.go
Created October 16, 2018 13:20
Stepconf usage
var config Config
if err := stepconf.Parse(&config); err != nil {
log.Errorf("Could not validate config, error: %s\n", err)
os.Exit(1)
}
@koral--
koral-- / main.go
Created October 16, 2018 15:09
Chuck Norris jokes Bitrise step URL building
func buildJokeRequest(config Config) (*http.Request, error) {
jokeURL, err := buildJokeURL(config)
if err != nil {
return nil, err
}
request, err := http.NewRequest("GET", jokeURL.String(), nil)
if err != nil {
return nil, err
}
@koral--
koral-- / main.go
Created October 16, 2018 15:48
HTTP request with timeout in Go
func getJoke(request *http.Request) (*http.Response, error) {
client := &http.Client{}
client.Timeout = 20 * time.Second
return client.Do(request)
}
@koral--
koral-- / main.go
Created October 16, 2018 15:55
Reading plain text HTTP response
func readJokeFromResponse(response *http.Response) (string, error) {
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("server returned an error: %s", response.Status)
}
content, err := ioutil.ReadAll(response.Body)
return string(content), err
}
@koral--
koral-- / main.go
Last active October 16, 2018 18:21
Downloading Chuck Norris joke as a plain text
func getRandomJoke(config Config) (string, error) {
request, err := buildJokeRequest(config)
if err != nil {
return "", err
}
response, err := getJoke(request)
if err != nil {
return "", err
}