This commit is contained in:
2025-11-01 20:53:38 +01:00
parent aa8851aca0
commit 6e4452ddb9
12 changed files with 457 additions and 106 deletions

View File

@@ -28,7 +28,7 @@ func New(url string) *Client {
}
func (c *Client) Apply(pr project.Project) error {
url, err := url.JoinPath(c.url, "api", "v1", "projects", pr.Name)
url, err := url.JoinPath(c.url, "api", "v1", "projects")
if err != nil {
return fmt.Errorf("failed to make url: %s", err)
}
@@ -60,6 +60,31 @@ func (c *Client) Apply(pr project.Project) error {
return nil
}
func (c *Client) Version() (obj.SystemInformation, error) {
url, err := url.JoinPath(c.url, "api", "v1", "version")
if err != nil {
return obj.SystemInformation{}, fmt.Errorf("failed to make url: %s", err)
}
res, err := http.Get(url)
if err != nil {
return obj.SystemInformation{}, fmt.Errorf("failed to send the request to the server: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return obj.SystemInformation{}, fmt.Errorf("failed to send the request to the server: %s: %s", res.Status, toError(res.Body))
}
var payload obj.HTTPObject[obj.SystemInformation]
d := json.NewDecoder(res.Body)
if err := d.Decode(&payload); err != nil {
return obj.SystemInformation{}, fmt.Errorf("failed to parse the server response, is your client up-to-date? (reason: %s)", err)
}
return payload.Data, nil
}
func (c *Client) List() ([]project.Project, error) {
url, err := url.JoinPath(c.url, "api", "v1", "projects", "all")
if err != nil {
@@ -92,6 +117,70 @@ func (c *Client) List() ([]project.Project, error) {
return prs, nil
}
func (c *Client) Remove(pr project.Project) error {
url, err := url.JoinPath(c.url, "api", "v1", "projects")
if err != nil {
return fmt.Errorf("failed to make url: %s", err)
}
data, err := json.Marshal(pr)
if err != nil {
return fmt.Errorf("failed to marshal project data: %s", err)
}
r := bytes.NewReader(data)
req, err := http.NewRequest("DELETE", url, r)
if err != nil {
return fmt.Errorf("failed to make request: %s", err)
}
cli := http.Client{}
res, err := cli.Do(req)
if err != nil {
return fmt.Errorf("failed to send the request to the server: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 204 {
return fmt.Errorf("failed to send the request to the server: %s: %s", res.Status, toError(res.Body))
}
return nil
}
func (c *Client) RunOne(pr project.Project) error {
url, err := url.JoinPath(c.url, "api", "v1", "run")
if err != nil {
return fmt.Errorf("failed to make url: %s", err)
}
data, err := json.Marshal(pr)
if err != nil {
return fmt.Errorf("failed to marshal project data: %s", err)
}
r := bytes.NewReader(data)
req, err := http.NewRequest("EXECUTE", url, r)
if err != nil {
return fmt.Errorf("failed to make request: %s", err)
}
cli := http.Client{}
res, err := cli.Do(req)
if err != nil {
return fmt.Errorf("failed to send the request to the server: %s", err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("failed to send the request to the server: %s: %s", res.Status, toError(res.Body))
}
return nil
}
func toError(body io.ReadCloser) error {
var msg SimpleError

View File

@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goccy/go-yaml"
@@ -49,6 +48,11 @@ type (
Username string `yaml:"username"`
Password string `yaml:"password"`
}
DefaultValues struct {
DaemonURL string
ProjectName string
}
)
var (
@@ -57,7 +61,7 @@ var (
ErrParsing error = errors.New("failed to parse file")
)
func LoadCurrent() (Project, error) {
func LoadCurrent(defaultValues DefaultValues) (Project, error) {
f, err := os.OpenFile("./git-compose.yaml", os.O_RDONLY, 0)
if err != nil {
return Project{}, fmt.Errorf("%w: %s", ErrIO, err)
@@ -70,31 +74,26 @@ func LoadCurrent() (Project, error) {
return Project{}, fmt.Errorf("%w: %s", ErrParsing, err)
}
return decode(mainFile)
return decode(mainFile, defaultValues)
}
func LoadBytes(b []byte) (Project, error) {
func LoadBytes(b []byte, defaultValues DefaultValues) (Project, error) {
var mainFile MainFile
if err := json.Unmarshal(b, &mainFile); err != nil {
return Project{}, fmt.Errorf("%w: %s", ErrParsing, err)
}
return decode(mainFile)
return decode(mainFile, defaultValues)
}
func decode(mainFile MainFile) (Project, error) {
wd, err := os.Getwd()
if err != nil {
return Project{}, fmt.Errorf("%w: cannot get current working directory path: %s", ErrOS, err)
}
func decode(mainFile MainFile, defaultValues DefaultValues) (Project, error) {
if err := checkConfig(mainFile); err != nil {
return Project{}, fmt.Errorf("failed to validate configuration: %w", err)
}
pr := Project{
Name: filepath.Base(wd),
ServerURL: "http://localhost:25697",
Name: defaultValues.ProjectName,
ServerURL: defaultValues.DaemonURL,
}
if len(strings.TrimSpace(mainFile.ProjectName)) > 0 {

View File

@@ -2,12 +2,14 @@ package project
type (
Project struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Repositories []Repository `json:"repositories"`
ServerURL string `json:"-"`
}
Repository struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Schedule string `json:"schedule"`
Source string `json:"source"`