Massive architectural rework

This commit massively overhauls the project's structure to simplify
development. Most parts are now correctly compartmentalized and
dependencies are passed in a sane way rather than global variables
galore xd.
This commit is contained in:
Zoe
2025-05-02 12:15:40 -05:00
parent f4bf2ff5a1
commit c891c24843
50 changed files with 2684 additions and 2410 deletions

View File

@@ -0,0 +1,70 @@
package cli_util
import (
"encoding/json"
"fmt"
"os"
"github.com/juls0730/flux/pkg"
"github.com/juls0730/flux/pkg/API"
)
type Project struct {
Id string `json:"id"`
Name string `json:"name"`
}
func GetProject(command string, args []string, config pkg.CLIConfig) (*Project, error) {
var projectName string
// we are in a project directory and the project is deployed
if _, err := os.Stat(".fluxid"); err == nil {
id, err := os.ReadFile(".fluxid")
if err != nil {
return nil, fmt.Errorf("failed to read .fluxid: %v", err)
}
app, err := GetRequest[API.App](config.DaemonURL + "/app/by-id/" + string(id))
if err != nil {
return nil, fmt.Errorf("failed to get app: %v", err)
}
return &Project{
Id: app.Id.String(),
Name: app.Name,
}, nil
}
// we are calling flux from a project directory, but the project isnt deployed yet
if len(args) == 0 {
if _, err := os.Stat("flux.json"); err != nil {
return nil, fmt.Errorf("the current directory is not a flux project, please run flux %[1]s in the project directory", command)
}
fluxConfigFile, err := os.Open("flux.json")
if err != nil {
return nil, fmt.Errorf("failed to open flux.json: %v", err)
}
defer fluxConfigFile.Close()
var config pkg.ProjectConfig
if err := json.NewDecoder(fluxConfigFile).Decode(&config); err != nil {
return nil, fmt.Errorf("failed to decode flux.json: %v", err)
}
projectName = config.Name
} else {
projectName = args[0]
}
// we are calling flux with a project name (ie `flux start my-project`)
app, err := GetRequest[API.App](config.DaemonURL + "/app/by-name/" + projectName)
if err != nil {
return nil, fmt.Errorf("failed to get app: %v", err)
}
return &Project{
Id: app.Id.String(),
Name: app.Name,
}, nil
}

View File

@@ -0,0 +1,86 @@
package cli_util
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
// make a function that makes an http GET request to the daemon and returns data of type T
func GetRequest[T any](url string) (*T, error) {
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("http get request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
responseBody = []byte(strings.TrimSuffix(string(responseBody), "\n"))
return nil, fmt.Errorf("http get request failed: %s", responseBody)
}
var data T
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, fmt.Errorf("failed to decode http response: %v", err)
}
return &data, nil
}
func DeleteRequest(url string) error {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return fmt.Errorf("failed to delete: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to delete: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}
responseBody = []byte(strings.TrimSuffix(string(responseBody), "\n"))
return fmt.Errorf("delete failed: %s", responseBody)
}
return nil
}
func PutRequest(url string, data io.Reader) error {
req, err := http.NewRequest("PUT", url, data)
if err != nil {
return fmt.Errorf("failed to put: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to put: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}
responseBody = []byte(strings.TrimSuffix(string(responseBody), "\n"))
return fmt.Errorf("put failed: %s", responseBody)
}
return nil
}

View File

@@ -0,0 +1,72 @@
package cli_util
import (
"fmt"
"os"
"sync"
)
type CustomSpinnerWriter struct {
currentSpinnerMsg string
lock sync.Mutex
}
func NewCustomSpinnerWriter() *CustomSpinnerWriter {
return &CustomSpinnerWriter{
currentSpinnerMsg: "",
lock: sync.Mutex{},
}
}
func (w *CustomSpinnerWriter) Write(p []byte) (n int, err error) {
w.lock.Lock()
defer w.lock.Unlock()
n, err = os.Stdout.Write(p)
if err != nil {
return n, err
}
w.currentSpinnerMsg = string(p)
return len(p), nil
}
type CustomStdout struct {
spinner *CustomSpinnerWriter
lock sync.Mutex
}
func NewCustomStdout(spinner *CustomSpinnerWriter) *CustomStdout {
return &CustomStdout{
spinner: spinner,
lock: sync.Mutex{},
}
}
// We have this custom writer because we want to have a spinner at the bottom of the terminal, but we dont want to have
// it interfere with the output of the command
func (w *CustomStdout) Write(p []byte) (n int, err error) {
w.lock.Lock()
defer w.lock.Unlock()
// clear line and carriage return
n, err = os.Stdout.Write(fmt.Appendf(nil, "\033[2K\r%s", p))
if err != nil {
return n, err
}
nn, err := os.Stdout.Write([]byte(w.spinner.currentSpinnerMsg))
if err != nil {
return n, err
}
n = nn + n
return n, nil
}
func (w *CustomStdout) Printf(format string, a ...interface{}) (n int, err error) {
str := fmt.Sprintf(format, a...)
return w.Write([]byte(str))
}