Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
superbrothers committed Mar 8, 2021
1 parent 2479b6e commit 52c0f1d
Show file tree
Hide file tree
Showing 16 changed files with 1,699 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CI

on:
push:
branches: [master]
paths-ignore: ['**.md']
pull_request:
types: [opened, synchronize]
paths-ignore: ['**.md']

jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.16.x
- name: Ensure go.mod is already tidied
run: go mod tidy && git diff -s --exit-code go.sum
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: make lint
- run: make build
23 changes: 23 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Release

on:
push:
tags: ["v*"]

jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.16.x
- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: make release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
/hack/tools/bin
25 changes: 25 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
before:
hooks:
- go mod tidy
project_name: opener
env:
- GOPROXY=https://gocenter.io
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- darwin
goarch:
- amd64
- arm
- arm64
archives:
- name_template: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
format: zip
files:
- LICENSE
- README.md
wrap_in_directory: false
checksum:
name_template: 'checksums.txt'
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Kazuki Suda <kazuki.suda@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
NAME := opener
DIST_DIR := dist
GO ?= GOPROXY=https://gocenter.io go
VERSION ?= $(shell git describe --tags --always --dirty)

.PHONY: build
build:
$(GO) build -o $(DIST_DIR)/$(NAME) .

TOOLS_DIR := hack/tools
TOOLS_BIN_DIR := $(TOOLS_DIR)/bin
GORELEASER_BIN := bin/goreleaser
GORELEASER := $(TOOLS_DIR)/$(GORELEASER_BIN)

$(GORELEASER): $(TOOLS_DIR)/go.mod
cd $(TOOLS_DIR) && $(GO) build -o $(GORELEASER_BIN) github.com/goreleaser/goreleaser

.PHONY: build-cross
build-cross: $(GORELEASER)
$(GORELEASER) build --snapshot --rm-dist

.PHONY: vet
vet:
$(GO) vet ./...

.PHONY: fmt
fmt:
$(GO) fmt ./...

.PHONY: lint
lint: vet fmt
docker run --rm -v $(shell pwd):/app -w /app golangci/golangci-lint:v1.31.0 golangci-lint run

.PHONY: dist
dist: $(GORELEASER)
$(GORELEASER) release --rm-dist --skip-publish --snapshot

.PHONY: release
release: $(GORELEASER)
$(GORELEASER) release --rm-dist --skip-validate

.PHONY: clean
clean: clean-tools clean-dist

.PHONY: clean-tools
clean-tools:
rm -rf $(TOOLS_BIN_DIR)

.PHONY: clean-dist
clean-dist:
rm -rf $(DIST_DIR)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# opener
3 changes: 3 additions & 0 deletions bin/open
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "${@}" | socat - "UNIX-CLIENT:$HOME/.opener.sock"
3 changes: 3 additions & 0 deletions bin/xdg-open
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

echo "${@}" | socat - "UNIX-CLIENT:$HOME/.opener.sock"
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/superbrothers/opener

go 1.16

require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/spf13/cobra v1.1.3
)
290 changes: 290 additions & 0 deletions go.sum

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions hack/tools/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/superbrothers/opener/hack/tools

go 1.16

require (
github.com/goreleaser/goreleaser v0.159.0
)
1,104 changes: 1,104 additions & 0 deletions hack/tools/go.sum

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions hack/tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build tools
package main

import (
_ "github.com/goreleaser/goreleaser"
)
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"log"
"os"
)

func main() {
cmd := NewOpenerCmd(os.Stderr)
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}
113 changes: 113 additions & 0 deletions opener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"bufio"
"fmt"
"io"
"net"
"os"
"os/signal"
"strings"
"syscall"

"github.com/mitchellh/go-homedir"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)

var version string
var commit string
var date string

type OpenerOptions struct {
Address string

ErrOut io.Writer
}

func NewOpenerCmd(errOut io.Writer) *cobra.Command {
o := &OpenerOptions{
Address: "~/.opener.sock",
ErrOut: errOut,
}

cmd := &cobra.Command{
Use: "opener",
RunE: func(_ *cobra.Command, args []string) error {
if err := o.Validate(); err != nil {
return err
}

return o.Run()
},
}

return cmd
}

func (o *OpenerOptions) Validate() error {
address, err := homedir.Expand(o.Address)
if err != nil {
return err
}
o.Address = address

return nil
}

func (o *OpenerOptions) Run() error {
fmt.Fprintf(o.ErrOut, "version: %s, commit: %s, date: %s\n", version, commit, date)

syscall.Umask(0077)

if err := os.RemoveAll(o.Address); err != nil {
return err
}

fmt.Fprintf(o.ErrOut, "starting UNIX domain socket server at %s\n", o.Address)

ln, err := net.Listen("unix", o.Address)
if err != nil {
return err
}

defer ln.Close()

go func() {
for {
conn, err := ln.Accept()
if err != nil {
fmt.Fprintln(o.ErrOut, err)
return
}

go handleConnection(conn, o.ErrOut)
}
}()

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
sig := <-c
fmt.Fprintf(o.ErrOut, "got signal %s\n", sig)

return nil
}

func handleConnection(conn net.Conn, errOut io.Writer) {
defer conn.Close()

line, err := bufio.NewReader(conn).ReadString('\n')
line = strings.TrimRight(line, "\n")
fmt.Fprintf(errOut, "received %q\n", line)
if err != nil {
if err != io.EOF {
fmt.Fprintln(errOut, err)
return
}
}

if err := browser.OpenURL(line); err != nil {
fmt.Fprintf(errOut, "failed to open %q: %v\n", line, err)
return
}
}

0 comments on commit 52c0f1d

Please sign in to comment.