Expand logging, and daemonless command support.
This adds more logging in certain places, and adds logging to the CLI. It also allows for certain commands in the CLI to be used without a daemon connection, namely `init`, which previously required the daemon to be connected, but now does not since it doesnt need it.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/juls0730/flux/pkg"
|
||||
"github.com/juls0730/flux/pkg/API"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Project struct {
|
||||
@@ -14,7 +15,7 @@ type Project struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func GetProject(command string, args []string, config pkg.CLIConfig) (*Project, error) {
|
||||
func GetProject(command string, args []string, config pkg.CLIConfig, logger *zap.SugaredLogger) (*Project, error) {
|
||||
var projectName string
|
||||
|
||||
// we are in a project directory and the project is deployed
|
||||
@@ -24,7 +25,7 @@ func GetProject(command string, args []string, config pkg.CLIConfig) (*Project,
|
||||
return nil, fmt.Errorf("failed to read .fluxid: %v", err)
|
||||
}
|
||||
|
||||
app, err := GetRequest[API.App](config.DaemonURL + "/app/by-id/" + string(id))
|
||||
app, err := GetRequest[API.App](config.DaemonURL+"/app/by-id/"+string(id), logger)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get app: %v", err)
|
||||
}
|
||||
@@ -58,7 +59,7 @@ func GetProject(command string, args []string, config pkg.CLIConfig) (*Project,
|
||||
}
|
||||
|
||||
// we are calling flux with a project name (ie `flux start my-project`)
|
||||
app, err := GetRequest[API.App](config.DaemonURL + "/app/by-name/" + projectName)
|
||||
app, err := GetRequest[API.App](config.DaemonURL+"/app/by-name/"+projectName, logger)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get app: %v", err)
|
||||
}
|
||||
|
||||
@@ -6,17 +6,26 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func isOk(statusCode int) bool {
|
||||
return statusCode >= 200 && statusCode < 300
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func GetRequest[T any](url string, logger *zap.SugaredLogger) (*T, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
logger.Debugw("GET request failed", zap.String("url", url), zap.Error(err))
|
||||
return nil, fmt.Errorf("http get request failed: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.Debugw("GET request", zap.String("url", url), resp)
|
||||
|
||||
if !isOk(resp.StatusCode) {
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading response body: %v", err)
|
||||
@@ -35,7 +44,7 @@ func GetRequest[T any](url string) (*T, error) {
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
func DeleteRequest(url string) error {
|
||||
func DeleteRequest(url string, logger *zap.SugaredLogger) error {
|
||||
req, err := http.NewRequest("DELETE", url, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete: %v", err)
|
||||
@@ -46,7 +55,9 @@ func DeleteRequest(url string) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.Debugw("DELETE request", zap.String("url", url), resp)
|
||||
|
||||
if !isOk(resp.StatusCode) {
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading response body: %v", err)
|
||||
@@ -60,7 +71,7 @@ func DeleteRequest(url string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func PutRequest(url string, data io.Reader) error {
|
||||
func PutRequest(url string, data io.Reader, logger *zap.SugaredLogger) error {
|
||||
req, err := http.NewRequest("PUT", url, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to put: %v", err)
|
||||
@@ -71,7 +82,9 @@ func PutRequest(url string, data io.Reader) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
logger.Debugw("PUT request", zap.String("url", url), resp)
|
||||
|
||||
if !isOk(resp.StatusCode) {
|
||||
responseBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading response body: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user