Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Zdenko Vrabel committed May 10, 2022
1 parent 8e26647 commit b905725
Show file tree
Hide file tree
Showing 30 changed files with 1,398 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: build

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '>=1.18.0'

- name: test
run: |
go test ./...
77 changes: 77 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: release

on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
build:
name: Release
permissions: write-all
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '>=1.18.0'

- run: |
rm -f ./toolbx || true
env GOOS=linux GOARCH=amd64 go build ./cmd/toolbx
tar -czvf toolbx-${{github.ref_name}}-linux-64bit.tar.gz b./toolbx
- run: |
rm -f ./toolbx || true
env GOOS=darwin GOARCH=amd64 go build ./cmd/toolbx
tar -czvf toolbx-${{github.ref_name}}-macos-64bit.tar.gz b./toolbx
- run: |
rm -f ./toolbx || true
env GOOS=windows GOARCH=amd64 go build ./cmd/toolbx
zip toolbx-${{github.ref_name}}-win-64bit.zip ./toolbx
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref_name }}
draft: true
prerelease: false

- name: Upload Linux archive
id: upload-linux-archive
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./toolbx-commands-${{github.ref_name}}-linux-64bit.tar.gz
asset_name: toolbx-commands-${{github.ref_name}}-linux-64bit.tar.gz
asset_content_type: application/x-gzip

- name: Upload MacOS archive
id: upload-macos-archive
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./toolbx-commands-${{github.ref_name}}-macos-64bit.tar.gz
asset_name: toolbx-commands-${{github.ref_name}}-macos-64bit.tar.gz
asset_content_type: application/x-gzip

- name: Upload Win archive
id: upload-macos-archive
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./toolbx-commands-${{github.ref_name}}-win-64bit.zip
asset_name: toolbx-commands-${{github.ref_name}}-win-64bit.zip
asset_content_type: application/zip
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# Goland
.idea

# binary
toolbx
39 changes: 39 additions & 0 deletions api/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package api

import (
"runtime"
"strings"
)

type Command struct {
Parent *Command
Name string
Dir string
Args []string
Metadata *Metadata
}

// GetInstallationID returns you identifier in form of a flat
// name with all parents e.g. for command 'hello world' it
// returns 'hello-world'
func (cmd *Command) GetInstallationID() string {
var prefix = ""
if cmd.Parent != nil {
if cmd.Parent.Name != "" {
prefix = cmd.Parent.GetInstallationID() + "-"
}
}
return prefix + cmd.Name
}

// GetPackage returns you package for your OS and ARCH. If there
// is no package for your platform available, then it returns nil
func (cmd *Command) GetPackage() *Package {
platform := strings.ToLower(runtime.GOOS + "-" + runtime.GOARCH)
for _, pkg := range cmd.Metadata.Packages {
if strings.ToLower(pkg.Platform) == platform {
return &pkg
}
}
return nil
}
59 changes: 59 additions & 0 deletions api/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package api

import (
"testing"
)

func Test_InstallationID(t *testing.T) {

// Scenario: resolve installation ID hierarchically by joining all parents
// names and name of the current name.
//
// It's very same logic the Git is using e.g. for subcommand 'one two three'
// the binary will be 'one-two-three'.
t.Run("binaryAsChain", func(t *testing.T) {
level1Cmd := &Command{
Name: "a",
Metadata: &Metadata{},
}

level2Cmd := &Command{
Name: "b",
Parent: level1Cmd,
Metadata: &Metadata{},
}

level3Cmd := &Command{
Name: "c",
Parent: level2Cmd,
Metadata: &Metadata{},
}

binary := level3Cmd.GetInstallationID()
if binary != "a-b-c" {
t.FailNow()
}
})

// Scenario: when root parent have empty name
t.Run("rootWithoutName", func(t *testing.T) {
level1Cmd := &Command{
Name: "",
}

level2Cmd := &Command{
Name: "hello",
Parent: level1Cmd,
}

level3Cmd := &Command{
Name: "subcommand",
Parent: level2Cmd,
}

binary := level3Cmd.GetInstallationID()
if binary != "hello-subcommand" {
t.FailNow()
}
})
}
20 changes: 20 additions & 0 deletions api/installation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package api

import (
"path/filepath"
)

type Installation struct {
ID string `yaml:"id"`
InstalledVersion string `yaml:"installedVersion"`
InstalledCmd string `yaml:"installedCmd"`
}

func (i *Installation) Dir(rootDir string) string {
installationDir := filepath.Join(rootDir, i.ID, i.InstalledVersion)
return installationDir
}

func (i *Installation) Binary(rootDir string) string {
return filepath.Join(i.Dir(rootDir), i.InstalledCmd)
}
8 changes: 8 additions & 0 deletions api/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package api

type Metadata struct {
Version string `yaml:"version"`
Description string `yaml:"description",omitempty`
Usage string `yaml:"usage",omitempty`
Packages []Package `yaml:"packages"`
}
7 changes: 7 additions & 0 deletions api/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package api

type Package struct {
Platform string `yaml:"platform"`
Uri string `yaml:"uri"`
Cmd string `yaml:"cmd",omitempty`
}
19 changes: 19 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package toolbx

type Config struct {
// path to all installed binaries e.g. $TOOLBX/installations
InstallationsPath string `yaml:"installationsPath",omitempty`

// path to all command definitions, it's synced with Git repository (e.g. $TOOLBX/commands)
CommandsPath string `yaml:"commandsPath",omitempty`

// URL to Git repository with commands you want to sync with (e.g. https://gitlab.myorg.com/toolbx-commands.git
Repository string `yaml:"repository",omitempty`

// What to say, branch that will be used for sync
Branch string `yaml:"branch",omitempty`

// path to sync file. It's empty file that serve for ensuring
// sync is executed only once per day ( e.g. $TOOLBX/sync )
SyncFile string `yaml:"syncFile",omitempty`
}
13 changes: 13 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package toolbx

import "errors"

var (
// NoChildError signalising there is no child command of parent
// command
NoChildError = errors.New("no child subcommands")

// NoMetadataError indicates the command.yaml is missing in the subcommand
// folder
NoMetadataError = errors.New("no metadata file")
)
27 changes: 27 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module toolbx

go 1.18

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/net v0.0.0-20210326060303-6b1517762897 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit b905725

Please sign in to comment.