Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

feat: Adding ping command, usage docs and release utils #98

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.github
build
44 changes: 44 additions & 0 deletions .github/releaser/release_cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# Set –e is used within the Bash to stop execution instantly as a query exits
# while having a non-zero status.
set -e

ROOT_DIR="$(pwd)"
VERSION="$(echo `git -C ${ROOT_DIR} describe --abbrev=0 --tags` | sed 's/^.//')" # "v1.2.3" -> "1.2.3"
PACKAGE_NAME="ttrace_${VERSION}"


# https://go.dev/doc/install/source#environment

for OS_ARCH in \
"linux amd64" "linux arm64" \
"android arm64" \
"freebsd amd64" "freebsd arm" \
"darwin amd64" "darwin arm64" \
"windows 386" "windows amd64"; do

PAIR=($OS_ARCH);
OS=${PAIR[0]};
ARCH=${PAIR[1]};

cd ${ROOT_DIR}

PACKAGE_NAME_OS=${PACKAGE_NAME}_${OS}_${ARCH}
BUILD_DIR=${ROOT_DIR}/build/${PACKAGE_NAME_OS}

if [ $OS = "windows" ]; then
EXE=".exe"
fi

CGO_ENABLED=0 GOOS=${OS} GOARCH=${ARCH} go build -ldflags "-s -w" -trimpath -o ${BUILD_DIR}/${PACKAGE_NAME}/ttrace${EXE} ./cmd/mian.go

cd ${BUILD_DIR}
if [ $OS = "windows" ]; then
zip -r ${PACKAGE_NAME_OS}.zip ${PACKAGE_NAME}
mv ${PACKAGE_NAME_OS}.zip ${ROOT_DIR}
else
tar -czvf ${PACKAGE_NAME_OS}.tar.gz -p ${PACKAGE_NAME}
mv ${PACKAGE_NAME_OS}.tar.gz ${ROOT_DIR}
fi
done
36 changes: 36 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Building Docker and Push to DockerHub

on:
push:
tags:
- v*
branches:
- main

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Log in to Docker Hub
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: pactus/pactus

- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

69 changes: 69 additions & 0 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Releaser

on:
push:
tags:
- v*

jobs:
########################################
build-cli:
runs-on: ubuntu-latest

# Defining outputs for jobs
# https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs
outputs:
checksums: ${{ steps.calc_checksums.outputs.checksums }}

steps:
- uses: actions/checkout@v3

- name: Install Dependencies
run: |
sudo apt update
sudo apt install zip

- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.21.1

- name: Create release files
run: bash ./.github/releasers/releaser_cli.sh

# Multiline strings in GitHub actions
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
- name: Calculate sha256sum
id: calc_checksums
run: |
set -e
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "checksums<<$EOF" >> "$GITHUB_OUTPUT"
echo "$(sha256sum pactus-*.zip pactus-*tar.gz)" >> "$GITHUB_OUTPUT"
echo "$EOF" >> "$GITHUB_OUTPUT"

- name: Publish
uses: softprops/action-gh-release@v1
with:
files: |
pactus-*.zip
pactus-*.tar.gz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}


checksums:
needs: [build-cli]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Create SHA256SUMS file
run: |
echo "${{ needs.build-cli.outputs.checksums }}" >> SHA256SUMS
- name: Publish
uses: softprops/action-gh-release@v1
with:
files: SHA256SUMS
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.21.5-alpine3.18 as builder

RUN apk add --no-cache git gmp-dev build-base g++ openssl-dev
ADD . /ttrace

# Building ttrace-cli
RUN cd /ttrace && \
go build -ldflags "-s -w" -trimpath -o ./build/ttrace ./cmd/main.go


## Copy binary files from builder into second container
FROM golang:1.21.5-alpine3.18

COPY --from=builder /timetrace/build/ttrace /usr/bin

ENV WORKING_DIR "/ttrace"

VOLUME $WORKING_DIR
WORKDIR $WORKING_DIR
42 changes: 42 additions & 0 deletions cmd/commands/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package commands

import (
"fmt"
"net"
"os"

cobra "github.com/spf13/cobra"
)

func PingCommand(parentCmd *cobra.Command) {
ping := &cobra.Command{
Use: "ping",
Short: "Ping a remote instance of time trace.",
}
parentCmd.AddCommand(ping)

address := ping.Flags().StringP("address", "a", "localhost:7070", "remote address of your time trace instance.")
username := ping.Flags().StringP("username", "u", "root", "username of the user you are going to connect with.")
password := ping.Flags().StringP("password", "p", "", "password of user trying to connect with.")

ping.Run = func(cmd *cobra.Command, args []string) {
conn, err := net.Dial("tcp", *address)
if err != nil {
Dead(cmd, err)
}
defer conn.Close()

conQuery := fmt.Sprintf("CON %v %v", *username, *password)

do(conn, conQuery)

response := do(conn, "PING")
if response == "PONG" {
cmd.Println("PONG, everything is ok.")
os.Exit(0)
} else {
cmd.Printf("something is wrong: %v", response)
os.Exit(1)
}
}
}
4 changes: 2 additions & 2 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func REPLCommand(parentCmd *cobra.Command) {
connect.Run = func(cmd *cobra.Command, args []string) {
conn, err := net.Dial("tcp", *address)
if err != nil {
dead(cmd, err)
Dead(cmd, err)
}
defer conn.Close()

Expand All @@ -51,7 +51,7 @@ func REPLCommand(parentCmd *cobra.Command) {
cmd.Print(do(conn, input))
}
} else {
dead(cmd, errors.New(response)) //nolint
Dead(cmd, errors.New(response)) //nolint
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ func RunCommand(parentCmd *cobra.Command) {

run.Run = func(cmd *cobra.Command, args []string) {
if confingPath == nil || *confingPath == "" {
dead(cmd, tte.ErrInavlidConfigPath)
Dead(cmd, tte.ErrInavlidConfigPath)
}

cfg, err := config.LoadFromFile(*confingPath)
if err != nil {
dead(cmd, err)
Dead(cmd, err)
}

db := database.Init(cfg)
ttlog.InitGlobalLogger(&cfg.Log)

server := server.NewServer(cfg, db)
if err := server.Start(); err != nil {
dead(cmd, err)
Dead(cmd, err)
}
}
}
2 changes: 1 addition & 1 deletion cmd/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

func dead(cmd *cobra.Command, err error) {
func Dead(cmd *cobra.Command, err error) {
cmd.PrintErrln(err)
os.Exit(1)
}
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func main() {

commands.RunCommand(rootCmd)
commands.REPLCommand(rootCmd)
commands.PingCommand(rootCmd)

err := rootCmd.Execute()
if err != nil {
panic(err)
commands.Dead(rootCmd, err)
}
}
1 change: 1 addition & 0 deletions core/TQL/execute/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ExecutorMap map[string]Executor
var Executors ExecutorMap = ExecutorMap{
"SET": database.IDataBase.AddSet,
"CON": database.IDataBase.Connect,
"PING": database.IDataBase.Ping,
"SSET": database.IDataBase.AddSubSet,
"PUSH": database.IDataBase.PushElement,
"DRPS": database.IDataBase.DropSet,
Expand Down
4 changes: 4 additions & 0 deletions core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (db *Database) Connect(args []string) string {
return INVALID
}

func (db *Database) Ping(_ []string) string {
return PONG
}

func (db *Database) AddSet(args []string) string {
db.Lock()
defer db.Unlock()
Expand Down
1 change: 1 addition & 0 deletions core/database/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type IDataBase interface {
SetsMap() Sets

Connect([]string) string
Ping([]string) string
AddSet([]string) string
AddSubSet([]string) string
PushElement([]string) string
Expand Down
1 change: 1 addition & 0 deletions core/database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
const (
INVALID = "INVALID"
DONE = "DONE"
PONG = "PONG"
SET_NOT_FOUND = "SNF"
SUB_SET_NOT_FOUND = "SSNF"
ELEMENT_NOT_FOUND = "ENF"
Expand Down
3 changes: 2 additions & 1 deletion doc/TQL/TQL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Time trace is using a query language called TQL. Here is documentation and speci

| Command | Action | Arguments |
|----------|:-------------|:------|
| CON | to make a connection and access to database | username - password |
| CON * | to make a connection and access to database | username - password |
| PING * | should send a `PONG` back if everything is ok | |
| SET * | make a new set | set-name |
| SSET * | make a new subset | set-name - subset-name |
| PUSH * | push an element to a subset | set-name - subset-name - value-of-element - time(unix-timestamp) |
Expand Down
Empty file removed doc/usage/.keep
Empty file.
2 changes: 2 additions & 0 deletions doc/usage/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Usage of time trace