Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Km/update docker and make #19

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ _testmain.go
imgurcmd/imgurcmd
.vscode
dist/


# PHPStorm creates.idea folders
.idea
Empty file added .golangci.yml
Empty file.
15 changes: 15 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.19.6-alpine AS base

RUN apk update && apk add make bash curl build-base

WORKDIR /tmp
RUN go install github.com/canthefason/go-watcher/cmd/watcher@latest && go install github.com/google/gops@latest && go install github.com/maruel/panicparse@latest\
&& go install github.com/go-delve/delve/cmd/dlv@latest

ENV GOFLAGS=-mod=vendor

FROM golangci/golangci-lint:latest AS lint-base

FROM base
COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint

31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
DOCKER_COMPOSE_TEST := docker-compose -f dev/test.yml
TEST_SERVICE_NAME := test_goimgur

ifdef TEST_RUN
TESTRUN := -run ${TEST_RUN}
endif

GOPACKAGES := $(shell go list ./... | egrep -v 'github.com/mix/go-imgur$$')
TEST_MODULES ?= $(GOPACKAGES)


test: # run unit tests
${DOCKER_COMPOSE_TEST} rm --force || true
${DOCKER_COMPOSE_TEST} run ${TEST_SERVICE_NAME}
${DOCKER_COMPOSE_TEST} down

test-direct: # [INTERNAL]
go test -p 1 -v -race -coverprofile=$(COVERAGE_FILE) $(TESTRUN)

lint: # Run go lint
${DOCKER_COMPOSE_TEST} run test_goimgur bash -c "GOGC=50 make -e lint-direct"

lint-direct: # [INTERNAL]
@golangci-lint run

stop: # stop services
${DOCKER_COMPOSE_TEST} down || true

update: stop
${DOCKER_COMPOSE_TEST} rm --force ${TEST_SERVICE_NAME}
${DOCKER_COMPOSE_TEST} build ${TEST_SERVICE_NAME}
13 changes: 6 additions & 7 deletions album.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,18 @@ type AlbumInfo struct {
Limit *RateLimit // Current rate limit
}

// GetAlbumInfo queries imgur for information on a album
// returns album info, status code of the request, error
// GetAlbumInfo queries imgur for information on an album
// returns album info, status code of the request or of album payload, error
// http status code of request, -1 if request was not made
func (client *Client) GetAlbumInfo(id string) (*AlbumInfo, int, error) {
body, rl, err := client.getURL("album/" + id)
body, statusCode, rl, err := client.getURL("album/" + id)
if err != nil {
return nil, -1, errors.New("Problem getting URL for album info ID " + id + " - " + err.Error())
return nil, statusCode, errors.New("Problem getting URL for album info ID " + id + " - " + err.Error())
}
//client.Log.Debugf("%v\n", body)

dec := json.NewDecoder(strings.NewReader(body))
var alb albumInfoDataWrapper
if err := dec.Decode(&alb); err != nil {
return nil, -1, errors.New("Problem decoding json for albumID " + id + " - " + err.Error())
return nil, statusCode, errors.New("Problem decoding json for albumID " + id + " - " + err.Error())
}

if !alb.Success {
Expand Down
14 changes: 14 additions & 0 deletions dev/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '2'

services:
test_goimgur:
build:
context: ../
dockerfile: Dockerfile.dev
environment:
- TEST_RUN
- TEST_MODULES
volumes:
- ${PWD}:/app:delegated
working_dir: /app
command: make -e test-direct
27 changes: 19 additions & 8 deletions fromURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package imgur

import (
"errors"
"fmt"
"strings"
)

Expand All @@ -16,7 +17,7 @@ type GenericInfo struct {
}

// GetInfoFromURL tries to query imgur based on information identified in the URL.
// returns image/album info, status code of the request, error
// returns image/album info, http status code of the request, error
func (client *Client) GetInfoFromURL(url string) (*GenericInfo, int, error) {
url = strings.TrimSpace(url)

Expand Down Expand Up @@ -97,13 +98,16 @@ func (client *Client) galleryURL(url string) (*GenericInfo, int, error) {
ai, status, err := client.GetGalleryAlbumInfo(id)
if err == nil && status < 400 {
ret.GAlbum = ai
return &ret, status, err
return &ret, status, nil
}
// fallback to GetGalleryImageInfo
client.Log.Debugf("Failed to retrieve imgur gallery album. Attempting to retrieve imgur gallery image. err: %v status: %d", err, status)
ii, status, err := client.GetGalleryImageInfo(id)
ii, statusCode, err := client.GetGalleryImageInfo(id)
if err != nil {
return nil, statusCode, fmt.Errorf("client.GetGalleryImageInfo:%w", err)
}
ret.GImage = ii
return &ret, status, err
return &ret, statusCode, nil
}

func (client *Client) imageURL(url string) (*GenericInfo, int, error) {
Expand All @@ -118,15 +122,22 @@ func (client *Client) imageURL(url string) (*GenericInfo, int, error) {
if id == "" {
return nil, -1, errors.New("Could not find ID in URL " + url + ". I was going down imgur.com/ path.")
}
// check if id is a full filename E.G `vsadghes.jpg`, and if so, extract the actual id `vsadghes`
hasDotIndex := strings.LastIndex(id, ".")
if hasDotIndex > -1 {
id = id[0:hasDotIndex]
}
client.Log.Debugf("Detected imgur image ID %v. Was going down the imgur.com/ path.", id)
ii, status, err := client.GetGalleryImageInfo(id)
if err == nil && status < 400 {
ret.GImage = ii

return &ret, status, err
return &ret, status, nil
}

i, st, err := client.GetImageInfo(id)
i, statusCode, err := client.GetImageInfo(id)
if err != nil {
return nil, statusCode, fmt.Errorf("client.GetImageInfo:%w", err)
}
ret.Image = i
return &ret, st, err
return &ret, statusCode, nil
}
Loading