envoid/internal/types/project.go
Sagi Dayan 22bb2adcaf
Some checks failed
CI / check for spelling errors (pull_request) Successful in 20s
CI / code quality (lint/tests) (pull_request) Failing after 24s
CI / make sure build does not fail (pull_request) Has been skipped
CI / notify-fail (pull_request) Successful in 14s
Added lint to the CI. formatted all code accordingly
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
2024-12-17 13:06:41 +02:00

66 lines
1.3 KiB
Go

package types
import (
"encoding/base64"
"fmt"
"os"
"git.dayanhub.com/sagi/envoid/internal/errors"
)
type Project struct {
Path string `json:"path"`
Name string `json:"name"`
Password string `json:"password"`
Environments []*Environment `default:"[]" json:"envorinments"`
}
func (p *Project) GetEnv(name string) (*Environment, error) {
var e *Environment
for _, env := range p.Environments {
if env.Name == name {
passByte, err := base64.RawStdEncoding.DecodeString(p.Password)
if err != nil {
fmt.Printf("[ERROR] Failed to decode project password @ %s. %e\n", p.Path, err)
os.Exit(1)
}
e = &Environment{
Name: env.Name,
Password: string(passByte),
}
}
}
if e == nil {
return nil, errors.NewEnvironmentNotFoundError(p.Path, name)
}
return e, nil
}
func (p *Project) NewEnv(name string) error {
e := &Environment{
Name: name,
}
p.Environments = append(p.Environments, e)
return nil
}
func (p *Project) RemoveEnv(name string) {
environments := []*Environment{}
for _, env := range p.Environments {
if env.Name != name {
environments = append(environments, env)
}
}
p.Environments = environments
}
func (p *Project) IsEmpty() bool {
return len(p.Environments) == 0
}