Sagi Dayan
d72005afe1
All checks were successful
Codespell / Check for spelling errors (push) Successful in 23s
Signed-off-by: Sagi Dayan <sagidayan@gmail.com>
58 lines
1.3 KiB
Go
58 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 `json:"envorinments" default:"[]"`
|
|
}
|
|
|
|
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
|
|
}
|