✨ add "new" command
This commit is contained in:
@ -18,7 +18,7 @@ import (
|
||||
type Project struct {
|
||||
Name string `yaml:"name"`
|
||||
Root string `yaml:"root"`
|
||||
OnProjectStart []string `yaml:"on_project_start"`
|
||||
OnProjectStart []string `yaml:"on_project_start,omitempty"`
|
||||
Windows []Window `yaml:"windows"`
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ func ListProjects() error {
|
||||
func LoadProject(name string) (*Project, error) {
|
||||
project := &Project{}
|
||||
|
||||
fileName := path.Join(getConfigDir(), name+".yml")
|
||||
fileName := getConfigFilePath(name)
|
||||
|
||||
data, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
@ -107,6 +107,47 @@ func LoadProject(name string) (*Project, error) {
|
||||
return project, err
|
||||
}
|
||||
|
||||
func NewProject(name string) error {
|
||||
project := &Project{
|
||||
Name: name,
|
||||
Root: "~/",
|
||||
OnProjectStart: []string{""},
|
||||
Windows: make([]Window, 3),
|
||||
}
|
||||
|
||||
project.Windows[0] = Window{
|
||||
Name: "shell",
|
||||
Commands: []string{""},
|
||||
}
|
||||
|
||||
project.Windows[1] = Window{
|
||||
Name: "server",
|
||||
Commands: []string{""},
|
||||
}
|
||||
|
||||
project.Windows[2] = Window{
|
||||
Name: "logs",
|
||||
Commands: []string{""},
|
||||
}
|
||||
|
||||
return project.Save()
|
||||
}
|
||||
|
||||
func (p *Project) Save() error {
|
||||
fileName := getConfigFilePath(p.Name)
|
||||
|
||||
_, err := os.Stat(fileName)
|
||||
if err == nil {
|
||||
return errors.New("Config file already exists")
|
||||
}
|
||||
|
||||
data, err := yaml.Marshal(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(fileName, data, 0644)
|
||||
}
|
||||
|
||||
func (p *Project) GetRoot() string {
|
||||
rootPath, err := homedir.Expand(p.Root)
|
||||
if err != nil {
|
||||
@ -120,6 +161,10 @@ func getConfigDir() string {
|
||||
return path.Join(home, ".workon")
|
||||
}
|
||||
|
||||
func getConfigFilePath(name string) string {
|
||||
return path.Join(getConfigDir(), name+".yml")
|
||||
}
|
||||
|
||||
func sessionExists(name string) bool {
|
||||
t := Tmux{}
|
||||
result, err := t.Exec("ls")
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
|
||||
type Window struct {
|
||||
Name string `yaml:"name"`
|
||||
Root string `yaml:"root"`
|
||||
Commands []string `yaml:"commands"`
|
||||
Root string `yaml:"root,omitempty"`
|
||||
Commands []string `yaml:"commands,omitempty"`
|
||||
ID string `yaml:"-"`
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user