Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickkirschen committed Nov 15, 2024
0 parents commit 503ea87
Show file tree
Hide file tree
Showing 19 changed files with 506 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
tab_width = 4
indent_size = 4
max_line_length = 120
insert_final_newline = true
trim_trailing_whitespace = true

[{*.md,LICENSE}]
max_line_length = 80
trim_trailing_whitespace = false

[{*.yml,*.yaml,*.json,*.js,*.ts,.prettierrc,*.html,*.css,*.scss}]
indent_size = 2
tab_width = 2

[{*.go}]
# Go uses tabs and gofmt emits them by default.
indent_style = tab

[{Makefile,.gitmodules}]
# Makefiles and .gitmodules must (!) use tabs.
indent_style = tab
34 changes: 34 additions & 0 deletions .github/commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
rules: {
'body-leading-blank': [2, 'always'],
'body-max-line-length': [2, 'always', 100],
'header-max-length': [2, 'always', 100],
'scope-case': [2, 'always', 'lower-case'],
'subject-case': [
2,
'never',
['start-case', 'pascal-case', 'upper-case'],
],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'type-enum': [
2,
'always',
[
'chore',
'build',
'ci',
'docs',
'feat',
'feat!',
'fix',
'perf',
'refactor',
'test'
],
],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'signed-off-by': [2, 'always']
}
};
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2

updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/.github/workflows"
schedule:
interval: "daily"
25 changes: 25 additions & 0 deletions .github/workflows/commit-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint commit message

on:
push:
branches: ["**"]

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Clone
uses: actions/checkout@v4

- name: Setup node 20
uses: actions/setup-node@v4
with:
node-version: "20"
check-latest: true

- name: Install commitlint
run: npm install -g @commitlint/cli @commitlint/config-conventional

- name: Lint commit message
run: echo "${{ github.event.head_commit.message }}" | commitlint --config .github/commitlint.config.js
60 changes: 60 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Go Workflow (Push)

on:
push:
branches: ["**"]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23"
check-latest: true

- name: Build
run: go build ./...

test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23"
check-latest: true

- name: Test
run: go test -json ./...

lint:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23"
check-latest: true

- name: Format
run: |
OUT="$(go fmt $(go list ./... | grep -v /vendor/) 2>&1)"
if [ -n "$OUT" ]; then
echo "The following files are not correctly formatted"
echo "${OUT}"
exit 1
fi
60 changes: 60 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Go Workflow (Release)

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23"
check-latest: true

- name: Build
run: go build ./...

test:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23"
check-latest: true

- name: Test
run: go test -json ./...

lint:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.23"
check-latest: true

- name: Format
run: |
OUT="$(go fmt $(go list ./... | grep -v /vendor/) 2>&1)"
if [ -n "$OUT" ]; then
echo "The following files are not correctly formatted"
echo "${OUT}"
exit 1
fi
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

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

# Go workspace file
go.work
go.work.sum

# env file
.env

.DS_Store
.idea/
*.iml
.settings
12 changes: 12 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"recommendations": [
"streetsidesoftware.code-spell-checker",
"editorconfig.editorconfig",
"davidanson.vscode-markdownlint",
"sonarsource.sonarlint-vscode",
"wayou.vscode-todo-highlight",
"redhat.vscode-yaml",
"ms-vscode.makefile-tools",
"golang.go"
]
}
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"cSpell.language": "en,de",
"cSpell.words": [
"commitlint",
"github",
"gomod",
"yannickkirschen"
],
"editor.formatOnSave": true,
"editor.wordWrap": "on",
"editor.rulers": [
80,
120
]
}
8 changes: 8 additions & 0 deletions .whitesource
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"checkRunSettings": {
"vulnerableCheckRunConclusionLevel": "failure"
},
"issueSettings": {
"minSeverityLevel": "LOW"
}
}
5 changes: 5 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Code of Conduct

Actually, the author is hungry and does not want to write a novel here. So,
please just act like a normal human and treat others the same way as you want to
be treated.
22 changes: 22 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Contributing

When contributing to this repository, please first discuss the change in the
discussions.

Please note we have a code of conduct, please follow it in all your interactions
with the project.

## Coding

1. Fork the repository.
2. Make your changes on a dedicated branch.
3. Commit your changes according
to [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0).
Please try to only make one commit. Squash/rebase if you made a bunch of
fix-up-commits! Commits will be linted on every push.
4. Open up a pull request.
5. Merge the pull request and make a [release](#release) if required.

## Release

Ask the maintainer how to make a release.
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# State Machine

[![Lint commit message](https://github.com/yannickkirschen/state-machine/actions/workflows/commit-lint.yml/badge.svg)](https://github.com/yannickkirschen/state-machine/actions/workflows/commit-lint.yml)
[![Push](https://github.com/yannickkirschen/state-machine/actions/workflows/push.yml/badge.svg)](https://github.com/yannickkirschen/state-machine/actions/workflows/push.yml)
[![Release](https://github.com/yannickkirschen/state-machine/actions/workflows/release.yml/badge.svg)](https://github.com/yannickkirschen/state-machine/actions/workflows/release.yml)
[![GitHub release](https://img.shields.io/github/release/yannickkirschen/state-machine.svg)](https://github.com/yannickkirschen/state-machine/releases/)

State Machine is a Golang library implementing a [finite-state machine](https://en.wikipedia.org/wiki/Finite-state_machine).

## Usage

Basic usage works as follows:

1. Create a machine with an initial state.
2. Set all possible transitions between states.
3. Optional: Set enter end exit actions.
4. Transition from state to state ;)

Here is an example code based on the very simple use-case of a door:

```go
// Signature: <initial state>
machine := fsm.NewMachine("close")

// Signature: <current state, input event, next state>
machine.SetTransition("open", "close-door", "close")
machine.SetTransition("close", "open-door", "open")

machine.SetEnterAction(func(last, new fsm.State) error {
fmt.Printf("Enter '%s' coming from '%s'\n", new, last)
return nil
})

machine.SetExitAction(func(current, next fsm.State) error {
fmt.Printf("Leaving '%s' going to '%s'\n", current, next)
return nil
})

// Signature: <input event>
if err := machine.Transition("open-door"); err != nil {
panic(err)
}
```
Loading

0 comments on commit 503ea87

Please sign in to comment.