Skip to content

Commit

Permalink
Prepare a release workflow
Browse files Browse the repository at this point in the history
* print help in root cmd
* update readme
* replace old logger with new logger
* add comments for exported functions
  • Loading branch information
abhi-g80 committed Aug 15, 2022
1 parent cf84d7a commit f555095
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 37 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release Chipku
on:
release:
types: [created]

jobs:
releases-matrix:
name: Release Chipku Binaries
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux, windows, darwin]
goarch: [amd64, arm64]
exclude:
- goos: windows
goarch: arm64

steps:
- name: Get Release Info
run: |
echo "RELEASE_TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
echo "REPOSITORY_NAME=${GITHUB_REPOSITORY#*/}" >> $GITHUB_ENV
echo "OS_NAME=${{ matrix.goos }}" >> $GITHUB_ENV
- name: OS darwin
if: matrix.goos == 'darwin'
run: echo "OS_NAME=macOS" >> $GITHUB_ENV
- uses: actions/checkout@v3
- uses: wangyoucao577/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
md5sum: false
sha256sum: true
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: 1.16
binary_name: chipku
ldflags: -s -w
extra_files: LICENSE README.md
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ Installation

You may download the standalone binary and run it as,

./chipku
./chipku serve

By default the binary starts listening on port `8080`. To change this, please set the port using `--port` flag.

./chipku serve --port=8081

Or you may download the project and build (or run) from source,

go run .
go build -o chipku && ./chipku serve
# or
go build -o chipku && ./chipku
# or
go install && chipku

go install && chipku serve

Usage
-----
Expand All @@ -38,4 +39,4 @@ Or you can simply use command-line to `PUT` your text, using `httpie`

To get your snippet in command-line, set the `No-Html` HTTP header,

![httpie-get](docs/httpie-get.png "httpie-get")
![httpie-get](docs/httpie-get.png "httpie-get")
15 changes: 11 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"

"github.com/abhi-g80/chipku/server"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Expand All @@ -14,13 +15,19 @@ var rootCmd = &cobra.Command{
May be used for sharing snippets with your loved ones and colleagues.
Partial documentation is available at http://github.com/abhi-go/chipku`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("And apparently, this is fun ?!")
// print cmd help if no serve subcommand isn't invoked
err := cmd.Help()
if err != nil {
server.LogError(err.Error())
os.Exit(1)
}
},
}

// Execute try to run the root command
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
server.LogError(err.Error())
os.Exit(1)
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ go 1.16
require (
github.com/gorilla/mux v1.8.0
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/kyoh86/richgo v0.3.10 // indirect
github.com/spf13/cobra v1.5.0
)
20 changes: 0 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kyoh86/richgo v0.3.10 h1:iSGvcjhtQN2IVrBDhPk0if0R/RMQnCN1E/9OyAW4UUs=
github.com/kyoh86/richgo v0.3.10/go.mod h1:2Odx7Qx2iJxXEWmoXVWaNbUcBXhHY43qSgyK7dbTaII=
github.com/kyoh86/xdg v1.2.0 h1:CERuT/ShdTDj+A2UaX3hQ3mOV369+Sj+wyn2nIRIIkI=
github.com/kyoh86/xdg v1.2.0/go.mod h1:/mg8zwu1+qe76oTFUBnyS7rJzk7LLC0VGEzJyJ19DHs=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/wacul/ptr v1.0.0 h1:FIKu08Wx0YUIf9MNsfF62OCmBSmz5A1Tk65zWhOIL/I=
github.com/wacul/ptr v1.0.0/go.mod h1:BD0gjsZrCwtoR+yWDB9v2hQ8STlq9tT84qKfa+3txOc=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211030160813-b3129d9d1021 h1:giLT+HuUP/gXYrG2Plg9WTjj4qhfgaW424ZIFog3rlk=
golang.org/x/sys v0.0.0-20211030160813-b3129d9d1021/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
9 changes: 6 additions & 3 deletions server/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ const (

var logger = log.New(os.Stdout, "["+BLUE+"chipku"+RESET+"] ", log.LstdFlags|log.Lmicroseconds)

// LogInfo stylized Info logger
func LogInfo(format string, a ...interface{}) {
logger.Printf(PURPLE+"[info] "+RESET+format, a...)
logger.Printf("["+PURPLE+"info "+RESET+"] "+format, a...)
}

// LogDebug stylized debug logger
func LogDebug(format string, a ...interface{}) {
logger.Printf(YELLOW+"[debug] "+RESET+format, a...)
logger.Printf("["+YELLOW+"debug"+RESET+"] "+format, a...)
}

// LogError stylized error logger
func LogError(format string, a ...interface{}) {
logger.Printf(RED+"[error] "+RESET+format, a...)
logger.Printf("["+RED+"error"+RESET+"] "+format, a...)
}
7 changes: 5 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var chipkus = map[string]string{}

var letterBytes = "abcdefghijklmnopqrstuvwxyz"

// RandStringBytes exported
// RandStringBytes return a random string bytes of size n
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
Expand Down Expand Up @@ -153,12 +153,14 @@ func fetchHandler(w http.ResponseWriter, r *http.Request) {
LogError("something went wrong while templating code %s", err)
}
} else {
LogInfo("invalid id %s requested by %s", id, r.RemoteAddr)
fmt.Fprintf(w, "Invalid id %s provided :(", id)
}
}

func pastePostHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
LogError("ParseForm() err: %v", err)
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
Expand All @@ -173,7 +175,8 @@ func pastePostHandler(w http.ResponseWriter, r *http.Request) {
func pastePutHandler(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Printf("\033[0;31m[Error]\033[0m -> while reading body")
LogError("while reading body = %v", b)
return
}
hashVal := store(string(b))
LogInfo("new %s request from connection from %s", r.Method, r.RemoteAddr)
Expand Down

0 comments on commit f555095

Please sign in to comment.