Files
flux/internal/util/cli/project.go
Zoe c51eca5dab 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.
2025-05-08 09:53:41 -05:00

72 lines
1.8 KiB
Go

package cli_util
import (
"encoding/json"
"fmt"
"os"
"github.com/juls0730/flux/pkg"
"github.com/juls0730/flux/pkg/API"
"go.uber.org/zap"
)
type Project struct {
Id string `json:"id"`
Name string `json:"name"`
}
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
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), logger)
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, logger)
if err != nil {
return nil, fmt.Errorf("failed to get app: %v", err)
}
return &Project{
Id: app.Id.String(),
Name: app.Name,
}, nil
}