Skip to content

Commit

Permalink
feat: add boilerplate code with support for showing GitHub release UR…
Browse files Browse the repository at this point in the history
…L && add basic CLI support

Signed-off-by: Siddharth Tiwari <[email protected]>
  • Loading branch information
opencloudengineer committed Dec 15, 2020
1 parent 0143dc9 commit 52300f1
Show file tree
Hide file tree
Showing 10 changed files with 545 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
echo "Building app for Version : ${VERSION} && Git Commit SHA : ${GITHUB_SHA}"

set -x

export GO111MODULE=on
export CGO_ENABLED=0

go get ./...

go vet ./...

mkdir -p ./bin

LDFLAGS="-s -w -X github.com/opencloudengineer/gogeta/cmd.Version=${VERSION} -X github.com/opencloudengineer/gogeta/cmd.GitCommit=${GITHUB_SHA}"
FLAGS='-a -installsuffix cgo -o'
BUILD='go build -ldflags'

GOOS=windows ${BUILD} "${LDFLAGS}" ${FLAGS} ./bin/gogeta-windows-amd64.exe
GOOS=darwin ${BUILD} "${LDFLAGS}" ${FLAGS} ./bin/gogeta-darwin-amd64
GOOS=linux ${BUILD} "${LDFLAGS}" ${FLAGS} ./bin/gogeta-linux-amd64
GOOS=linux GOARCH=arm64 ${BUILD} "${LDFLAGS}" ${FLAGS} ./bin/gogeta-linux-arm64
49 changes: 49 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Build Tag & Release

on:
push:
branches:
- main

jobs:
build-tag-release:
strategy:
matrix:
go-version: [ 1.15.x ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 1

- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- name: Conventional Changelog Action
id: changelog
uses: TriPSs/conventional-changelog-action@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
skip-commit: "true"
output-file: "false"

- name: Generate Binaries
run: bash ./.github/build.sh
env:
VERSION: ${{ steps.changelog.outputs.tag }}

- name: Release Binaries
env:
TAG: ${{ steps.changelog.outputs.tag }}
MSG: ${{ steps.changelog.outputs.clean_changelog }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
assets=()
for asset in ./bin/gogeta*; do
assets+=("-a" "$asset")
done
echo -e "$TAG\n\n$MSG" | hub release create "${assets[@]}" -F- "$TAG"
6 changes: 6 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tasks:
- before: >
brew install hadolint;
( cd && go get github.com/jessfraz/dockfmt ) ;
init: go get -v ./...
command: go build .
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM golang:alpine as builder

RUN apk update \
&& apk upgrade \
&& apk add --no-cache \
ca-certificates \
git \
curl \
coreutils \
&& curl -sSL -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
&& curl -sSL -O https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk \
&& apk add --no-cache \
glibc-2.28-r0.apk \
&& rm glibc-2.28-r0.apk \
&& update-ca-certificates 2>/dev/null || true

RUN addgroup -S gogeta \
&& adduser -S gogeta -G gogeta

WORKDIR /app

COPY . .

RUN go get ./...

RUN env GOOS=linux CGO_ENABLED=0 GOARCH=amd64 go build -o gogeta .

FROM scratch

WORKDIR /

COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /app/gogeta .

USER gogeta

CMD [ "/gogeta" ]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# gogeta

Go Get That App
67 changes: 67 additions & 0 deletions cmd/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-github/v33/github"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
"strings"
)

func Github() *cobra.Command {

var command = &cobra.Command{
Use: "github",
Short: `Fetch Github Release(s)`,
Long: `Fetch Github Release(s)`,
Example: `gogeta github github.com/GoogleContainerTools/skaffold
gogeta github /stedolan/jq -m linux64
gogeta github aquasecurity/trivy -m 64bit.deb
gogeta github helm/helm -m linux-amd64
gogeta github https://github.com/starship/starship -m linux-gnu`,
SilenceUsage: true,
Aliases: []string{"gh"},
}

command.Flags().StringP("match", "m", "", `Download release matching a specific pattern.
If no pattern is passed, then all releases are fetched.`)

command.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {

fmt.Println("Pass Repo Address")
return nil
}

match := ""
if command.Flags().Changed("match") {
match, _ = command.Flags().GetString("match")
}
splitURL := strings.Split(args[0], "/")
projectName := splitURL[len(splitURL)-1]
userOrOrgName := splitURL[len(splitURL)-2]

opt := &github.ListOptions{PerPage: 1}
client := github.NewClient(nil)
releasesInfo, _, err := client.Repositories.ListReleases(context.Background(), userOrOrgName, projectName, opt)

if err != nil {
fmt.Println(err)
}

releasesInfoJSON, err := json.Marshal(releasesInfo)
if err != nil {
fmt.Println(err)
}

query := fmt.Sprintf("#.assets.#(name%%\"*%s*\")#.browser_download_url", match)
downloadURL := gjson.Get(string(releasesInfoJSON), query).Array()

fmt.Println(downloadURL[0].Array())

return nil
}
return command
}
30 changes: 30 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

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

var (
Version string
GitCommit string
)

func AppVersion() *cobra.Command {
var command = &cobra.Command{
Use: "version",
Short: "Print Version",
Example: `gogeta version`,
Aliases: []string{"v"},
SilenceUsage: false,
}
command.Run = func(cmd *cobra.Command, args []string) {
if len(Version) == 0 {
fmt.Println("Version: dev")
} else {
fmt.Println("Version:", Version)
}
fmt.Println("Git Commit:", GitCommit)
}
return command
}
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/opencloudengineer/gogeta

go 1.15

require (
github.com/google/go-github/v33 v33.0.0
github.com/spf13/cobra v1.1.1
github.com/tidwall/gjson v1.6.4
)
Loading

0 comments on commit 52300f1

Please sign in to comment.