Compare commits
No commits in common. "main" and "v0.0.6" have entirely different histories.
17 changed files with 54 additions and 353 deletions
|
@ -1,41 +0,0 @@
|
||||||
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 }}
|
|
|
@ -1,67 +0,0 @@
|
||||||
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"
|
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
---
|
|
||||||
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
|
|
|
@ -22,7 +22,3 @@ 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
|
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`.
|
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`.
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
#!/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
|
|
16
cli/build.go
16
cli/build.go
|
@ -13,22 +13,14 @@ func getBuildCommand() *cobra.Command {
|
||||||
Use: "build",
|
Use: "build",
|
||||||
Short: "Build service images.",
|
Short: "Build service images.",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var pathProvided string
|
pathProvided, err := cmd.PersistentFlags().GetString("definition")
|
||||||
var fetcher service_definition.DefinitionFetcher
|
|
||||||
var def service_definition.ServiceDefinition
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if pathProvided, err = cmd.PersistentFlags().GetString("definition"); err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%+v", err)
|
return fmt.Errorf("%+v", err)
|
||||||
}
|
}
|
||||||
|
def, err := service_definition.GetServiceDefinitionFromFile(pathProvided)
|
||||||
|
|
||||||
defPathType := service_definition.GetPathType(pathProvided)
|
if err != nil {
|
||||||
|
|
||||||
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)
|
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,22 +47,12 @@ func getStartCommand() *cobra.Command {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var fetcher service_definition.DefinitionFetcher
|
|
||||||
var def service_definition.ServiceDefinition
|
|
||||||
var err error
|
|
||||||
|
|
||||||
ctx := cmd.Context()
|
ctx := cmd.Context()
|
||||||
flags := ctx.Value("flags").(ParsedFlags)
|
flags := ctx.Value("flags").(ParsedFlags)
|
||||||
|
def, err := service_definition.GetServiceDefinitionFromFile(flags.definitionPath)
|
||||||
|
|
||||||
defPath := flags.definitionPath
|
if err != nil {
|
||||||
defPathType := service_definition.GetPathType(defPath)
|
return fmt.Errorf("Failed to read service definition from file: %+v", err)
|
||||||
|
|
||||||
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 {
|
if flags.daemonHost != "" && flags.daemonPort != 0 {
|
||||||
|
|
26
git/main.go
26
git/main.go
|
@ -1,26 +0,0 @@
|
||||||
// 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
|
|
||||||
}
|
|
24
service_definition/errors.go
Normal file
24
service_definition/errors.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
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
|
||||||
|
}
|
|
@ -1,26 +0,0 @@
|
||||||
// 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)
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
// 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
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
// 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"))
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
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])
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,11 @@
|
||||||
package service_definition
|
package service_definition
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/goccy/go-yaml"
|
||||||
|
)
|
||||||
|
|
||||||
type BuildImage struct {
|
type BuildImage struct {
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
TagPrefix string `yaml:"tag"`
|
TagPrefix string `yaml:"tag"`
|
||||||
|
@ -43,3 +49,19 @@ type ServiceDefinition struct {
|
||||||
Containers []ContainerDefinition `yaml:"containers"`
|
Containers []ContainerDefinition `yaml:"containers"`
|
||||||
Ports []PortMapping `yaml:"ports"`
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package service_definition
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetPathType(path string) string {
|
|
||||||
if strings.HasPrefix(path, "git+") {
|
|
||||||
return "git"
|
|
||||||
}
|
|
||||||
|
|
||||||
return "file"
|
|
||||||
}
|
|
|
@ -5,8 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
func TestGetServiceDefinitionFromFileDoesNotExist(t *testing.T) {
|
||||||
fetcher, _ := NewDefinitionFetcher("file")
|
_, err := GetServiceDefinitionFromFile(t.TempDir() + "/not-a-file.yml")
|
||||||
_, err := fetcher.GetDefinition(t.TempDir() + "/not-a-file.yml")
|
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error, got nil.")
|
t.Errorf("Expected error, got nil.")
|
||||||
|
|
Loading…
Reference in a new issue