Skip to content

Commit b120e02

Browse files
authored
Post to HN using lukakerr/hkn go module (#2)
* post to hn using lukakerr/hkn go module * use the go module, remove index.js
1 parent 248c5fe commit b120e02

File tree

5 files changed

+67
-69
lines changed

5 files changed

+67
-69
lines changed

Dockerfile

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
# Container image that runs your code
2-
FROM alpine:3.10
1+
FROM golang:1.16-alpine
32

4-
# NOTE: install puppeteer Docker deps
5-
# https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-on-alpine
6-
# Installs latest Chromium (92) package.
7-
RUN apk add --no-cache \
8-
chromium \
9-
nss \
10-
freetype \
11-
harfbuzz \
12-
ca-certificates \
13-
ttf-freefont \
14-
nodejs \
15-
npm
3+
WORKDIR /app
164

17-
# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
18-
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
19-
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
5+
COPY go.mod ./
6+
COPY go.sum ./
207

21-
RUN npm install puppeteer
22-
COPY index.js /index.js
8+
RUN go mod download
239

24-
CMD ["node", "/index.js"]
10+
COPY *.go ./
11+
12+
RUN go build -o /publish-to-hn
13+
14+
CMD [ "/publish-to-hn" ]

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module action-hackernews-post
2+
3+
go 1.17
4+
5+
require github.com/lukakerr/hkn v0.0.0-20190218004349-b4aa957c3360 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/lukakerr/hkn v0.0.0-20190218004349-b4aa957c3360 h1:UbY5nZGTN279+eU7Ld2KimDTS29UzZF7T/4whepdY/I=
2+
github.com/lukakerr/hkn v0.0.0-20190218004349-b4aa957c3360/go.mod h1:/7Z86kh6Q6xub2pdlrDF4SFERHDw3mflKzNOYXD7d4I=

index.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"github.com/lukakerr/hkn"
7+
)
8+
9+
func _main() int {
10+
username := os.Getenv("INPUT_HN_USERNAME")
11+
password := os.Getenv("INPUT_HN_PASSWORD")
12+
title := os.Getenv("INPUT_POST_TITLE")
13+
url := os.Getenv("INPUT_POST_URL")
14+
15+
if (len(username) == 0 || len(password) == 0) {
16+
fmt.Println("No supplied credentials. Provide `username` & `password`");
17+
return 1
18+
}
19+
20+
if (len(title) == 0 || len(url) == 0) {
21+
fmt.Println("No supplied post. Provide `title` & `url` to post");
22+
return 1
23+
}
24+
25+
client := hkn.NewClient()
26+
cookie, err := client.Login(username, password)
27+
28+
if (err != nil) {
29+
fmt.Println(err)
30+
return 1
31+
}
32+
33+
created, err := client.CreateStoryWithURL(title, url, cookie)
34+
35+
if (err != nil) {
36+
fmt.Println(err)
37+
return 1
38+
} else if (created == false) {
39+
fmt.Println("Unknown error posting to HN")
40+
return 1
41+
}
42+
43+
fmt.Println("Successfully submitted to HN")
44+
45+
return 0
46+
}
47+
48+
func main() {
49+
os.Exit(_main())
50+
}

0 commit comments

Comments
 (0)