From ead24aad8fec198851dc878168b9d949542b3886 Mon Sep 17 00:00:00 2001 From: "Barrera, Angel" Date: Wed, 9 Feb 2022 11:38:29 +0100 Subject: [PATCH] re-bumping the project --- .github/workflows/release.yaml | 32 ++++++ .goreleaser.yml | 6 +- .travis.yml | 17 --- Makefile | 25 +---- cmd/hasselhoffme/hasselhoffme.go | 177 +++++++++++++------------------ go.mod | 15 +-- go.sum | 49 ++++----- internal/embed.go | 24 +++++ internal/images/embeded.go | 34 ++++++ internal/images/utils.go | 26 +++-- internal/motd/motd.go | 105 ++++++++++++++++++ 11 files changed, 314 insertions(+), 196 deletions(-) create mode 100644 .github/workflows/release.yaml delete mode 100644 .travis.yml create mode 100644 internal/embed.go create mode 100644 internal/images/embeded.go create mode 100644 internal/motd/motd.go diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..23f7726 --- /dev/null +++ b/.github/workflows/release.yaml @@ -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/* \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml index 2626876..3d024ad 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 @@ -20,5 +20,5 @@ builds: - 5 - 6 - 7 -archive: - format: binary +archives: + - format: binary diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 810a19e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: go -os: -- linux -go: -- 1.x -env: -- GO111MODULE=on -before_install: -- go mod download - -deploy: -- provider: script - skip_cleanup: true - script: curl -sL http://git.io/goreleaser | bash - on: - tags: true - condition: $TRAVIS_OS_NAME = linux diff --git a/Makefile b/Makefile index 088c874..96cbecc 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/hasselhoffme/hasselhoffme.go b/cmd/hasselhoffme/hasselhoffme.go index ffd11f8..342a465 100644 --- a/cmd/hasselhoffme/hasselhoffme.go +++ b/cmd/hasselhoffme/hasselhoffme.go @@ -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] [] - 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) } @@ -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 <