wip (apply)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"mirror-sync/pkg/client"
|
||||
"mirror-sync/pkg/project"
|
||||
"os"
|
||||
|
||||
@@ -18,7 +19,7 @@ type (
|
||||
func (*ApplyCmd) Name() string { return "apply" }
|
||||
func (*ApplyCmd) Synopsis() string { return "apply the current project settings" }
|
||||
func (*ApplyCmd) Usage() string {
|
||||
return `Usage: git-sync apply
|
||||
return `Usage: mirror-sync apply
|
||||
|
||||
apply the current project settings
|
||||
|
||||
@@ -36,7 +37,11 @@ func (p *ApplyCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
|
||||
cli := client.New(projectConfig.ServerURL)
|
||||
if err := cli.Apply(projectConfig); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
||||
return subcommands.ExitFailure
|
||||
}
|
||||
|
||||
return subcommands.ExitSuccess
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ type (
|
||||
func (*VersionCmd) Name() string { return "version" }
|
||||
func (*VersionCmd) Synopsis() string { return "show version and system information" }
|
||||
func (*VersionCmd) Usage() string {
|
||||
return `Usage: cloudsave version
|
||||
return `Usage: mirror-sync version
|
||||
|
||||
Print the version of the software
|
||||
|
||||
@@ -36,7 +36,7 @@ func (p *VersionCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{
|
||||
}
|
||||
|
||||
func local() {
|
||||
fmt.Println("Client: git-sync cli")
|
||||
fmt.Println("Client: mirror-sync cli")
|
||||
fmt.Println(" Version: " + constants.Version)
|
||||
fmt.Println(" API version: " + strconv.Itoa(constants.ApiVersion))
|
||||
fmt.Println(" Go version: " + runtime.Version())
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"mirror-sync/cmd/server/core/storage"
|
||||
"mirror-sync/pkg/constants"
|
||||
"mirror-sync/pkg/project"
|
||||
"mirror-sync/pkg/remote/obj"
|
||||
"net/http"
|
||||
"runtime"
|
||||
@@ -14,11 +18,14 @@ import (
|
||||
type (
|
||||
HTTPServer struct {
|
||||
Server *http.Server
|
||||
data *storage.Repository
|
||||
}
|
||||
)
|
||||
|
||||
func NewServer(port int) *HTTPServer {
|
||||
s := &HTTPServer{}
|
||||
func NewServer(data *storage.Repository, port int) *HTTPServer {
|
||||
s := &HTTPServer{
|
||||
data: data,
|
||||
}
|
||||
router := chi.NewRouter()
|
||||
router.NotFound(func(writer http.ResponseWriter, request *http.Request) {
|
||||
notFound("id not found", writer, request)
|
||||
@@ -35,8 +42,10 @@ func NewServer(port int) *HTTPServer {
|
||||
routerAPI.Route("/v1", func(r chi.Router) {
|
||||
// Get information about the server
|
||||
r.Get("/version", s.Information)
|
||||
r.Route("/sync", func(r chi.Router) {
|
||||
|
||||
r.Route("/projects", func(r chi.Router) {
|
||||
r.Get("/{name}", func(w http.ResponseWriter, r *http.Request) {})
|
||||
r.Post("/{name}", s.ProjectPostHandler)
|
||||
r.Delete("/{name}", func(w http.ResponseWriter, r *http.Request) {})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -57,3 +66,28 @@ func (s *HTTPServer) Information(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
ok(info, w, r)
|
||||
}
|
||||
|
||||
func (s *HTTPServer) ProjectPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
name := chi.URLParam(r, "name")
|
||||
|
||||
if len(name) == 0 {
|
||||
badRequest("project name cannot be empty", w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var pr project.Project
|
||||
d := json.NewDecoder(r.Body)
|
||||
if err := d.Decode(&pr); err != nil {
|
||||
slog.Error("failed to parse project description", "err", err)
|
||||
internalServerError(err, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.data.Save(pr); err != nil {
|
||||
slog.Error("failed to save project to the database", "err", err)
|
||||
internalServerError(err, w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(201)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ func recoverMiddleware(next http.Handler) http.Handler {
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
internalServerError(err, w, r)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
@@ -2,13 +2,14 @@ package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"mirror-sync/pkg/remote/obj"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func internalServerError(w http.ResponseWriter, r *http.Request) {
|
||||
func internalServerError(err any, w http.ResponseWriter, r *http.Request) {
|
||||
payload := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusInternalServerError,
|
||||
@@ -16,7 +17,7 @@ func internalServerError(w http.ResponseWriter, r *http.Request) {
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Error: "Internal Server Error",
|
||||
Message: "The server encountered an unexpected condition that prevented it from fulfilling the request.",
|
||||
Message: fmt.Sprintf("%v", err),
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
25
cmd/server/core/storage/migration.go
Normal file
25
cmd/server/core/storage/migration.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
"github.com/pressly/goose/v3"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var embedMigrations embed.FS
|
||||
|
||||
func (r *Repository) Migrate() error {
|
||||
goose.SetBaseFS(embedMigrations)
|
||||
|
||||
if err := goose.SetDialect("sqlite3"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := goose.Up(r.db, "migrations"); err != nil {
|
||||
return fmt.Errorf("failed to migrate the database: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
18
cmd/server/core/storage/migrations/001_first_schema.sql
Normal file
18
cmd/server/core/storage/migrations/001_first_schema.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE Projects (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE Repositories (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
schedule TEXT NOT NULL,
|
||||
"source" TEXT NOT NULL,
|
||||
destination TEXT NOT NULL,
|
||||
project INTEGER NOT NULL
|
||||
);
|
||||
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE Projects;
|
||||
73
cmd/server/core/storage/storage.go
Normal file
73
cmd/server/core/storage/storage.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"mirror-sync/pkg/project"
|
||||
|
||||
_ "github.com/glebarez/go-sqlite"
|
||||
)
|
||||
|
||||
type (
|
||||
Repository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
)
|
||||
|
||||
func OpenDB(path string) (*Repository, error) {
|
||||
// connect
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open database: %s", err)
|
||||
}
|
||||
|
||||
return &Repository{
|
||||
db: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Save(pr project.Project) (err error) {
|
||||
tx, err := r.db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create transaction: %s", err)
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
}()
|
||||
|
||||
stmt, err := tx.Prepare("INSERT INTO Projects (name) VALUES (?)")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create statement: %s", err)
|
||||
}
|
||||
if _, err := stmt.Exec(pr.Name); err != nil {
|
||||
return fmt.Errorf("failed to execute sql query: %s", err)
|
||||
}
|
||||
|
||||
rows, err := tx.Query("SELECT id FROM Projects WHERE name = ?", pr.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to query project id: %s", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var id int
|
||||
rows.Next()
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return fmt.Errorf("failed to query project id: %s", err)
|
||||
}
|
||||
|
||||
for _, repo := range pr.Repositories {
|
||||
stmt, err := tx.Prepare("INSERT INTO Repositories (name, source, destination, schedule, project) VALUES (?, ?, ?, ?, ?)")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create statement: %s", err)
|
||||
}
|
||||
if _, err := stmt.Exec(repo.Name, repo.Source, repo.Destination, repo.Schedule, id); err != nil {
|
||||
return fmt.Errorf("failed to execute sql query: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,19 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"mirror-sync/cmd/server/api"
|
||||
"mirror-sync/cmd/server/core/storage"
|
||||
"mirror-sync/pkg/constants"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var dbPath string
|
||||
flag.StringVar(&dbPath, "db-path", "/var/lib/mirror-sync/data.db", "path to the sqlite database")
|
||||
flag.Parse()
|
||||
|
||||
fmt.Printf("mirror-sync daemon -- v%s.%s.%s\n\n", constants.Version, runtime.GOOS, runtime.GOARCH)
|
||||
|
||||
s := api.NewServer(8080)
|
||||
data, err := storage.OpenDB(dbPath)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to start server:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println("daemon listening to :8080")
|
||||
if err := data.Migrate(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to start server:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
s := api.NewServer(data, 8080)
|
||||
|
||||
slog.Info("daemon listening to :8080")
|
||||
if err := s.Server.ListenAndServe(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to start server:", err.Error())
|
||||
os.Exit(1)
|
||||
|
||||
16
go.mod
16
go.mod
@@ -3,10 +3,12 @@ module mirror-sync
|
||||
go 1.25
|
||||
|
||||
require (
|
||||
github.com/glebarez/go-sqlite v1.22.0
|
||||
github.com/go-chi/chi/v5 v5.2.3
|
||||
github.com/go-git/go-git/v6 v6.0.0-20250929195514-145daf2492dd
|
||||
github.com/goccy/go-yaml v1.18.0
|
||||
github.com/google/subcommands v1.2.0
|
||||
github.com/pressly/goose/v3 v3.26.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -14,15 +16,29 @@ require (
|
||||
github.com/ProtonMail/go-crypto v1.3.0 // indirect
|
||||
github.com/cloudflare/circl v1.6.1 // indirect
|
||||
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/emirpasic/gods v1.18.1 // indirect
|
||||
github.com/go-git/gcfg/v2 v2.0.2 // indirect
|
||||
github.com/go-git/go-billy/v6 v6.0.0-20250627091229-31e2a16eef30 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/kevinburke/ssh_config v1.4.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mfridman/interpolate v0.0.2 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/pjbgf/sha1cd v0.5.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/sergi/go-diff v1.4.0 // indirect
|
||||
github.com/sethvargo/go-retry v0.3.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/crypto v0.42.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/net v0.44.0 // indirect
|
||||
golang.org/x/sync v0.16.0 // indirect
|
||||
golang.org/x/sys v0.36.0 // indirect
|
||||
modernc.org/libc v1.66.3 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
modernc.org/sqlite v1.38.2 // indirect
|
||||
)
|
||||
|
||||
57
go.sum
57
go.sum
@@ -13,10 +13,14 @@ github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGL
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
|
||||
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=
|
||||
github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc=
|
||||
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
|
||||
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
|
||||
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
|
||||
@@ -33,8 +37,12 @@ github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
|
||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||
github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE=
|
||||
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/kevinburke/ssh_config v1.4.0 h1:6xxtP5bZ2E4NF5tuQulISpTO2z8XbtH8cg1PWkxoFkQ=
|
||||
github.com/kevinburke/ssh_config v1.4.0/go.mod h1:q2RIzfka+BXARoNexmF9gkxEX7DmvbW9P4hIVx2Kg4M=
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||
@@ -42,29 +50,78 @@ github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
|
||||
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/pjbgf/sha1cd v0.5.0 h1:a+UkboSi1znleCDUNT3M5YxjOnN1fz2FhN48FlwCxs0=
|
||||
github.com/pjbgf/sha1cd v0.5.0/go.mod h1:lhpGlyHLpQZoxMv8HcgXvZEhcGs0PG/vsZnEJ7H0iCM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pressly/goose/v3 v3.26.0 h1:KJakav68jdH0WDvoAcj8+n61WqOIaPGgH0bJWS6jpmM=
|
||||
github.com/pressly/goose/v3 v3.26.0/go.mod h1:4hC1KrritdCxtuFsqgs1R4AU5bWtTAf+cnWvfhf2DNY=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
|
||||
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
|
||||
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
|
||||
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
||||
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
|
||||
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
|
||||
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
||||
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
||||
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
|
||||
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ=
|
||||
golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA=
|
||||
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
|
||||
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
modernc.org/cc/v4 v4.26.2 h1:991HMkLjJzYBIfha6ECZdjrIYz2/1ayr+FL8GN+CNzM=
|
||||
modernc.org/cc/v4 v4.26.2/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||
modernc.org/ccgo/v4 v4.28.0 h1:rjznn6WWehKq7dG4JtLRKxb52Ecv8OUGah8+Z/SfpNU=
|
||||
modernc.org/ccgo/v4 v4.28.0/go.mod h1:JygV3+9AV6SmPhDasu4JgquwU81XAKLd3OKTUDNOiKE=
|
||||
modernc.org/fileutil v1.3.8 h1:qtzNm7ED75pd1C7WgAGcK4edm4fvhtBsEiI/0NQ54YM=
|
||||
modernc.org/fileutil v1.3.8/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
|
||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
|
||||
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.38.2 h1:Aclu7+tgjgcQVShZqim41Bbw9Cho0y/7WzYptXqkEek=
|
||||
modernc.org/sqlite v1.38.2/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
|
||||
71
pkg/client/client.go
Normal file
71
pkg/client/client.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mirror-sync/pkg/project"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type (
|
||||
Client struct {
|
||||
url string
|
||||
}
|
||||
|
||||
SimpleError struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
)
|
||||
|
||||
func New(url string) *Client {
|
||||
return &Client{
|
||||
url: url,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Apply(pr project.Project) error {
|
||||
url, err := url.JoinPath(c.url, "api", "v1", "projects", pr.Name)
|
||||
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("POST", url, r)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to generate http request: %s", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
var 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 != 201 {
|
||||
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
|
||||
|
||||
d := json.NewDecoder(body)
|
||||
if err := d.Decode(&msg); err != nil {
|
||||
return fmt.Errorf("failed to decode error message: %s", err)
|
||||
}
|
||||
|
||||
return fmt.Errorf("%s", msg.Message)
|
||||
}
|
||||
@@ -14,6 +14,13 @@ type (
|
||||
MainFile struct {
|
||||
Repositories map[string]RepositoryDescriptor `yaml:"repositories"`
|
||||
ProjectName string `yaml:"project_name"`
|
||||
Server ServerDescriptor `yaml:"server"`
|
||||
}
|
||||
|
||||
ServerDescriptor struct {
|
||||
Hostname string `yaml:"hostname"`
|
||||
Port int `yaml:"port"`
|
||||
Insecure bool `yaml:"insecure"`
|
||||
}
|
||||
|
||||
RepositoryDescriptor struct {
|
||||
@@ -53,12 +60,25 @@ func LoadCurrent() (Project, error) {
|
||||
|
||||
pr := Project{
|
||||
Name: filepath.Base(wd),
|
||||
ServerURL: "http://localhost:8080",
|
||||
}
|
||||
|
||||
if len(strings.TrimSpace(mainFile.ProjectName)) > 0 {
|
||||
pr.Name = mainFile.ProjectName
|
||||
}
|
||||
|
||||
if len(strings.TrimSpace(mainFile.Server.Hostname)) > 0 {
|
||||
method := "https"
|
||||
port := 8080
|
||||
if mainFile.Server.Insecure {
|
||||
method = "http"
|
||||
}
|
||||
if mainFile.Server.Port > 0 {
|
||||
port = mainFile.Server.Port
|
||||
}
|
||||
pr.ServerURL = fmt.Sprintf("%s://%s:%d", method, mainFile.Server.Hostname, port)
|
||||
}
|
||||
|
||||
for repoName, repo := range mainFile.Repositories {
|
||||
pr.Repositories = append(pr.Repositories, Repository{
|
||||
Name: fmt.Sprintf("%s-%s", pr.Name, strings.ToLower(repoName)),
|
||||
|
||||
@@ -4,6 +4,7 @@ type (
|
||||
Project struct {
|
||||
Name string `json:"name"`
|
||||
Repositories []Repository `json:"repositories"`
|
||||
ServerURL string `json:"-"`
|
||||
}
|
||||
|
||||
Repository struct {
|
||||
|
||||
Reference in New Issue
Block a user