Skip to content

Commit

Permalink
Post to HN using lukakerr/hkn go module (#2)
Browse files Browse the repository at this point in the history
* post to hn using lukakerr/hkn go module

* use the go module, remove index.js
  • Loading branch information
higgins authored Nov 23, 2021
1 parent 248c5fe commit b120e02
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 69 deletions.
30 changes: 10 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
# Container image that runs your code
FROM alpine:3.10
FROM golang:1.16-alpine

# NOTE: install puppeteer Docker deps
# https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-on-alpine
# Installs latest Chromium (92) package.
RUN apk add --no-cache \
chromium \
nss \
freetype \
harfbuzz \
ca-certificates \
ttf-freefont \
nodejs \
npm
WORKDIR /app

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

RUN npm install puppeteer
COPY index.js /index.js
RUN go mod download

CMD ["node", "/index.js"]
COPY *.go ./

RUN go build -o /publish-to-hn

CMD [ "/publish-to-hn" ]
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module action-hackernews-post

go 1.17

require github.com/lukakerr/hkn v0.0.0-20190218004349-b4aa957c3360 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/lukakerr/hkn v0.0.0-20190218004349-b4aa957c3360 h1:UbY5nZGTN279+eU7Ld2KimDTS29UzZF7T/4whepdY/I=
github.com/lukakerr/hkn v0.0.0-20190218004349-b4aa957c3360/go.mod h1:/7Z86kh6Q6xub2pdlrDF4SFERHDw3mflKzNOYXD7d4I=
49 changes: 0 additions & 49 deletions index.js

This file was deleted.

50 changes: 50 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"os"
"github.com/lukakerr/hkn"
)

func _main() int {
username := os.Getenv("INPUT_HN_USERNAME")
password := os.Getenv("INPUT_HN_PASSWORD")
title := os.Getenv("INPUT_POST_TITLE")
url := os.Getenv("INPUT_POST_URL")

if (len(username) == 0 || len(password) == 0) {
fmt.Println("No supplied credentials. Provide `username` & `password`");
return 1
}

if (len(title) == 0 || len(url) == 0) {
fmt.Println("No supplied post. Provide `title` & `url` to post");
return 1
}

client := hkn.NewClient()
cookie, err := client.Login(username, password)

if (err != nil) {
fmt.Println(err)
return 1
}

created, err := client.CreateStoryWithURL(title, url, cookie)

if (err != nil) {
fmt.Println(err)
return 1
} else if (created == false) {
fmt.Println("Unknown error posting to HN")
return 1
}

fmt.Println("Successfully submitted to HN")

return 0
}

func main() {
os.Exit(_main())
}

0 comments on commit b120e02

Please sign in to comment.