Skip to content

Commit

Permalink
re-bumping the project
Browse files Browse the repository at this point in the history
  • Loading branch information
Barrera, Angel authored and Barrera, Angel committed Feb 9, 2022
1 parent c0efafd commit ead24aa
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 196 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: release

on:
push:
tags:
- '*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload assets
uses: actions/upload-artifact@v2
with:
name: hasselhoffme
path: dist/*
6 changes: 3 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
before:
hooks:
- go mod download
- go mod tidy
builds:
- main: ./cmd/hasselhoffme/
env:
- CGO_ENABLED=0
- GO111MODULE=on
binary: hasselhoffme
goos:
- windows
Expand All @@ -20,5 +20,5 @@ builds:
- 5
- 6
- 7
archive:
format: binary
archives:
- format: binary
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

25 changes: 2 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
.PHONY: test lint check cover build
#.PHONY: release

test: ## Run tests
go test -race ./... -timeout=5m

lint: ## Run linters. Use make install-linters first.
go get -u golang.org/x/lint/golint
golint --set_exit_status ./...
go vet -all ./...

check: lint test ## Run tests and linters

cover: ## Runs tests on ./cmd/ with HTML code coverage
go test -race -cover -coverprofile=cover.out -coverpkg=github.com/angelbarrera92/hasselhoffme/... ./...
go tool cover -html=cover.out
.PHONY: build

build: ## Builds the binary
export GO111MODULE=on && \
go mod download && \
go build -v ./cmd/hasselhoffme

## TODO: Add support for goreleaser
#release: check ## Use GoReleaser to build, package and release
# goreleaser --rm-dist
go mod download && go mod tidy && go build -v ./cmd/hasselhoffme
177 changes: 75 additions & 102 deletions cmd/hasselhoffme/hasselhoffme.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,77 @@
package main

import (
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"

"github.com/angelbarrera92/hasselhoffme/internal/images"
"github.com/angelbarrera92/hasselhoffme/internal/motd"
"github.com/reujab/wallpaper"
"github.com/zyxar/image2ascii/ascii"
)

const (
// MOTDFile is a path to /etc/motd file
MOTDFile = "/etc/motd"
// UpdateMOTDPath is a path to /etc/update-motd.d
UpdateMOTDPath = "/etc/update-motd.d"
// UpdateMOTDFile is a path to /etc/update-motd.d/99-hasselhofme
UpdateMOTDFile = UpdateMOTDPath + "/99-hasselhoffme"
)

func usage() {
fmt.Fprintf(os.Stderr, `%s [-h] [<action>]
available actions:
setwallpaper: sets a random wallpaper
(default if no action specified)
setmotd: sets a random ascii art motd
`, os.Args[0])
os.Exit(1)
}

func main() {
action := "setwallpaper"
url := SearchRandomImage(images.SearchGithubRawImages, "")
// Create CLI flags
action := flag.String("action", "setwallpaper", "action to perform. Available actions: setwallpaper or setmotd")
provider := flag.String("provider", "embeded", "image provider. Available providers: embeded, repository, bing")
words := flag.String("words", "hasselhoff", "words to search for. Only working with bing provider")
// Parse
flag.Parse()

// Default values
fn := images.SearchEmbededImages
remote := true

// Validate
switch *provider {
case "embeded":
fn = images.SearchEmbededImages
remote = false
case "repository":
fn = images.SearchGithubRawImages
case "bing":
fn = images.SearchBingImage
if *words == "" {
fmt.Fprintf(os.Stderr, "provider bing requires words to search\n")
os.Exit(1)
}
default:
fmt.Fprintf(os.Stderr, "unknown provider %s\n", *provider)
os.Exit(1)
}

if len(os.Args) >= 2 {
action = os.Args[1]
// Look for an image in the specified provider
pathOrURL := searchRandomImage(fn, *words)
if !remote {
defer os.Remove(pathOrURL)
}

switch action {
// Then, perform the specified action
switch *action {
case "setwallpaper":
setWallpaperFromURL(url)
if remote {
setWallpaperFromURL(pathOrURL)
} else {
setWallpaperFromPath(pathOrURL)
}
case "setmotd":
setMotdFromURL(url)
if remote {
setMotdFromURL(pathOrURL)
} else {
setMotdFromPath(pathOrURL)
}
default:
usage()
fmt.Fprintf(os.Stderr, "unknown action %s\n", *action)
flag.PrintDefaults()
os.Exit(1)
}
}

// SearchRandomImage searches for a random image using provided image search function
func SearchRandomImage(sifn images.SearchImageFn, wordsToSearch string) string {
// searchRandomImage searches for a random image using provided image search function (provider)
func searchRandomImage(sifn images.SearchImageFn, wordsToSearch string) string {
return sifn(wordsToSearch)
}

Expand All @@ -62,94 +83,46 @@ func setWallpaperFromURL(url string) {
}
}

func setMotdFromURL(url string) {
if os.Getuid() != 0 {
fmt.Fprintf(os.Stderr, "motd can only be changed as root\n")
os.Exit(1)
}

resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "error while downloading %s: %v\n", url, err)
os.Exit(1)
}
defer resp.Body.Close()

opt := ascii.Options{
Width: 0,
Height: 0,
Color: false,
Invert: false,
Flipx: false,
Flipy: false}
motd, err := ascii.Decode(resp.Body, opt)
func setWallpaperFromPath(path string) {
err := wallpaper.SetFromFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error while decoding url response body %s: %v\n", url, err)
fmt.Fprintf(os.Stderr, "error while setting wallpaper from path %s: %v\n", path, err)
os.Exit(1)
}

if _, err := os.Stat(UpdateMOTDPath); os.IsNotExist(err) {
writeMotd(motd)
} else {
writeUpdateMotdScript(motd)
}
}

func writeMotd(motd *ascii.Ascii) {
content := ""
if _, err := os.Stat(MOTDFile); !os.IsNotExist(err) {
contentBytes, err := ioutil.ReadFile(MOTDFile)
if err != nil {
fmt.Fprintf(os.Stderr, "error while opening %s: %v\n", MOTDFile, err)
os.Exit(1)
}
content = string(contentBytes)
}

re := regexp.MustCompile(`(?s)### hasselhon ###.*### hasselhoff ###\n`)
content = re.ReplaceAllString(content, "")

f, err := os.OpenFile(MOTDFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) // nolint
if err != nil {
fmt.Fprintf(os.Stderr, "error while opening %s: %v\n", MOTDFile, err)
os.Exit(1)
}
defer f.Close()

fmt.Fprintf(f, "%s### hasselhon ###\n", content)
_, err = motd.WriteTo(f)
func setMotdFromURL(url string) {
data, err := download(url)
if err != nil {
fmt.Fprintf(os.Stderr, "error while writing to file %s: %v\n", f.Name(), err)
fmt.Fprintf(os.Stderr, "error while downloading %s: %v\n", url, err)
os.Exit(1)
}
fmt.Fprintf(f, "### hasselhoff ###\n")
motd.SetMotd(data)
}

func writeUpdateMotdScript(motd *ascii.Ascii) {
f, err := os.OpenFile(UpdateMOTDFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) // nolint
func setMotdFromPath(path string) {
data, err := readFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error while opening %s: %v\n", UpdateMOTDFile, err)
fmt.Fprintf(os.Stderr, "error reading file %s: %v\n", path, err)
os.Exit(1)
}
defer f.Close()
motd.SetMotd(data)
}

_, err = fmt.Fprintf(f, `#!/bin/sh
cat <<EOF
`)
func download(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "error while outputting to file %s: %v\n", f.Name(), err)
os.Exit(1)
fmt.Fprintf(os.Stderr, "error while downloading %s: %v\n", url, err)
return nil, err
}

_, err = motd.WriteTo(f)
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "error while writing to file %s: %v\n", f.Name(), err)
os.Exit(1)
return nil, err
}
return bodyBytes, nil
}

_, err = fmt.Fprintf(f, "EOF\n")
if err != nil {
fmt.Fprintf(os.Stderr, "error while outputting to file %s: %v\n", f.Name(), err)
os.Exit(1)
}
func readFile(path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
15 changes: 3 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
module github.com/angelbarrera92/hasselhoffme

go 1.14
go 1.16

require (
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/reujab/wallpaper v0.0.0-20200229074030-4e1aa3ff8284
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a // indirect
github.com/reujab/wallpaper v0.0.0-20210630195606-5f9f655b3740
github.com/zyxar/image2ascii v0.0.0-20180912034614-460a04e371ae
golang.org/x/image v0.0.0-20180926015637-991ec62608f3 // indirect
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 // indirect
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc
golang.org/x/tools v0.0.0-20181029181417-2d2de6298155 // indirect
gopkg.in/ini.v1 v1.39.0 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
)
Loading

0 comments on commit ead24aa

Please sign in to comment.