Skip to content

Commit

Permalink
♻️ Rewrite in Golang
Browse files Browse the repository at this point in the history
  • Loading branch information
segersniels committed Apr 13, 2024
1 parent 9dde907 commit ff96ea1
Show file tree
Hide file tree
Showing 18 changed files with 413 additions and 494 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ quote_type = single
# Zig ruleset
[*.zig]
indent_size = 4

# Makefile
[Makefile]
indent_style = tab
indent_size = 4
18 changes: 11 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,43 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v2
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm run lint
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.54

build:
needs: [lint]
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
steps:
- uses: actions/checkout@v4
- uses: goto-bus-stop/setup-zig@v2
- uses: actions/setup-go@v5
with:
version: master
go-version: '1.22'
- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm run build:ci --workspace cli
- run: npm run build --workspace cli
- name: Get current package version
id: current_version
run: echo "version=$(cat build.zig.zon |grep .version |awk -F' ' '{print $3}' |tr -d '\",')" >> $GITHUB_OUTPUT
run: echo "version=$(make version)" >> $GITHUB_OUTPUT
working-directory: apps/cli
- uses: mukunku/[email protected]
id: check_tag
with:
tag: '${{ steps.current_version.outputs.version }}'
- run: ./scripts/rename.sh
working-directory: apps/cli
- uses: ncipollo/release-action@v1
if: steps.check_tag.outputs.exists == 'false'
with:
Expand Down
24 changes: 24 additions & 0 deletions apps/cli/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.PHONY: build clean run version

BINARY_NAME=genmoji
VERSION=0.4.0
BUILD_DIR=bin

TARGETS=darwin-arm64 darwin-amd64 linux-arm64 linux-amd64
LDFLAGS="-X main.AppVersion=$(VERSION) -X main.AppName=$(BINARY_NAME)"

build:
GOOS=darwin GOARCH=arm64 go build -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 -ldflags $(LDFLAGS)
GOOS=darwin GOARCH=amd64 go build -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-amd64 -ldflags $(LDFLAGS)
GOOS=linux GOARCH=arm64 go build -o $(BUILD_DIR)/$(BINARY_NAME)-linux-arm64 -ldflags $(LDFLAGS)
GOOS=linux GOARCH=amd64 go build -o $(BUILD_DIR)/$(BINARY_NAME)-linux-amd64 -ldflags $(LDFLAGS)

clean:
rm -rf $(BUILD_DIR)

run:
@go build -o $(BUILD_DIR)/genmoji
@./bin/genmoji

version:
@echo $(VERSION)
147 changes: 0 additions & 147 deletions apps/cli/build.zig

This file was deleted.

11 changes: 0 additions & 11 deletions apps/cli/build.zig.zon

This file was deleted.

81 changes: 81 additions & 0 deletions apps/cli/emoji.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"encoding/json"
"net/http"
"os"
)

type Gitmoji struct {
Emoji string `json:"emoji"`
Code string `json:"code"`
Description string `json:"description"`
Name string `json:"name"`
}

type Response struct {
Gitmojis []Gitmoji `json:"gitmojis"`
}

func isCached(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}

return true
}

func fetchFromCache(path string) ([]Gitmoji, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

var response Response
err = json.NewDecoder(file).Decode(&response)
if err != nil {
return nil, err
}

return response.Gitmojis, nil
}

func writeToCache(path string, response Response) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()

err = json.NewEncoder(file).Encode(&response)
if err != nil {
return err
}

return nil
}

func fetchGitmojis() ([]Gitmoji, error) {
var path string = "~/.genmoji/gitmojis.json"
if isCached(path) {
return fetchFromCache(path)
}

res, err := http.Get("https://gitmoji.dev/api/gitmojis")
if err != nil {
return nil, err
}
defer res.Body.Close()

var response Response
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}

if err := writeToCache(path, response); err != nil {
return nil, err
}

return response.Gitmojis, nil
}
26 changes: 26 additions & 0 deletions apps/cli/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"errors"
"os/exec"
)

func getStagedChanges() (string, error) {
cmd := exec.Command("git", "diff", "--cached")
stdout, err := cmd.Output()

if err != nil {
return "", err
}

if len(stdout) == 0 {
return "", errors.New("no staged changes found")
}

return string(stdout), nil
}

func commit(message string) error {
cmd := exec.Command("git", "commit", "-m", message)
return cmd.Run()
}
13 changes: 13 additions & 0 deletions apps/cli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module segersniels/genmoji

go 1.22.2

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/cli/v2 v2.27.1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
)
16 changes: 16 additions & 0 deletions apps/cli/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Loading

0 comments on commit ff96ea1

Please sign in to comment.