diff --git a/cmd/new.go b/cmd/new.go new file mode 100644 index 0000000..f31e970 --- /dev/null +++ b/cmd/new.go @@ -0,0 +1,49 @@ +/* +Copyright © 2020 James Walker + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/walkah/workon/tmux" +) + +// newCmd represents the new command +var newCmd = &cobra.Command{ + Use: "new [NAME]", + Short: "Create a new project", + Run: func(cmd *cobra.Command, args []string) { + if len(args) < 1 { + fmt.Println("Provide a project name") + return + } + err := tmux.NewProject(args[0]) + if err != nil { + fmt.Println("Unable to create project:", err) + } + }, +} + +func init() { + rootCmd.AddCommand(newCmd) +} diff --git a/tmux/project.go b/tmux/project.go index cf21094..1a0e242 100644 --- a/tmux/project.go +++ b/tmux/project.go @@ -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") diff --git a/tmux/window.go b/tmux/window.go index 93c4f99..c669ed5 100644 --- a/tmux/window.go +++ b/tmux/window.go @@ -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:"-"` }