Compare commits

..

7 commits
v0.0.6 ... main

Author SHA1 Message Date
98fda3b404
refactor(deadcode): unused custom errors
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m9s
Pull-Request / static-analysis (pull_request) Successful in 1m38s
Pull-Request / post-run (pull_request) Successful in 30s
Push / pre-run (push) Successful in 26s
Push / tests (push) Successful in 1m4s
Push / static-analysis (push) Successful in 1m28s
Push / post-run (push) Successful in 40s
2024-11-13 23:59:55 -05:00
7cc11af378
refactor: split fetcher by type, reorganize fetcher tests 2024-11-13 23:56:19 -05:00
5a18a5bf44
refactor: isolate + inject DefinitionFetcher
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m6s
Pull-Request / static-analysis (pull_request) Successful in 1m31s
Pull-Request / post-run (pull_request) Successful in 24s
Push / pre-run (push) Successful in 28s
Push / tests (push) Successful in 1m3s
Push / static-analysis (push) Successful in 1m29s
Push / post-run (push) Successful in 38s
docs: add documentation to core functions and structs
2024-11-10 23:05:57 -05:00
8bc33870e6
feat: fetch service def from git
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m6s
Pull-Request / static-analysis (pull_request) Successful in 1m31s
Pull-Request / post-run (pull_request) Successful in 24s
2024-11-10 10:52:06 -05:00
c580c0c433
refactor: generalize service definition fetch 2024-11-10 10:37:46 -05:00
24e795e1aa
ci: run pre-commit on pr + push
All checks were successful
Pull-Request / tests (pull_request) Successful in 1m8s
Pull-Request / static-analysis (pull_request) Successful in 1m33s
Pull-Request / post-run (pull_request) Successful in 26s
Push / pre-run (push) Successful in 28s
Push / tests (push) Successful in 1m7s
Push / static-analysis (push) Successful in 1m33s
Push / post-run (push) Successful in 36s
2024-11-10 10:25:33 -05:00
8f0489a622
build: add pre-commit tooling + basic go/shell/yaml hooks
docs: mention pre-commit
2024-11-10 10:25:30 -05:00
17 changed files with 353 additions and 54 deletions

View file

@ -0,0 +1,41 @@
name: Pull-Request
on: [pull_request]
jobs:
static-analysis:
runs-on: runner-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Validate Yaml
run: pipx run pre-commit run check-yaml -a
- name: Validate shell scripts
run: pipx run pre-commit run shellcheck -a
- name: Check formatting
run: pipx run pre-commit run go-fmt -a
- name: Check code patterns
run: pipx run pre-commit run go-vet-mod -a
tests:
runs-on: runner-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Tests
run: pipx run pre-commit run go-test-mod -a
post-run:
runs-on: runner-latest
needs: [static-analysis,tests]
if: ${{ always() }}
steps:
- name: Export trace
uses: https://forge.karnov.club/marc/opentelemetry-trace-export-forgejo-action@main
with:
otlp-endpoint: "http://otel.home.karnov.club:4318"
forgejo-token: ${{ secrets.GITHUB_TOKEN }}
forgejo-base-url: ${{ env.GITHUB_SERVER_URL }}
run-id: ${{ env.GITHUB_RUN_NUMBER }}
repo-name: ${{ env.GITHUB_REPOSITORY }}

View file

@ -0,0 +1,67 @@
name: Push
on:
push:
branches: [main]
jobs:
pre-run:
runs-on: runner-latest
steps:
- uses: https://forge.karnov.club/marc/push-status-to-discord-action@main
with:
webhook-url: ${{secrets.DISCORD_WEBHOOK_URL}}
status: "Started"
init: true
static-analysis:
runs-on: runner-latest
needs: [pre-run]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Validate Yaml
run: pipx run pre-commit run check-yaml -a
- name: Validate shell scripts
run: pipx run pre-commit run shellcheck -a
- name: Check formatting
run: pipx run pre-commit run go-fmt -a
- name: Check code patterns
run: pipx run pre-commit run go-vet-mod -a
tests:
runs-on: runner-latest
needs: [pre-run]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Tests
run: pipx run pre-commit run go-test-mod -a
post-run:
runs-on: runner-latest
needs: [static-analysis,tests]
steps:
- name: Export trace
uses: https://forge.karnov.club/marc/opentelemetry-trace-export-forgejo-action@main
with:
otlp-endpoint: "http://otel.home.karnov.club:4318"
forgejo-token: ${{ secrets.GITHUB_TOKEN }}
forgejo-base-url: ${{ env.GITHUB_SERVER_URL }}
run-id: ${{ env.GITHUB_RUN_NUMBER }}
repo-name: ${{ env.GITHUB_REPOSITORY }}
- name: Notify success
uses: https://forge.karnov.club/marc/push-status-to-discord-action@main
if: ${{always() && success()}}
with:
webhook-url: ${{secrets.DISCORD_WEBHOOK_URL}}
status: "Success"
variant: "success"
- name: Notify failure
uses: https://forge.karnov.club/marc/push-status-to-discord-action@main
if: ${{always() && failure()}}
with:
webhook-url: ${{secrets.DISCORD_WEBHOOK_URL}}
status: "Failure"
variant: "failure"

16
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,16 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-yaml
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
hooks:
- id: shellcheck
- repo: https://github.com/tekwizely/pre-commit-golang
rev: master
hooks:
- id: go-test-mod
- id: go-fmt
- id: go-vet-mod

View file

@ -22,3 +22,7 @@ curl https://forge.karnov.club/spadinastan/spud/raw/branch/main/install.sh | bas
To pull a specific version, supply a `$SPUD_VERSION` that corresponds to an existing release tag, and to control where
the binary is unpacked and installed, supply `$SPUD_ROOT`.
## Development
The `bootstrap.sh` script should be run to prepare any pre-commit hooks and other required tooling for local development. Checks can be run manually via `pipx run pre-commit run -a`.

8
bootstrap.sh Executable file
View file

@ -0,0 +1,8 @@
#!/bin/bash
if [[ -z "$(command -v pipx)" ]]; then
echo "ERROR: Pre-commit tooling requires pipx to be available."
exit 1
fi
pipx run pre-commit install

View file

@ -13,14 +13,22 @@ func getBuildCommand() *cobra.Command {
Use: "build",
Short: "Build service images.",
RunE: func(cmd *cobra.Command, args []string) error {
pathProvided, err := cmd.PersistentFlags().GetString("definition")
var pathProvided string
var fetcher service_definition.DefinitionFetcher
var def service_definition.ServiceDefinition
var err error
if err != nil {
if pathProvided, err = cmd.PersistentFlags().GetString("definition"); err != nil {
return fmt.Errorf("%+v", err)
}
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
if err != nil {
defPathType := service_definition.GetPathType(pathProvided)
if fetcher, err = service_definition.NewDefinitionFetcher(defPathType); err != nil {
return fmt.Errorf("Couldn't set up fetcher from the path provided.")
}
if def, err = fetcher.GetDefinition(pathProvided); err != nil {
return fmt.Errorf("Failed to read service definition from file: %+v", err)
}

View file

@ -47,12 +47,22 @@ func getStartCommand() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var fetcher service_definition.DefinitionFetcher
var def service_definition.ServiceDefinition
var err error
ctx := cmd.Context()
flags := ctx.Value("flags").(ParsedFlags)
def, err := service_definition.GetServiceDefinitionFromFile(flags.definitionPath)
if err != nil {
return fmt.Errorf("Failed to read service definition from file: %+v", err)
defPath := flags.definitionPath
defPathType := service_definition.GetPathType(defPath)
if fetcher, err = service_definition.NewDefinitionFetcher(defPathType); err != nil {
return fmt.Errorf("Couldn't set up fetcher from the path provided.")
}
if def, err = fetcher.GetDefinition(defPath); err != nil {
return fmt.Errorf("Failed to read service definition: %+v", err)
}
if flags.daemonHost != "" && flags.daemonPort != 0 {

26
git/main.go Normal file
View file

@ -0,0 +1,26 @@
// Git wrapper
//
// Facilitates the usage of `git` commands when dealing with
// data living in repositories.
package git
import (
"os/exec"
)
type GitClient interface {
Clone(path string, destination string) (string, error)
}
type Git struct{}
func (g Git) Clone(path string, destination string) (string, error) {
cloneCmd := exec.Command("git", "clone", path, destination)
if err := cloneCmd.Run(); err != nil {
return "", err
}
return path, nil
}

View file

@ -1,24 +0,0 @@
package service_definition
type FileDoesNotExistError struct {
Message string
ExpectedPath string
}
func (r *FileDoesNotExistError) Error() string {
prefix := "File not found"
if r.Message != "" {
prefix = r.Message
}
return prefix + ": " + r.ExpectedPath
}
type InvalidServiceDefinitionError struct {
Path string
}
func (r *InvalidServiceDefinitionError) Error() string {
return "Service definition does not satisfy expected schema: " + r.Path
}

View file

@ -0,0 +1,26 @@
// Definition fetcher
//
// Handles fetching and building ServiceDefinition structs from different
// data sources.
package service_definition
import (
"fmt"
)
type DefinitionFetcher interface {
GetDefinition(path string) (ServiceDefinition, error)
}
func NewDefinitionFetcher(fetcher_type string) (DefinitionFetcher, error) {
if fetcher_type == "git" {
return NewGitDefinitionFetcher(), nil
}
if fetcher_type == "file" {
return NewFileDefinitionFetcher(), nil
}
return nil, fmt.Errorf("Unrecognized fetcher type: %s", fetcher_type)
}

View file

@ -0,0 +1,19 @@
package service_definition
import (
"testing"
)
func TestGetPathTypeDetectsGitPathPrefix(t *testing.T) {
actual := GetPathType("git+https://test.com")
if actual != "git" {
t.Errorf("Expected 'git' type, got %s", actual)
}
}
func TestGetPathTypeDetectsFileNoPrefix(t *testing.T) {
actual := GetPathType("file/path.yml")
if actual != "file" {
t.Errorf("Expected 'file' type, got %s", actual)
}
}

View file

@ -0,0 +1,34 @@
// File Definition fetcher
//
// Handles extracting service definitions from local files.
package service_definition
import (
"fmt"
"github.com/goccy/go-yaml"
"os"
)
type FileDefinitionFetcher struct{}
func NewFileDefinitionFetcher() *FileDefinitionFetcher {
return &FileDefinitionFetcher{}
}
// Retrieves a service definition from the given filepath.
func (f FileDefinitionFetcher) GetDefinition(path string) (ServiceDefinition, error) {
var definition ServiceDefinition
defData, err := os.ReadFile(path)
if err != nil {
return ServiceDefinition{}, fmt.Errorf("Could not find service configuration file: %s", path)
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
return ServiceDefinition{}, fmt.Errorf("Service definition does not satisfy expected schema: %s", path)
}
return definition, nil
}

View file

@ -0,0 +1,38 @@
// Git Definition fetcher
//
// Handles fetching and building ServiceDefinition structs from git
// repositories.
package service_definition
import (
"os"
git "spud/git"
"strings"
)
type GitDefinitionFetcher struct {
Git git.GitClient
}
func NewGitDefinitionFetcher() *GitDefinitionFetcher {
return &GitDefinitionFetcher{
Git: git.Git{},
}
}
// Clones the target git repository and uses it as a basis to extract
// a service definition.
func (f GitDefinitionFetcher) GetDefinition(path string) (ServiceDefinition, error) {
dir, err := os.MkdirTemp("/tmp", "spud-service-")
if err != nil {
return ServiceDefinition{}, err
}
if _, err := f.Git.Clone(strings.TrimPrefix(path, "git+"), dir); err != nil {
return ServiceDefinition{}, err
}
return NewFileDefinitionFetcher().GetDefinition((dir + "/service.yml"))
}

View file

@ -0,0 +1,34 @@
package service_definition
import (
"strings"
"testing"
)
type MockGit struct {
calls []string
}
func (g *MockGit) Clone(path string, destination string) (string, error) {
g.calls = append(g.calls, path+":"+destination)
return path, nil
}
func TestGetDefinitionGetDefinitionFromGit(t *testing.T) {
var gitFetcher GitDefinitionFetcher
fetcher, _ := NewDefinitionFetcher("git")
gitFetcher, _ = fetcher.(GitDefinitionFetcher)
mockGit := MockGit{}
gitFetcher.Git = &mockGit
mockUrl := "https://git.com/owner/repo.git"
gitFetcher.GetDefinition("git+" + mockUrl)
if !strings.HasPrefix(mockGit.calls[0], mockUrl) {
t.Errorf("Expected git cloning for %s, got %s instead.", mockUrl, mockGit.calls[0])
}
}

View file

@ -1,11 +1,5 @@
package service_definition
import (
"os"
"github.com/goccy/go-yaml"
)
type BuildImage struct {
Path string `yaml:"path"`
TagPrefix string `yaml:"tag"`
@ -49,19 +43,3 @@ type ServiceDefinition struct {
Containers []ContainerDefinition `yaml:"containers"`
Ports []PortMapping `yaml:"ports"`
}
func GetServiceDefinitionFromFile(path string) (ServiceDefinition, error) {
var definition ServiceDefinition
defData, err := os.ReadFile(path)
if err != nil {
return ServiceDefinition{}, &FileDoesNotExistError{Message: "Could not find service configuration file", ExpectedPath: path}
}
if err = yaml.Unmarshal(defData, &definition); err != nil {
return ServiceDefinition{}, &InvalidServiceDefinitionError{Path: path}
}
return definition, nil
}

View file

@ -0,0 +1,13 @@
package service_definition
import (
"strings"
)
func GetPathType(path string) string {
if strings.HasPrefix(path, "git+") {
return "git"
}
return "file"
}

View file

@ -5,7 +5,8 @@ import (
)
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
_, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml")
fetcher, _ := NewDefinitionFetcher("file")
_, err := fetcher.GetDefinition(t.TempDir() + "/not-a-file.yml")
if err == nil {
t.Errorf("Expected error, got nil.")